skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search