0

I'm working on dockerizing a conda environment so I've created a toy example. My main goal is to end up with the following:

  • A docker image
  • A docker container that automatically loads the conda environment
  • A docker container that can accept arguments for executables in the conda environment (e.g., docker run [some arguments] "seqkit -h"
  • A docker container that will be interactive if no arguments are given but will automatically be in the conda environment.

For simplicity, I've created a docker container that installs seqkit through conda.

Here is my Dockerfile:

FROM continuumio/miniconda3

ARG ENV_NAME

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

WORKDIR /root/

# Install Miniconda
RUN /opt/conda/bin/conda init bash && \
    /opt/conda/bin/conda config --add channels jolespin && \
    /opt/conda/bin/conda config --add channels bioconda && \
    /opt/conda/bin/conda config --add channels conda-forge && \
    /opt/conda/bin/conda update conda -y && \
    /opt/conda/bin/conda clean -afy

# =================================

# Add conda bin to path
ENV PATH /opt/conda/bin:$PATH

# Create environment
RUN conda create -n ${ENV_NAME} -c bioconda seqkit -y

# Activate environment
RUN conda activate ${ENV_NAME}

# ENTRYPOINT ["/bin/bash", "-c", "source activate ${ENV_NAME} && exec /bin/bash"]

Here is my command to build the docker image:

docker build --build-arg ENV_NAME=test_env -t test -f Dockerfile-test .

Issues:

1. When I run the container, it does not activate the test_env environment automatically. How can I make it so when I run my docker image to build a container it automatically activates test_env regardless of the commands given?

(base) jespinozlt2-osx:docker jespinoz$ docker run -it test bash
(base) root@19dcf0f9570f:~# echo $CONDA_PREFIX
/opt/conda
(base) root@19dcf0f9570f:~# conda activate test_env
(test_env) root@19dcf0f9570f:~# echo $CONDA_PREFIX
/opt/conda/envs/test_env

2. How am I able to give it commands once the environment is activated? My test command I want to run is just seqkit -h

O.rka
  • 29,847
  • 68
  • 194
  • 309

2 Answers2

0

I figured it out:

# =================================
# Miniconda3
# =================================

FROM continuumio/miniconda3

ARG ENV_NAME

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

WORKDIR /root/

# Install Miniconda
RUN /opt/conda/bin/conda init bash && \
    /opt/conda/bin/conda config --add channels jolespin && \
    /opt/conda/bin/conda config --add channels bioconda && \
    /opt/conda/bin/conda config --add channels conda-forge && \
    /opt/conda/bin/conda update conda -y && \
    /opt/conda/bin/conda install -c conda-forge mamba -y && \
    /opt/conda/bin/conda clean -afy

# =================================

# Add conda bin to path
ENV PATH /opt/conda/bin:$PATH

# Create environment
RUN mamba create -n ${ENV_NAME} -c bioconda seqkit -y

# Set up environment
RUN echo "conda activate ${ENV_NAME}" >> ~/.bashrc

# Set entrypoint to bash
ENTRYPOINT ["bash", "-l", "-c"]

Here's how you activate it interactively:

docker run -it test  -c "bash"

Here's how you run commands:

docker run -it test  -c "seqkit -h"
O.rka
  • 29,847
  • 68
  • 194
  • 309
0

You could add

CMD ['conda', 'activate', '${ENV_NAME}']

at the end of the Dockerfile.

You can look at the difference between RUN and CMD commands here

  • From my (limited) understanding of docker is that any commands given will override this. – O.rka May 16 '23 at 19:09