Florian
Florian

Reputation: 25415

Difference in environment when using 'docker build' or 'docker run'

I am starting to learn Docker, and I ran into some behavior I could not understand.

I want to use conda inside my Docker container, but when using docker build . I ran into the error /bin/sh: 1: conda: not found. After some investigation, I think I found the problem; conda is not listed in the environment variable PATH. My simplified Dockerfile:

FROM continuumio/anaconda3
RUN env
RUN conda info

The resulting environment variables do not show any reference to conda. However, when I use docker run -it continuumio/anaconda3, and then run the command env there, my PATH does contain references to conda.

I think solving or understanding the discrepancy between the behavior of the docker run and docker build commands will (help me) solve my initial problem. Does anyone know what causes the difference? Thanks in advance.

Upvotes: 1

Views: 207

Answers (1)

astef
astef

Reputation: 9508

Look at Dockerfile:

There's a line:

echo "conda activate base" >> ~/.bashrc

I believe it is registering the environment variables.

When you run container conda activate base command is executed, as a part of .bashrc script. Here's why: https://unix.stackexchange.com/questions/129143/what-is-the-purpose-of-bashrc-and-how-does-it-work

On build stage the only thing happens is echo command writes to .bashrc file.

But you must not understand build stage as a build on your machine. The base image was built once by someone else ("upstream build") and you only fetching a resulting filesystem to run your build over it ("downstream build").

That's why you can't rely on ENV declaration in base Dockerfile.

There's a technique how the upstream build can affect the dowstream build with ONBUILD directive, but it's not the case for your images.

It's hard to answer why the resulting filesystem for anaconda3 doesn't contain some environment variables defined, and why miniconda has them. This images are very different, and diagnosing a build stage scripts is not easy at all.

Upvotes: 1

Related Questions