Elykov Alexandr
Elykov Alexandr

Reputation: 85

Can't get envs from inside of docker container

I have alpine based docker with preset env $FOO:

FROM alpine:3.9
ARG FOO
ENV FOO ${FOO}

I pass variable with run command from gitlab ci: docker build --build-arg FOO=$BAR .
Then I try docker run -it container_name echo $BO_USER and get BAR.
But I can't get this env nor from my application running inside nor via interactive shell: docker run -it container_name /bin/sh and echo $FOO
What can be wrong?

Upvotes: 0

Views: 360

Answers (1)

Adiii
Adiii

Reputation: 60074

First thing, there is no ARG FOO in your Dockerfile, it should be

build

export BAR=my_user && docker build --no-cache --build-arg BO_USER=$BAR .

Where $BAR is variable that I assume is set on host

Dockerfile

FROM alpine
ARG BO_USER
ENV BO_USER=${BO_USER}
RUN echo "ENV is $BO_USER"

But you when you run below command, you should look for BO_USER not FOO

docker run -it container_name /bin/sh and echo $BO_USER

or

You should use single quotes otherwise bash will look for variable on host

docker run -it --rm image_name sh -c 'echo $BO_USER'

enter image description here

Upvotes: 1

Related Questions