I have a docker container (running Ubuntu) with conda (miniconda) installed. I can create new conda environments in the container but they will not persist the next time I create a new container. I don't want to install the packages as part of the Docker image, so what is the best way to make install packages persist in the container? I imagine some directory needs to be mounted on the host so any conda install
command installs packages on a host directory and is there the next time a new container is spawned.

- 1,378
- 2
- 14
- 24
2 Answers
The following two locations need to be mounted from outside of the container to make sure conda environment packages don't get removed between container runs:
1. Create directories for python packages outside of container
mkdir ~/conda_stuff
mkdir ~/conda_stuff/envs
mkdir ~/conda_stuff/pkgs
2. Mount conda env and pkgs directories
# assume conda installed in /opt/anaconda in the container
docker run -it -v ~/conda_stuff/envs:/opt/anaconda/envs \
-v ~/conda_stuff/pkgs:/opt/anaconda/pkgs
Now any packages installed in the conda environment via pip
or conda install
will persist when the container is exited.

- 1,378
- 2
- 14
- 24
The best way actually would be to create your own image with the required conda
environment already pre-installed.
This is because docker
containers are meant to be ephemeral, i.e., stopping and removing a container will also throw away any changed state in it and you should be able to just start a new container from the same image.
To store persistent data you have to use volumes
or bind-mounts
, see the docker
storage docs on this: https://docs.docker.com/storage/
In your case you would have to add one or multiple volumes
to your container and place any changed/installed files there.

- 7,070
- 1
- 18
- 35
-
Right, that's the crux of the question. What do I need to mount? – BigBrownBear00 Nov 23 '22 at 16:51
-
Please look into the `docker` documentation regarding _how_ to use `volumes`: https://docs.docker.com/storage/ Regarding _where_ to use volumes: this depends heavily on your specific case and can therefor not be answered with more additional context on what exactly you are trying to achieve and what you already tried. – acran Nov 23 '22 at 16:56
-
https://stackoverflow.com/questions/31003994/where-is-site-packages-located-in-a-conda-environment – SiHa Nov 23 '22 at 16:57
-
I know how to mount volumes. The question is clear, you don’t understand the use case – BigBrownBear00 Nov 23 '22 at 17:26