Make changes to an image
Save the image to a tar file
Stop the container
Prune all data
Then later:
Load the image from the tar file
Run the image in a container
However after docker commit there are 2 images. Should I try to remove the image that existed before docker commit? If so, should I force the removal: docker image -f <image>
There is no such thing as changing an image. An image is immutable. You can only add more layers on top of an existing image. You will still have the original image. Even if you delete that, you just delete the tag on the layer of the original image.
Even if you use “docker commit”, which is a bad idea, you just save the container’s writable filesystem layer to a new image filesystem layer.
If you want to create an image, use a Dockerfile and build the image. You can first run the command sinteractively as you did before, but after that, write your commands in a Dockerfile and run docker build.
Using docker commit you will never be able to reproduce the image and and you won’t be able to update the base image either. If you just run “apt update” and “apt upgrade”, you just add more layers and your image will grow to unnecesserily big forever keeping what was already in the image before.
I am using docker to try to imitate the runtime environment in production. Nobody uses my docker image, other than me. I have been using compose and not build, because I don’t think I am allowed to push to docker hub and I don’t see a reason to install a local registry.
The reason I am asking about removing an image is to save space. it’s not important that commit is adding rather than replacing. I could add to the docker file, instead of modifying the file system within the docker image, but it is convenient to be able to update the filesystem interactively, and then save the result, because I am the only user.
I think after commit save and load I am using one image and there is another image that I’m not using. I thought I should try to remove the image that I am not using, unless I have misunderstood and I need the other image. If I forced removal of the original image would that cause problems for the image that I am using?
I think you got the answers already to the “docker build” vs “docker commit” part, but I would add some notes here and clarify my previous post because your message indicates I was not clear enough.
So if you can delete an image, it will not affect other images. Doesn’t matter if they use common layers. It is always safe to delete images you don’t need, but it is not clear what image you had. I assume an untagged image which only had the repository in its name, so you can delete it by ID. Or if you commited the new image with a different version tag or name, you can use those.
Note that although committing could seem convinient, it can lead to issues you would care about even during development. You cannot have unlimited number of layers, so you would run out of usable layers eventually and you would not have any way to reproduce what you have in the image. You would then need to flatten it, which is possible, but you would not need it if you just used “docker build”.
If you are not comfortable with Dockerfile instructions and you don’t care about learning the right way at the moment, you can create a shell script that installs everything and just execute the shell script in a RUN instruction defined i the Dockerfile..
I never had issues with it, but I think you could also have performnce issues when using too many layers, especially if you need to move files or just write files later that are in the image.
So I don’t see any good reason using docker commit. It just makes things more difficult even if you don’t see it now.
There is a Docker feature that could be used instead of comitting without using docker build, but it is experimental and doesn’t seem to support docker networks, which is probably a bug, but it could be useful in some cases later: docker checkpoint | Docker Docs
But again, you can remove any images. It will not affect others..