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
2
Answers
I figured it out:
Here's how you activate it interactively:
Here's how you run commands:
You could add
at the end of the Dockerfile.
You can look at the difference between RUN and CMD commands here