docker run interactive with conda environment already activated

Ben Caine

I'd like to create a docker image such that when you run it interactively, a conda environment is already activated.

Current state:

docker run -it my_image
(base) root@1c32ba066db2:~# conda activate my_env
(my_env) root@1c32ba066db2:~#

Desired state:

docker run -it my_image
(my_env) root@1c32ba066db2:~#

More info:

In my Dockerfile, I include all the necessary RUN commands to install conda, create the environment, and activate the environment. Relevant portions reproduced below.

SHELL [ "/bin/bash", "--login", "-c" ]

...

# Install miniconda.
ENV CONDA_DIR $HOME/miniconda3
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
    chmod +x ~/miniconda.sh && \
    ~/miniconda.sh -b -p $CONDA_DIR && \
    rm ~/miniconda.sh
# Make non-activate conda commands available.
ENV PATH=$CONDA_DIR/bin:$PATH
# Make conda activate command available from /bin/bash --login shells.
RUN echo ". $CONDA_DIR/etc/profile.d/conda.sh" >> ~/.profile
# Make conda activate command available from /bin/bash --interative shells.
RUN conda init bash

# Create and activate the environment.
RUN conda env create --force -f environment.yml
RUN conda activate my_env

When I run this, conda activate my_env seems to run and succeed. But when I enter interactively with docker run -it, the activated env is (base).

Additionally, I've tried having the last command be CMD conda activate my_env, but then it just runs that and does not enter interactive mode.

merv

Each RUN statement (including docker run) is executed in a new shell, so one cannot simply activate an environment in a RUN command and expect it to continue being active in subsequent RUN commands.

Instead, you need to activate the environment as part of the shell initialization. The SHELL command has already been changed to include --login, which is great. Now you simply need to add conda activate my_env to .profile or .bashrc:

...
# Create and activate the environment.
RUN conda env create --force -f environment.yml
RUN echo "conda activate my_env" >> ~/.profile

and just be sure this is after the section added by Conda.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

No indication of activated conda environment in terminal

From Dev

cron job to run under conda virtual environment

From Dev

cron job to run under conda virtual environment

From Dev

Using systemd to run airflow inside conda environment

From Dev

Build and run a development environment with Docker

From Dev

Difference between environment in Dockerfile RUN and interactive shell on a running container?

From Dev

Environment variables in docker when exec docker run

From Dev

How can you run QtConsole in a specific Conda environment?

From Dev

How to execute python from conda environment by dvc run

From Dev

How can you run QtConsole in a specific Conda environment?

From Dev

How to filter docker client environment variables into a docker run command?

From Dev

How to filter docker client environment variables into a docker run command?

From Java

How do I run a command on an already existing Docker container?

From Dev

pylint failing in conda environment

From Java

Create empty conda environment

From Java

Use Conda environment in pycharm

From Dev

Create a conda environment offline

From Dev

Use Conda environment in pycharm

From Dev

Conda: Creating a virtual environment

From Dev

conda environment not reading dependencies

From Dev

Create a conda environment offline

From Dev

How can I run runsnakerun on Mac OS X inside a conda environment?

From Dev

Re-activated virtual environment missing module

From Dev

Access environment variables passed in docker run from supervisor child process

From Dev

How can I use environment variables in a docker run command?

From Dev

Access environment variables passed in docker run from supervisor child process

From Dev

Committing an interactive container in docker

From Dev

How would one run windows applications in Linux that are already Installed in a Windows Environment?

From Dev

Fast/interactive development environment for python

Related Related

  1. 1

    No indication of activated conda environment in terminal

  2. 2

    cron job to run under conda virtual environment

  3. 3

    cron job to run under conda virtual environment

  4. 4

    Using systemd to run airflow inside conda environment

  5. 5

    Build and run a development environment with Docker

  6. 6

    Difference between environment in Dockerfile RUN and interactive shell on a running container?

  7. 7

    Environment variables in docker when exec docker run

  8. 8

    How can you run QtConsole in a specific Conda environment?

  9. 9

    How to execute python from conda environment by dvc run

  10. 10

    How can you run QtConsole in a specific Conda environment?

  11. 11

    How to filter docker client environment variables into a docker run command?

  12. 12

    How to filter docker client environment variables into a docker run command?

  13. 13

    How do I run a command on an already existing Docker container?

  14. 14

    pylint failing in conda environment

  15. 15

    Create empty conda environment

  16. 16

    Use Conda environment in pycharm

  17. 17

    Create a conda environment offline

  18. 18

    Use Conda environment in pycharm

  19. 19

    Conda: Creating a virtual environment

  20. 20

    conda environment not reading dependencies

  21. 21

    Create a conda environment offline

  22. 22

    How can I run runsnakerun on Mac OS X inside a conda environment?

  23. 23

    Re-activated virtual environment missing module

  24. 24

    Access environment variables passed in docker run from supervisor child process

  25. 25

    How can I use environment variables in a docker run command?

  26. 26

    Access environment variables passed in docker run from supervisor child process

  27. 27

    Committing an interactive container in docker

  28. 28

    How would one run windows applications in Linux that are already Installed in a Windows Environment?

  29. 29

    Fast/interactive development environment for python

HotTag

Archive