brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker commit turns a container into an image, and --change edits its metadata

Docker
~/tips/tip-docker-commit-change

docker commit <container> myname/testimage:version3

Saves the current state of a container as a new image, with optional Dockerfile-style changes.

A Dockerfile is the proper way to build an image, and I’d always pick it. But docker commit exists, and it is handy for a one-off snapshot of a container you poked around in.

docker commit <container> myname/testimage:version3

That creates a new image from the container’s filesystem changes. The docs give a second form that edits the image configuration at the same time:

docker commit --change "ENV DEBUG=true" <container> myname/testimage:version3
docker commit --change='CMD ["apachectl", "-DFOREGROUND"]' -c "EXPOSE 80" <container> myname/testimage:version4

--change (short -c) applies Dockerfile instructions to the new image. The docs list CMD, ENTRYPOINT, ENV, EXPOSE, LABEL, ONBUILD, USER, VOLUME and WORKDIR. You can add -m for a commit message and -a for an author.

Two things to know. First, by default the container and its processes are paused while the image is committed. If that is a problem, --no-pause turns it off. Second, the docs state that commits do not include any data in mounted volumes. If your container keeps its data in a volume, the image will not contain it.

Because nothing records how the image was made, I would treat a committed image as a snapshot for debugging or a quick copy, and not as something to share. Anyone who wants to rebuild it later has only the layers, no recipe.

Further reading: docker container commit

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker import /path/to/exampleimage.tgz docker compose events docker compose volumes All Docker tips (192) All topics

Link