Home >Operation and Maintenance >Docker >There are several ways to create a Docker image
There are two ways to create a Docker image: 1. Use the "docker commit" command to manually build the image based on the existing container; 2. Use Dockerfile to automatically build the image. The Docker program will read the instructions in the Dockerfile build file. Automatically generate images.
The operating environment of this tutorial: linux5.9.8 system, docker-1.13.1 version, Dell G3 computer.
Sometimes the image downloaded from the Docker image warehouse cannot meet the requirements. We can build an own image based on a basic image.
Under what circumstances Next we need to build the image ourselves?
(1) When we cannot find an existing image, such as an application developed by ourselves
(2) We need to add specific functions to the image
docker build There are two ways to image: docker commit
command and Dockerfile
Build file
Building an image based on an existing container is mainly done through docker commit
command to build a new image.
There are three main steps for dockercommit construction:
For example, install the vim editor in the centos image and save it as a new image
(1) Run the container
[root@ken1 docker]# docker run -it centos Unable to find image 'centos:latest' locally latest: Pulling from library/centos a02a4930cb5d: Pull complete Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426 Status: Downloaded newer image for centos:latest
(2) Install the vim editor
vim The editor confirms that it is not installed
[root@69f501e858a6 /]# vim bash: vim: command not found
Install it
[root@69f501e858a6 /]# yum install vim -y
(3) Save it as a new image
First check the currently running image
[root@ken1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69f501e858a6 centos "/bin/bash" 2 minutes ago Up 2 minutes quizzical_torvalds
Use commit Save as a new image
[root@ken1 ~]# docker commit 69f501e858a6 centos-vim sha256:42083b89a179368bc29a8f40d14f8824990183c8e4b28fd84411d440c26342e5
69f501e858a6 is the ID of the running container. You can also use the name below name.
centos-vim is the name of the new image.
Check to see if centos-vim is available. Mirror
Restart the new mirror and verify whether the vim editor can be used
You can find that the new mirror can be used with the vim editor
[root@ken1 ~]# docker run -it centos-vim [root@61d090898bad /]# vim [root@61d090898bad /]# vim test
The above demonstrates how to use commit to create a new image, but docker does not recommend using this method to create an image for the following reasons:
Dockerfile is a file composed of a set of instructions. Each instruction corresponds to a command in Linux. The Docker program reads the Dockerfile The instructions in ultimately generate the image.
Step one: Create a new directory
[root@ken1 ~]# mkdir /test
Step two: Write a Dockerfile
The name is Dockerfile, and The first D needs to be capitalized
[root@ken1 ~]# cat Dockerfile FROM centos RUN yum install vim -y
FROMcentos means using the centos basic image
RUN means installing the vim editor on centos
Step 3: Build the image
[root@ken1 ~]# docker build -t centos-vim2 .
-t is followed by specifying the tag name (tag) of the new image
. The last dot indicates that the docker context is the current directory. Docker will search for the Dockerfile file from the build context by default. We can also specify the location of the Dockerfile through the -f parameter
Step 4: View the image
容器启动时运行指定的命令
dockerfile中可以多个CMD指令,但是只要最后一个生效。CMD可以被docker run之后的参数替换
设置容器启东市的命令
dockerfile中可以有多个ENTRYPOINT,但是只有最后一个生效。
CMD或者docker run之后的参数会被当做参数传递给ENTERYPOINT.
下面演示一个比较全面的dockerfile
[root@ken1 test]# cat Dockerfile #my Dockerfile FROM busybox MAINTAINER ken WORKDIR /ken RUN touch test COPY ["ken1","."] ADD ["wordpress.tar.gz","."] ENV name "ken"
注意:Dockerfile支持以#开头的注释
构建镜像
[root@ken1 test]# docker build -t myimage . Sending build context to Docker daemon 4.281MB Step 1/7 : FROM busybox ---> 3a093384ac30 Step 2/7 : MAINTAINER ken ---> Running in 2a73a83507ce Removing intermediate container 2a73a83507ce ---> 8c3df9b3d823 Step 3/7 : WORKDIR /ken ---> Running in 31c6f9fe2195 Removing intermediate container 31c6f9fe2195 ---> a458cf986072 Step 4/7 : RUN touch test ---> Running in e1b08ebd363c Removing intermediate container e1b08ebd363c ---> 41601920009a Step 5/7 : COPY ["ken1","."] ---> 2ebfa0933fca Step 6/7 : ADD ["wordpress.tar.gz","."] ---> d0ad29d3aa34 Step 7/7 : ENV name "ken" ---> Running in fceae6e20e63 Removing intermediate container fceae6e20e63 ---> 7efe0600e48f Successfully built 7efe0600e48f Successfully tagged myimage:latest
查看镜像
运行该镜像
[root@ken1 test]# docker run -it myimage /ken # ls ken1 test wordpress /ken # echo $name ken
推荐学习:《docker视频教程》
The above is the detailed content of There are several ways to create a Docker image. For more information, please follow other related articles on the PHP Chinese website!