Docker container commit package is getting bigger and bigger
1. Background and reasons
Docker containers are known for their convenience for porting and deployment. Then during the use of docker, repeated packaging and deployment of containers are indispensable.
In order to pursue container integrity, docker commit container ID new image name: tag is often used to operate, and then save/load is used to package/deploy the image.
However, according to the above commands, it is often found that even a small container can get a very large image after several operations of the above steps (even if all the files in the container are deleted).
This is mainly due to:
The construction method of docker images is implemented based on the concept of layers. All operations done in the container will be run once when building the image, and the memory space involved in each operation will become part of the image file.
Most of the things I see are compression container root directory to create basic images or operate Dockerfiles, which is relatively complicated, or not the answer we want.
The following referenceIn the articleMethod 1 gives more detailed and useful solutions.
2. Solution
In order to reduce the content space occupied by the image file, export is used to directly package the container, then import the image through import, and then expand it into a container.
1. You can use the export command to package the container to generate a mirror file.
docker export -o containerID ordocker export containerID >
Note: At this time, you must record the COMMAND content of the current container, and you will use it later.
2. Loading into a mirror
The generated tar image package can be ported and deployed. Suppose that the image is deployed after a machine is replaced, and the commands that can be used to load the image:
docker import containername:tag
3. Expand the image into a container
If the docker run command is used normally, an error will occur:
docker: Error response from daemon: No command specified.
See 'docker run --help'.
This is the importance of COMMAND content that needs to be remembered in step 1.
If the original environment is still there, you can view the COMMAND of your original container through docker ps --no-trunc. Suppose the COMMAND of the original container is "bash".
Append the container COMMAND content when exporting the image using export to the container to be started:
docker run -itd --restart=always --name xxx xxx:latest "bash"
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.