dockerdockerfileanacondaconda

Dockerfile ENV CC not set in container


I'm working with the following Dockerfile

FROM pangeo/pangeo-notebook:2024.04.08

ENV DEFAULT_CC=/usr/bin/gcc
ENV PATH=/usr/bin:${PATH}:/usr/bin/mpich CC=/usr/bin/gcc

RUN echo 'hello world!!!'

I create an image using the following command

docker build --no-cache --platform linux/amd64 -t app_test .

Doing a docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' app_test on app_test yields the following result

PATH=/usr/bin:/srv/conda/envs/notebook/bin:/srv/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/mpich
CONDA_ENV=notebook
DEBIAN_FRONTEND=noninteractive
NB_USER=jovyan
NB_UID=1000
SHELL=/bin/bash
LANG=C.UTF-8
LC_ALL=C.UTF-8
CONDA_DIR=/srv/conda
NB_PYTHON_PREFIX=/srv/conda/envs/notebook
HOME=/home/jovyan
DASK_ROOT_CONFIG=/srv/conda/etc
TZ=UTC
DEFAULT_CC=/usr/bin/gcc
CC=/usr/bin/gcc

However, after running a container from the image and printing values of CC and PATH variables shows the following

docker run -it app_test /bin/bash

jovyan@ff8afb9c86e7:~$ echo $CC
/srv/conda/envs/notebook/bin/x86_64-conda-linux-gnu-cc
jovyan@ff8afb9c86e7:~$ echo $PATH
/srv/conda/envs/notebook/bin:/srv/conda/condabin:/usr/bin:/srv/conda/envs/notebook/bin:/srv/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/mpich
jovyan@ff8afb9c86e7:~$ echo $DEFAULT_CC 
/usr/bin/gcc

Note /usr/bin/mpich gets appended to PATH but /usr/bin does not get prepended. Can someone explain whether this is something specific to the way pangeo/pangeo-notebook:2024.04.08 is created or my understanding of docker environment variables is wrong?


Solution

  • pangeo auto-activates a conda environment, which overrides the environment variables (including PATH and CC) after docker sets them.

    docker inspect shows you the environment variables in the image before they're being modified via a running process.

    If you need the conda environment but also need to override CC, you can modify the activation scripts.

    something like, eg. override-cc.sh

    #!/usr/bin/env bash
    
    echo "Overriding CC to point to /usr/bin/gcc..."
    export CC=/usr/bin/gcc
    

    You also need to modify your Dockerfile

    FROM pangeo/pangeo-notebook:2024.04.08
    
    RUN mkdir -p /srv/conda/envs/notebook/etc/conda/activate.d
    
    COPY override-cc.sh /srv/conda/envs/notebook/etc/conda/activate.d/
    
    USER root
    
    RUN chmod +x /srv/conda/envs/notebook/etc/conda/activate.d/override-cc.sh
    
    USER jovyan
    

    By default, many jupyter docker images switch from the root user to a less-privileged user (jovyan). If your docker build steps run under that "joyvan" user, you will not be able to modify files in system-owned directories, hence the "USER root"