Please indicate the source of the reprint: http://blog.csdn.net/zhaoyanjun6/article/details/130338433
This article comes from [Zhao Yanjun's blog]
Article directory
Note: docker commit
In addition to learning commands, there are also some special applications, such as saving the scene after being invaded. However, don't use docker commit
custom images, custom images should be Dockerfile
done using . See the next section if you want to customize the image.
The image is the basis of the container, which image will be specified as the basis for the container to run each time it docker run
is . In the previous examples, we used images from Docker Hub. Direct use of these images can meet certain requirements, and when these images cannot directly meet the requirements, we need to customize these images. The next few sections will explain how to customize the image.
Looking back at the knowledge we have learned before, mirroring is a multi-layer storage, and each layer is modified on the basis of the previous layer; and containers are also multi-layer storage, which is based on mirroring. Add a layer as the storage layer for the container runtime.
Now let's take customizing a web server as an example to explain how the image is built.
$ docker run --name webserver -d -p 80:80 nginx
This command will use nginx
the image to start a container, named it webserver
, and mapped port 80, so that we can use the browser to access the nginx server.
If it is running on the local machine Docker
, you can directly access: http://localhost
, if you installed Docker on a virtual machine or cloud server, you need to replace localhost with the address of the virtual machine or the actual cloud server.
If we visit directly with a browser, we will see the default Nginx welcome page.
Now, suppose we don't like this welcome page very much, and we want to change it to welcome Docker text, we can use docker exec
the command to enter the container and modify its content.
$ docker exec -it webserver bash
root@3729b97e8226:/# echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
root@3729b97e8226:/# exit
exit
We enter the webserver container in an interactive terminal mode, and execute the bash command, that is, obtain an operational Shell.
Then, we <h1>Hello, Docker!</h1>
overwrite /usr/share/nginx/html/index.html
the content of with .
Now if we refresh the browser again, we will find that the content has changed.
We modified the file of the container, that is, changed the storage layer of the container. We can see the specific changes through docker diff
the command .
$ docker diff webserver
C /root
A /root/.bash_history
C /run
C /usr
C /usr/share
C /usr/share/nginx
C /usr/share/nginx/html
C /usr/share/nginx/html/index.html
C /var
C /var/cache
C /var/cache/nginx
A /var/cache/nginx/client_temp
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp
Now that we have customized our changes, we want to save them as a mirror image.
Be aware that when we run a container (if we don't use volumes), any file modifications we make will be recorded in the container storage layer. And Docker provides a docker commit
command to save the storage layer of the container as a mirror image. In other words, on the basis of the original image, the storage layer of the container is added to form a new image. When we run this new image in the future, we will have the last file changes of the original container.
The syntax format of docker commit is:
docker commit [选项] <容器ID或容器名> [<仓库名>[:<标签>]]
We can save the container as an image with the following command:
$ docker commit \
--author "Tao Wang <[email protected]>" \
--message "修改了默认网页" \
webserver \
nginx:v2
sha256:07e33465974800ce65751acc279adc6ed2dc5ed4e0838f8b86f0c87aa1795214
Among them --author
is the author of the specified modification, --message
and is to record the content of this modification. This is similar to git
version control, but here these information can be omitted and left blank.
We can see this newly customized image docker image ls
in :
$ docker image ls nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v2 07e334659748 9 seconds ago 181.5 MB
nginx 1.11 05a60462f8ba 12 days ago 181.5 MB
nginx latest e43d811ce2f4 4 weeks ago 181.5 MB
We can also view the history in the mirror with docker history
specific , if we compare nginx:latest
the history of , we will find that the layer we just submitted has been added.
$ docker history nginx:v2
IMAGE CREATED CREATED BY SIZE COMMENT
07e334659748 54 seconds ago nginx -g daemon off; 95 B 修改了默认网页
e43d811ce2f4 4 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) EXPOSE 443/tcp 80/tcp 0 B
<missing> 4 weeks ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx/ 22 B
<missing> 4 weeks ago /bin/sh -c apt-key adv --keyserver hkp://pgp. 58.46 MB
<missing> 4 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.11.5-1 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) MAINTAINER NGINX Docker Ma 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) ADD file:23aa4f893e3288698c 123 MB
After the new image is customized, we can run the image.
docker run --name web2 -d -p 81:80 nginx:v2
Here we name the new service as web2
and map to 81
the port . Visit http://localhost:81
to see the result, its content should be webserver
the same as the previously modified .
So far, we have completed the custom image for the first time, using docker commit
the command manually add a new layer to the old image to form a new image, and we should have a more intuitive feeling about the multi-layer storage of the image.
Use docker commits with caution
Although using docker commit
the command can be more intuitive to help understand the concept of mirrored hierarchical storage, it is not used in the actual environment.
First of all, if you carefully observe the docker diff webserver
previous results, you will find /usr/share/nginx/html/index.html
that in addition to the files you really want to modify, there are many files that have been changed or added due to the execution of the command. This is just the simplest operation. If you install software packages and compile and build, a lot of irrelevant content will be added, which will cause the image to be extremely bloated.
In addition, using docker commit
means that all operations on the image are black-box operations, and the generated image is also called a black-box image. In other words, except for the person who made the image who knows what commands have been executed and how to generate the image, no one else can get it at all. Know. Moreover, even the person who made the mirror image cannot remember the specific operation after a period of time. The maintenance of this black box image is very painful.
Moreover, looking back at the concept of hierarchical storage used in the previously mentioned mirroring, except for the current layer, each previous layer will not change. In other words, the result of any modification is only marked on the current layer , add, modify without changing the previous layer. If you use to docker commit
make a mirror image and modify it later, each modification will make the mirror image more bloated, and the deleted upper layer will not be lost, and will always follow the mirror image, even if it cannot be accessed at all. This will make the image more bloated.