Customizing images using docker commit
The general recommendation is that all Docker images should be built from a Dockerfile to create clean and proper image layers without unwanted temporary and log files, despite the fact that some vendors deliver their Docker images without an available Dockerfile . If there is a need to modify that existing image, you can use the standard docker commit functionality to convert an existing container to a new image.
As an example, we will try to modify our existing httpd container and make an image from it.
First, we need to get the httpd image:
$ docker pull httpd
Using default tag: latest
Trying to pull repository docker.io/library/httpd ...
latest: Pulling from docker.io/library/httpd
...
output truncated for brevity
...
Digest: sha256:6e61d60e4142ea44e8e69b22f1e739d89e1dc8a2764182d7eecc83a5bb31181e
Next, we need a container to be running. That container will be used as a template for a future image
$ docker run -d --name httpd httpd
c725209cf0f89612dba981c9bed1f42ac3281f59e5489d41487938aed1e47641
Now we can connect to the container and modify its layers. As an example, we will update index.html:
$ docker exec -it httpd /bin/sh
# echo "This is a custom image" > htdocs/index.html
# exit
Let's see the changes we made using the docker diff command. This command shows you all files that were modified from the original image. The output looks like this:
$ docker diff httpd
C /usr
C /usr/local
C /usr/local/apache2
C /usr/local/apache2/htdocs
C /usr/local/apache2/htdocs/index.html
...
output truncated for brevity
...
In our case, docker diff httpd command shows that index.html was changed.
Create a new image from the running container:
$ docker commit httpd custom_image
sha256:ffd3a523f9848776d65de8302253de9dc78e4948a792569ee46fad5c099312f6
Verify that the new image has been created:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
custom_image latest ffd3a523f984 3 seconds ago 177.4 MB
docker.io/httpd latest 01154c38b473 2 weeks ago 177.4 MB
The final step is to verify that the image works properly:
$ docker run -d --name custom_httpd -p 80:8080 custom_image
78fc5731d62e5a6377a7de152c0ba25d350603e6d97fa26967e06a82c8257e71
$ curl localhost:8080
This is a custom image