Can we run a container, install software dependencies, and then use the updated container without building again the image?
1 Answer
You can re-use the container on the system you created it on until you delete the container, even if you exit the container. You can give the container a name at the time you first run it; e.g.:
docker run -it --name my-container ubuntu
After you exit, you can restart and reattach to the container with:
docker start my-container && docker attach my-container
Note that if you docker run
with the --rm
option, the container is deleted when you exit, and so you won't be able to restart it.
If you wish to turn the current state of your container (with all of its installed software dependencies, etc) into a new docker image that you can docker run
or even docker push
to Dockerhub, etc., you can do that with:
docker commit my-container my-image[:<tag>]
At that point,
docker run -it --rm my-image
will create and run a new container that starts from the state your original container was in when it was committed. It's exactly the same as if you had built the new image from a Dockerfile that had run all the commands you issued in the container before it was committed.
Having said all that, it's generally a better idea to build your desired docker image using a Dockerfile, because then the build steps are repeatable, and you can make changes to the build steps (e.g., what prerequisites are needed) without having to start from scratch.