Answers: <p>Better way to do this is to define both envs and <code>env-a</code> and <code>env-b</code> inside the single dockerfile, like so:</p>
  <pre><code>FROM continuumio/miniconda3
  
  WORKDIR /usr/src/app
  
  #Create scripts to run in env-a and env-b (This is for example; pipelines in your case) 
  
  COPY run_env_a.sh /usr/src/app/run_env_a.sh
  COPY run_env_b.sh /usr/src/app/run_env_b.sh
  
  # Make the scripts executable
  RUN chmod +x /usr/src/app/run_env_a.sh /usr/src/app/run_env_b.sh
  
  # Install samtools in env-a
  RUN conda create --name env-a -c bioconda samtools
  
  # Install samtools in env-b
  RUN conda create --name env-b -c bioconda samtools
  
  # Run the script in env-a
  RUN conda run -n env-a /usr/src/app/run_env_a.sh
  
  # Run the script in env-b
  RUN conda run -n env-b /usr/src/app/run_env_b.sh
  
  
  
  # This is optional and can be removed to use base conda env generated from base image
  
  # Set the default shell to use env-a
  SHELL [&quot;conda&quot;, &quot;run&quot;, &quot;-n&quot;, &quot;env-a&quot;, &quot;/bin/bash&quot;, &quot;-c&quot;]
  
  # Set the default command to activate env-a
  CMD [&quot;conda&quot;, &quot;run&quot;, &quot;-n&quot;, &quot;env-a&quot;, &quot;/bin/bash&quot;]
  
  
  # Set the default command to activate env-a (After this command all commands will automatically run in env-a)
  CMD [&quot;conda&quot;, &quot;run&quot;, &quot;-n&quot;, &quot;env-a&quot;, &quot;/bin/bash”]
  
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-05-30T13:54:51.307Z
Answers: 2024-05-30T15:15:45.803Z
Answers: 2024-05-30T15:15:45.803Z
Answers: Aly Hassan
Answers: <p>You're overwriting the contents of <code>/opt/</code> in the container with the contents from <code>/media/data/home/booby/test/</code>. Mount it in some other directory - for example to a subdirectory of <code>/opt</code> as shown below as shown below</p>
  <pre><code>docker run --init -v /media/data/home/booby/test/:/opt/sam /bin/bash -c &quot;source activate env-a &amp;&amp; env view -h /opt/sam/my.bam &gt; /opt/sam/my.sam &amp;&amp; source activate env-b &amp;&amp; samtools view -b /opt/sam/my.sam &gt; /opt/sam/my.bam&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-09-12T20:05:57.510Z
Answers: 2021-09-12T20:05:57.510Z
Answers: Sathyajith Bhat
Body: <p>As a novice to the technology I have only a basic knowledge regarding how to build and deploy a docker so i was hoping someone could provide some help.</p>
  <p>The situation is the following:</p>
  <p>I need to create a docker container with two conda environments and then I want to call a command line from my native os (linux).</p>
  <p>Firs i created two conda envs:</p>
  <pre><code>env-a.yml:
  
  conda create --name env-a
  conda activate env-a
  conda install -c bioconda samtools
  conda env export &gt; env-a.yml
  
  env-b.yml:
  
  conda create --name env-b
  conda activate env-b
  conda install -c bioconda samtools
  conda env export &gt; env-b.yml
  
  </code></pre>
  <p>Next I build my docker container</p>
  <p>Dockerfile:</p>
  <pre><code>    FROM continuumio/miniconda3
       COPY ./env-a.yml /opt/
       COPY ./env-b.yml /opt/
       RUN conda env create -f /opt/env-a.yml
       RUN conda env create -f /opt/env-b.yml
       RUN echo &quot;source activate env-a&quot; &gt; ~/.bashrc
       ENV PATH /opt/conda/envs/env/bin:$PATH
  </code></pre>
  <p>my build :</p>
  <pre><code>docker build -t test ./
  </code></pre>
  <p>and now i run it</p>
  <pre><code>docker run -it test
  (env-a) root@1c44fcc2e83d:/#
  </code></pre>
  <p>looks good…
  now i want to mount a location from which my files are read and execute two pipelines each calling tools from different  environment:</p>
  <pre><code>docker run --init -v /media/data/home/booby/test/:/opt/ test /bin/bash -c &quot;source activate env-a &amp;&amp; env view -h /opt/my.bam &gt; /opt/my.sam &amp;&amp; source activate env-b &amp;&amp; samtools view -b /opt/my.sam &gt; /opt/my.bam&quot;
  </code></pre>
  <p>but when i mount a volume (-v) , it seams I lose access to my conda envs.</p>
  <pre><code>/bin/bash: activate: No such file or directory
  </code></pre>
  <p>what would be the proper way to set this up?</p>
  <p>Thank you</p>
  
Tags: <docker><dockerfile>
Title: Building a docker container with multiple conda envs
UserDisplayName: bboy
ThreadText: # Building a docker container with multiple conda envs
  
  (Asked by: username_0 on 2021-09-11)
  
  As a novice to the technology I have only a basic knowledge regarding how to build and deploy a docker so i was hoping someone could provide some help.
  
  The situation is the following:
  
  I need to create a docker container with two conda environments and then I want to call a command line from my native os (linux).
  
  Firs i created two conda envs:
  
  ```
  env-a.yml:
  
  conda create --name env-a
  conda activate env-a
  conda install -c bioconda samtools
  conda env export > env-a.yml
  
  env-b.yml:
  
  conda create --name env-b
  conda activate env-b
  conda install -c bioconda samtools
  conda env export > env-b.yml
  
  ```
  
  Next I build my docker container
  
  Dockerfile:
  
  ```
      FROM continuumio/miniconda3
       COPY ./env-a.yml /opt/
       COPY ./env-b.yml /opt/
       RUN conda env create -f /opt/env-a.yml
       RUN conda env create -f /opt/env-b.yml
       RUN echo "source activate env-a" > ~/.bashrc
       ENV PATH /opt/conda/envs/env/bin:$PATH
  
  ```
  
  my build :
  
  ```
  docker build -t test ./
  
  ```
  
  and now i run it
  
  ```
  docker run -it test
  (env-a) root@1c44fcc2e83d:/#
  
  ```
  
  looks good…
  now i want to mount a location from which my files are read and execute two pipelines each calling tools from different environment:
  
  ```
  docker run --init -v /media/data/home/booby/test/:/opt/ test /bin/bash -c "source activate env-a && env view -h /opt/my.bam > /opt/my.sam && source activate env-b && samtools view -b /opt/my.sam > /opt/my.bam"
  
  ```
  
  but when i mount a volume (-v) , it seams I lose access to my conda envs.
  
  ```
  /bin/bash: activate: No such file or directory
  
  ```
  
  what would be the proper way to set this up?
  
  Thank you
  
  **Tags:** docker, dockerfile
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2024-05-30. Score: 1
  
  Better way to do this is to define both envs and `env-a` and `env-b` inside the single dockerfile, like so:
  
  ```
  FROM continuumio/miniconda3
  
  WORKDIR /usr/src/app
  
  #Create scripts to run in env-a and env-b (This is for example; pipelines in your case) 
  
  COPY run_env_a.sh /usr/src/app/run_env_a.sh
  COPY run_env_b.sh /usr/src/app/run_env_b.sh
  
  # Make the scripts executable
  RUN chmod +x /usr/src/app/run_env_a.sh /usr/src/app/run_env_b.sh
  
  # Install samtools in env-a
  RUN conda create --name env-a -c bioconda samtools
  
  # Install samtools in env-b
  RUN conda create --name env-b -c bioconda samtools
  
  # Run the script in env-a
  RUN conda run -n env-a /usr/src/app/run_env_a.sh
  
  # Run the script in env-b
  RUN conda run -n env-b /usr/src/app/run_env_b.sh
  
  # This is optional and can be removed to use base conda env generated from base image
  
  # Set the default shell to use env-a
  SHELL ["conda", "run", "-n", "env-a", "/bin/bash", "-c"]
  
  # Set the default command to activate env-a
  CMD ["conda", "run", "-n", "env-a", "/bin/bash"]
  
  # Set the default command to activate env-a (After this command all commands will automatically run in env-a)
  CMD ["conda", "run", "-n", "env-a", "/bin/bash”]
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2021-09-12. Score: 0
  
  You're overwriting the contents of `/opt/` in the container with the contents from `/media/data/home/booby/test/`. Mount it in some other directory - for example to a subdirectory of `/opt` as shown below as shown below
  
  ```
  docker run --init -v /media/data/home/booby/test/:/opt/sam /bin/bash -c "source activate env-a && env view -h /opt/sam/my.bam > /opt/sam/my.sam && source activate env-b && samtools view -b /opt/sam/my.sam > /opt/sam/my.bam"
  
  ```
  
  
  ---
  
  