How to make docker image

Time:2021-4-8

This document describes two methods of making docker image. The system used isCentOS7

Two methods of image docker

Method 1: docker commit # save the current state of the container to the image, and then generate the corresponding image
Method 2: docker build # use dockerfile file to make image automatically

Method 1: docker commit

Create a container image with Apache tools installed

[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 470671670cac 4 months ago 237MB
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[[email protected] ~]# docker run -it centos:latest /bin/bash
[[email protected] /]#
[ [email protected] /]#Yum – y install httpd # install Apache package in container
[[email protected] /]# exit

View the images list

[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 470671670cac 4 months ago 237MB

Make an image image according to the current state of the container: create a CentOS image with Apache tools installed

Syntax: docker commit “ID of container” or “image”_ name”

View container ID

[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b96e68a3cce centos:latest “/bin/bash” 3 minutes ago Exited (0) 2 minutes ago awesome_hypatia
607752360adf centos:latest “/bin/bash -c ‘while…” 18 minutes ago Exited (137) 12 minutes ago brave_fermi
0a297ff99af8 centos:latest “/bin/bash” 22 minutes ago Exited (1) 20 minutes ago ecstatic_yonath
efb4af688330 centos:latest “/bin/bash” 24 minutes ago Exited (0) 23 minutes ago epic_mcclintock
[[email protected] ~]# docker commit 1b96e68a3cce centos:apache
sha256:b8822ec8a7bbb325793e7908e355180be82a49481cff197661fb5d0da5872e88
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos apache b8822ec8a7bb 9 seconds ago 280MB
centos latest 470671670cac 4 months ago 237MB

Use the newly created centos:apache Image to generate a container instance

[[email protected] ~]# docker run -it centos:apache /bin/bash
[[email protected] /]# rpm -qa httpd
httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64

The httpd software name indicates that the container based on Apache image is created successfully

Method 2: create a CentOS based httpd web server image through docker build

When using docker build to create images, it is necessary to use dockerfile file to automatically create image images
Dockerfile is a bit like makefile generated after source code compilation. / configure

1. Create working directory

[[email protected] ~]# mkdir /docker-build
[[email protected] ~]# cd /docker-build
[[email protected] docker-build]# touch Dockerfile
[[email protected] docker-build]# ls
Dockerfile
Note: makefile is required for automatic compilation of make, and dockerfile is required for automatic creation of docker image

2. Dockerfile editing

Dockerfile is used to create a user-defined image, including the software dependencies specified by the user.
[[email protected] docker-build ]# vim Dockerfile
FROM centos:latest
MAINTAINER RUN yum -y install httpd
ADD start.sh /usr/local/bin/start.sh
ADD index.html /var/www/html/index.html
CMD echo hello world

notes

FROM centos:latest # Which image is from based on
Maintainer # creator of maintainer image
Run Yum – y install httpd
ADD start.sh /usr/local/bin/start.sh
ADD index.html /var/www/html/index.html

Add copies the file to the path corresponding to the file system of the newly generated image. All copies to the new image

File and folder permissions are 0755, uid and GID are 0
Implementation of CMD echo Hello world # container when it is startedcommandOr start the service, but there can only be one CMD in a docker filecommandIf there are more than one, the last CMD will be executed

3. Create start.shscriptStart httpd service and Apache default home page index.html file

[[email protected] docker-build ]# echo “#!/bin/bash” >> start.sh
[[email protected] docker-build ]# echo “/usr/sbin/httpd -DFOREGROUND” >> start.sh
Note: usr / SBIN / httpd – dforeround is equivalent to executing systemctl start httpd

[[email protected] docker-build ]# chmod a+x start.sh

establish index.html
[[email protected] docker-build ]# echo “docker image build test from jaking” > index.html

4. Use the command build to create a new image

Syntax: docker build – t parent image name: the path of the image’s tag dockerfile file
-T: for tag, image name

Example: use the command docker build to create a new image and name it centos:httpd

[[email protected] docker-build]# ls
Dockerfile index.html start.sh
[[email protected] docker-build]# docker build -t centos:httpd ./

Note:. / indicates the current directory. In addition, your current directory should contain dockerfile

Sending build context to Docker daemon 4.096kB
Step 1/5 : FROM centos:latest
—> 470671670cac
Step 2/5 : MAINTAINER —> Running in 0180810d2ab3
Removing intermediate container 0180810d2ab3
—> 5b9af0184bcf
Step 3/5 : RUN yum -y install httpd
—> Running in 8f5c114649ed
CentOS-8 – AppStream 228 kB/s | 7.0 MB 00:31
CentOS-8 – Base 547 kB/s | 2.2 MB 00:04
CentOS-8 – Extras 431 B/s | 5.9 kB 00:14

Dependencies resolved.

Package Arch Version Repo Size

Installing:
httpd x86_64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 1.7 M
Installing dependencies:
apr x86_64 1.6.3-9.el8 AppStream 125 k
apr-util x86_64 1.6.1-6.el8 AppStream 105 k
centos-logos-httpd

               noarch 80.5-2.el8                            AppStream  24 k

… omit part of output information

Complete!
Removing intermediate container 8f5c114649ed
—> 040b5f229962
Step 4/5 : ADD start.sh /usr/local/bin/start.sh
—> 11a106005031
Step 5/5 : ADD index.html /var/www/html/index.html
—> 85b4a3657ced
Successfully built 85b4a3657ced
Successfully tagged centos:httpd

View the images list

[[email protected] docker-build]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd 85b4a3657ced 45 seconds ago 280MB
centos apache b8822ec8a7bb 20 minutes ago 280MB
centos latest 470671670cac 4 months ago 237MB

Note: docker image = Application / program + Library

Run the newly generated image

[[email protected] docker-build]# docker run -it centos:httpd /bin/bash
[[email protected] /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[[email protected] /]# rpm -qa httpd
httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
[[email protected] /]# exit
exit
[[email protected] docker-build]#

summary

The above is the introduction of docker image making method, hoping to bring help to you.

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]