<p>My goal is to have a gitlab CI/CD pipeline that builds my conda packages for me. For very large projects, conda is so slow that gitlab times out, so we are using mamba instead. Gitlab uses a Kubernetes runner, and what I've noticed is that my docker container works fine when I build/run it locally on my machine, but when the Kubernetes executor runs it, the conda environment doesn't have the required packages installed for some reason.</p>
<p>The Docker image gets generated from this Dockerfile:</p>
<pre><code>FROM ubuntu:focal

SHELL [&quot;/bin/bash&quot;, &quot;-l&quot;, &quot;-c&quot;]
RUN apt-get update &amp;&amp; apt-get install -y wget

# Install mamba
RUN wget -q -P /root/ https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh
RUN sh /root/Mambaforge-Linux-x86_64.sh -b
RUN /root/mambaforge/bin/conda shell.bash hook &gt; /root/activatehook.sh

# Create an environment and install curl and numpy
RUN source /root/activatehook.sh &amp;&amp; mamba create -n build-env -y -c conda-forge python=3.9
RUN source /root/activatehook.sh &amp;&amp; conda activate build-env &amp;&amp; mamba install curl numpy
</code></pre>
<p>Now if I build that <em><strong>locally</strong></em>, I can run <code>sudo docker run &lt;my image&gt; /bin/bash -c &quot;source /root/activatehook.sh &amp;&amp; conda activate build-env &amp;&amp; mamba info &amp;&amp; mamba list&quot;</code>, and I see (among other things) that:</p>
<ul>
<li>The active environment is <code>build-env</code></li>
<li><code>curl</code> is installed</li>
<li><code>numpy</code> is installed</li>
</ul>
<p>Now I move that into my gitlab CI script:</p>
<pre class="lang-yaml prettyprint-override"><code>stages:
  - test-stage

test-job:
  stage: test-stage
  tags:
    - kubernetes
  image: &lt;my-image&gt;
  script:
    - /bin/bash -c &quot;source /root/activatehook.sh &amp;&amp; conda activate build-env &amp;&amp; mamba info &amp;&amp; mamba list&quot;
</code></pre>
<p>When this runs, the output from gitlab indicates that:</p>
<ul>
<li>The active environment is <code>build-env</code></li>
<li><code>curl</code> is installed</li>
<li><code>numpy</code> is <em><strong>not</strong></em> installed!</li>
</ul>
<p>I can't figure out where to go with this. The conda environment exists and is active, and one of the packages in it is properly installed, but the other is not. Furthermore, when I pull the image to my local host and run the same command manually, both <code>curl</code> and <code>numpy</code> are installed as expected!</p>
<p>Also important: I am aware of the mambaforge docker image. I have tried something like this:</p>
<pre><code>FROM condaforge/mambaforge

RUN mamba create -y --name build-env python=3.9
RUN mamba install -n build-env -y -c conda-forge curl numpy==1.21
</code></pre>
<p>In this case, I get a similar result, except that, when run from the Kubernetes runner, <em><strong>neither curl nor numpy are installed</strong></em>! If I pull the image to my local host, again, the environment is fine (both packages are correctly installed). Can anyone help explain this behavior?</p>

maldata
<p>The issue is that the &quot;source&quot; command cannot be executed. To resolve this problem, you can use ENTRYPOINT in the Dockerfile. You can verify the presence of the numpy library in the mamba list by following these steps.</p>
<p>Firstly you can create <code>entrypoint.sh</code> file:</p>
<pre><code>#!/bin/bash

source /root/activatehook.sh
conda activate build-env

exec &quot;$@&quot;
</code></pre>
<p>Copy entrypoint file into the container. (I have enriched the content of the Dockerfile)</p>
<p>Dockerfile:</p>
<pre><code>FROM ubuntu:focal

SHELL [&quot;/bin/bash&quot;, &quot;-c&quot;]
WORKDIR /root/

RUN apt-get update &amp;&amp; \
    apt-get install -y wget

RUN wget -q https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh
RUN bash Mambaforge-Linux-x86_64.sh -b -p /root/mambaforge
RUN rm /root/Mambaforge-Linux-x86_64.sh

ENV PATH=&quot;/root/mambaforge/bin:$PATH&quot;

RUN conda shell.bash hook &gt; /root/activatehook.sh

RUN source /root/activatehook.sh &amp;&amp; \
    conda create -n build-env -y -c conda-forge python=3.9 &amp;&amp; \
    echo &quot;source /root/activatehook.sh &amp;&amp; conda activate build-env&quot; &gt;&gt; /root/.bashrc &amp;&amp; \
    /root/mambaforge/bin/conda install -n build-env -c conda-forge -y curl numpy

COPY entrypoint.sh /root/entrypoint.sh
RUN chmod +x /root/entrypoint.sh

RUN apt-get -y autoclean &amp;&amp; \
    apt-get -y autoremove &amp;&amp; \
    rm -rf /var/lib/apt/lists/*

ENTRYPOINT [&quot;/root/entrypoint.sh&quot;]
</code></pre>
<p>gitlab CI script:</p>
<pre><code>stages:
  - test-stage

test-job:
  stage: test-stage
  tags:
    - kubernetes
  image: &lt;your-image&gt;
  script:
    - /root/entrypoint.sh /bin/bash -c &quot;mamba info &amp;&amp; mamba list&quot;
</code></pre>
