Jones
Jones

Reputation: 1214

Docker build not setting env variables correctly?

I've got this Dockerfile:

FROM ubuntu:latest

RUN apt-get update && apt-get install -y \
        curl \
        python3-dev \
        python3-setuptools \
        python3-pip

RUN rm -rf /var/lib/apt/lists/* && \
        apt-get clean

ADD . /usr/src/index-server
WORKDIR /usr/src/index-server

RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

EXPOSE 8000

ENV LC_CTYPE=C.UTF-8
ENV FLASK_APP=app_server.py

CMD /usr/local/bin/flask run --host=0.0.0.0

It builds fine. But when I try to run it, I run into a Python error, which I tracked to incorrectly set locale values.

On my host machine locale | grep -E 'LC_ALL|LC_CTYPE|LANG' returns:

LANG="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_ALL=

But when I try the same with the docker instance (docker run -t 657c402ec253 locale | grep -E 'LC_ALL|LC_CTYPE|LANG'), I get this:

LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_ALL=

If I use docker's -e flag to set the env variable, it works fine:

$ docker run -e LC_CTYPE=C.UTF-8 -t 657c402ec253 locale | grep -E 'LC_ALL|LC_CTYPE|LANG'

LANG=
LANGUAGE=
LC_CTYPE=C.UTF-8
LC_ALL=

What's going on?

Upvotes: 0

Views: 1279

Answers (1)

sanath meti
sanath meti

Reputation: 6603

ENV set during the docker build will not be available to containers In-order to persist ENV for future containers you need to use ARG command.

So while build the image you need to pass ARG value which interns pass to ENV value and it will set to containers.

In docker file add this

ARG buildtime_variable=default_value.

ENV env_var_name=$buildtime_variable

And run this command

docker build --build-arg buildtime_variable=a_value # [...]

For detailed info follow below link https://vsupalov.com/docker-build-time-env-values/

Upvotes: 0

Related Questions