Interactive Image Creation
By the end of this exercise, you should be able to:
- Capture a container's filesystem state as a new docker image
- Read and understand the output of
docker container diff
Modifying a Container
Start a bash terminal in a CentOS container:
[centos@node-0 ~]$ docker container run -it centos:7 bashInstall a couple pieces of software in this container - there's nothing special about
wget, any changes to the filesystem will do. Afterwards, exit the container:[root@dfe86ed42be9 /]# yum install -y which wget [root@dfe86ed42be9 /]# exitFinally, try
docker container diffto see what's changed about a container relative to its image; you'll need to get the container ID viadocker container ls -afirst:[centos@node-0 ~]$ docker container ls -a [centos@node-0 ~]$ docker container diff <container ID> C /root A /root/.bash_history C /usr C /usr/bin A /usr/bin/gsoelim ...Those
Cs at the beginning of each line stand for filesChanged, andAforAdded; lines that start withDindicateDeletions.
Capturing Container State as an Image
Installing
whichandwgetin the last step wrote information to the container's read/write layer; now let's save that read/write layer as a new read-only image layer in order to create a new image that reflects our additions, via thedocker container commit:[centos@node-0 ~]$ docker container commit <container ID> myapp:1.0Check that you can see your new image by listing all your images:
[centos@node-0 ~]$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE myapp 1.0 34f97e0b087b 8 seconds ago 300MB centos 7 5182e96772bf 44 hours ago 200MBCreate a container running bash using your new image, and check that vim and wget are installed:
[centos@node-0 ~]$ docker container run -it myapp:1.0 bash [root@2ecb80c76853 /]# which wgetThe
whichcommands should show the path to the specified executable, indicating they have been installed in the image.
Conclusion
In this exercise, you saw how to inspect the contents of a container's read / write layer with docker container diff, and commit those changes to a new image layer with docker container commit. Committing a container as an image in this fashion can be useful when developing an environment inside a container, when you want to capture that environment for reproduction elsewhere.