Docker Ubuntu container installed software gets removed
You are mistaking a container for a VM. A container is just a network and filesystem bubble tailored to run a specific process. It comes with all the software dependencies and so only requires a Linux kernel.
A Docker image such as Ubuntu is rarely used plain. You tailor it to your needs using a Dockerfile
(see the docker build
command). A Dockerfile is used to:
- install specific software (mostly runtime support) that are useful for the process:
RUN apt-get install python3 python3-pip
,RUN pip install requests numpy
- copy specific files (usually, your own code, plus configuration files for your app and your system (for instance, certificates)):
COPY application.py /app/
. These files are frequently kept together with the Dockerfile. - set the command that is run when the container is launched:
ENTRYPOINT ["/usr/bin/python3","/app/application.py"]
At the end of the docker build
, you have a runnable docker image that can execute your application.
If you tweak a container, you can save its state as an image using docker commit
, but it is considered poor practice, since it doesn't ensure reproducibility: if you need to upgrade the software, you normally rebuild the container using the Dockerfile, so you have best make sure that the Dockerfile and its companion files are all you need to build the final container. This also makes the container more difficult to share, you have to export the whole image instead of just sending the Dockerfile and the code/config.
In serious usage, the Dockerfile and its companion files (configuration...) are versioned in a Git repository, and use the executable files that are produced by the integration chain.
Often, some attempt is often made to keep the container as small as possible. There are Docker images of very small footprint distributions. An Ubuntu is a lot of useless baggage and a Debian will usually do just as well. There are also special-purpose images with preinstalled software (Apache, nginx, MySQL...).
Last you can wonder what happens to files created in the container... by default they are local to the container, and lost if the container instance is removed (docker rm
command). However when launching the container, you can bind some files/directories of the container to files/directories on the host, and so have persistent storage outside the container. You can also use "volume images".
Containers are very useful, but must be used properly...