nKn
nKn

Reputation: 13761

"env" parameter not applied in container

I'm just testing out Docker so this might be a pretty simple question but I cannot seem to find out why it's not doing what I expect.

I created a pretty simple Dockerfile for testing, just to build a simple image that installs some packages, clones a git repo and build its requirements:

FROM ubuntu:18.04

ENV PYTHONEXEC=python3 \
    PIPEXEC=pip \
    VIRTUALENVEXEC=virtualenv \
    GITREPO=https://github.com/test/test.git \
    REPODIR=test

RUN apt-get update && apt-get install -y git \
    python3 \
    python3-dev \
    python3-virtualenv \
    python-virtualenv \
    qt5-default \
    libcurl4-openssl-dev \
    libxml2 \
    libxml2-dev \
    libxslt1-dev \
    libssl-dev \
    virt-viewer

RUN mkdir -p /app

WORKDIR /app

RUN git clone $GITREPO $REPODIR \
    && $VIRTUALENVEXEC -p $PYTHONEXEC venv \
    && . venv/bin/activate \
    && cd $REPODIR \
    && $PIPEXEC install -r requirements.txt

CMD ["sleep", "1000000"]

Then I build the image with:

docker build -t gitapp:latest .

This works so far. However, if I specify a -e parameter on the docker container run command, it seems not to replace it in the last RUN command.

So if I run docker container run -d -e "REPODIR=blah" gitapp, I expect it to be cloned in /app/blah, but it's still cloned in the /app/test directory.

When I run a docker container exec -it -e "REPODIR=blah" <container-id> env I get:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=2f6ba38341d6
TERM=xterm
REPODIR=blah
PYTHONEXEC=python3
PIPEXEC=pip
VIRTUALENVEXEC=virtualenv
GITREPO=https://github.com/test/test.git
HOME=/root

So it seems that the variable is indeed passed to the container. Then why it isn't passed to the last RUN command so it clones the repo in the right directory? Am I missing something basic here?

Upvotes: 1

Views: 388

Answers (1)

leopal
leopal

Reputation: 4959

When you execute a docker run you are instructing a container to execute Dockerfile's CMD or ENTRYPOINT command. Dockerfile commands that are above entrypoint have been already executed during build and are not executing again at runtime.

That's exactly the reason your github repo is being cloned to the directory defined initially at the Dockerfile and not in the one passed at the run command with -e flag.

A workaround would be to alter your image's entrypoint. You may transfer this part

RUN git clone $GITREPO $REPODIR \
    && $VIRTUALENVEXEC -p $PYTHONEXEC venv \
    && . venv/bin/activate \
    && cd $REPODIR \
    && $PIPEXEC install -r requirements.txt

to a bash script(let's call it my.script.sh) file that will be executed as image's entrypoint. Copy this file during build process in a preferred location, ensuring it holds executable flag and edit your Dockerfile's entrypoint accordingly:

CMD ["/path_to_script/myscript.sh" ]

This however has the caveat that the script will be executed each time the container is started in contrast with your current setup, possibly leading to delay depending on myscript.sh content.

Upvotes: 1

Related Questions