Persisting the writable CoW layer(s)

At some point or another, you might want to save the writable container layer to use as a regular image later. While this type of image splicing is highly discouraged, and I would tend to mostly agree, you may find times where it could provides you with an invaluable debugging tooling when you are unable to investigate the container code in other ways. To create an image from an existing container, there is the docker commit command:

$ docker commit --help

Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
-a, --author string Author (e.g., "John Hannibal Smith <[email protected]>")
-c, --change list Apply Dockerfile instruction to the created image
--help Print usage
-m, --message string Commit message
-p, --pause Pause container during commit (default true)

As you can see, we just need some basic information, and Docker will take care of the rest. How about we try this out on our own:

$ # Run a new NGINX container and add a new file to it
$ docker run -d nginx:latest
2020a3b1c0fdb83c1f70c13c192eae25e78ca8288c441d753d5b42461727fa78
$ docker exec -it
2020a3b1
/bin/bash -c "/bin/echo test > /root/testfile"


$ # Make sure that the file is in /root
$ docker exec -it
2020a3b1
/bin/ls /root

testfile

$ # Check what this container's base image is so that we can see changes
$ docker inspect 2020a3b1 | grep Image
"Image": "sha256:b8efb18f159bd948486f18bd8940b56fd2298b438229f5bd2bcf4cedcf037448",
"Image": "nginx:latest",

$ # Commit our changes to a new image called "new_nginx_image"
$ docker commit -a "Author Name <[email protected]>"
-m "Added a test file"
2020a3b1 new_nginx_image

sha256:fda147bfb46277e55d9edf090c5a4afa76bc4ca348e446ca980795ad4160fc11

$ # Clean up our original container
$ docker stop 2020a3b1 && docker rm 2020a3b1
2020a3b1
2020a3b1

$ # Run this new image that includes the custom file
$ docker run -d new_nginx_image
16c5835eef14090e058524c18c9cb55f489976605f3d8c41c505babba660952d

$ # Verify that the file is there
$ docker exec -it
16c5835e
/bin/ls /root

testfile

$ # What about the content?
$ docker exec -it
16c5835e
/bin/cat /root/testfile

test

$ See what the new container's image is recorded as
$ docker inspect 16c5835e | grep Image

"Image": "sha256:fda147bfb46277e55d9edf090c5a4afa76bc4ca348e446ca980795ad4160fc11",
"Image": "new_nginx_image",

$ # Clean up
$ docker stop 16c5835e && docker rm 16c5835e
16c5835e
16c5835e
The docker commit -c switch is very useful and adds a command to the image just like the Dockerfile would and accepts the same directives that the Dockerfile does, but since this form is so rarely used, we have decided to skip it. If you would like to know more about this particular form and/or more about docker commit, feel free to explore https://docs.docker.com/engine/reference/commandline/commit/#commit-a-container-with-new-configurations at leisure.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset