Dockerfile and environnement variables - docker

I have a strange behavior with my Dockerfile. I try to make it write file with text coming from environment varibales :
FROM ubuntu:14.04
ENV KEY ''
ENV VAL ''
RUN echo "${KEY}:${VAL}" > /etc/test
CMD []
I built this image and run it like this :
docker run -it --rm -e KEY=aaa -e VAL=bbb mytest
If I display the /etc/test file, it is empty (it is present, but empty). It seems that when it creates the file, environment variables are not set.
Any idea?
Thank you

The command in the docker file RUN echo "${KEY}:${VAL}" > /etc/test is executed when you build the image using docker build ...
Thus this is logical, since at that point the env variables are empty.
You need to move the commad to the CMD command which will run when the image is started.

You can define KEY and VALUE as arguments and set their values when you're building the docker image.
FROM ubuntu:14.04
ARG KEY
ARG VAL
RUN echo "${KEY}:${VAL}" > /etc/test
CMD []
Then you can build the image using like this:
docker build --build-arg KEY=<value> --build-arg VAL=<value> .
https://docs.docker.com/engine/reference/builder/#using-arg-variables

Related

Concatenate variables in Dockerfile at runtime

I want to concatenate JSON environment variable out of multiple parameters provided during runtime.
Simplified Dockerfile:
FROM ubuntu:18.04
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS='{"endpointCredentials": [{"endpoint":"'${AZP_ENDPOINT}'", "username":"someuser", "password":"'${AZP_TOKEN}'"}]}'
Then i build image like this:
docker build -t myubuntu .
and run it
docker run -it -e AZP_ENDPOINT="https://pkgs.dev.azure.com/myorg/nuget/v3/index.json" -e AZP_TOKEN="some token" myubuntu
However when i run env command inside the container i see the following picture. Variable provided during container run are there, but VSS_NUGET_EXTERNAL_FEED_ENDPOINTS has not been updated:
AZP_ENDPOINT=https://pkgs.dev.azure.com/myorg/nuget/v3/index.json
AZP_TOKEN=some token
VSS_NUGET_EXTERNAL_FEED_ENDPOINTS={"endpointCredentials": [{"endpoint":"", "username":"someuser", "password":""}]}
Just wondering what i'm doing wrong here?

How to change a docker ARG value inside an if statement?

In my Dockerfile on Ubuntu 16.04 with docker 17.12.1-ce I use
ARG ver=latest
ARG pkg=master
For building a docker container, I would like to call docker build --build-arg ver=v1 . in order to set a special package.
The code-handling part in my Dockerfile is
RUN if[ "x$ver" = "xv1" ] ; then pkg=v1.2.3 ; fi
RUN echo $pkg
Unfortunately, the ARG pkg variable is not updated and the echo statement always shows its initial value.
What can I do, to update my build variable pkg inside an if statement??
You will not be able to pass variables from one RUN to another because each RUN command is executed in a different shell.
A solution to your problem would be to extract the logic into a script and execute docker with the build arguments something like this:
if [ $ver="v1" ]; then pkg=1.2.3; fi; docker build --build-arg ver=$ver --build-arg pkg=$pkg .

How to use environment variable from parent Docker file?

I have two Dockerfiles Dockerfile.A & Dockerfile.B where Dockerfile.B inherits using the FROM keyword from Dockerfile.A. In Dockerfile.A I set an environment variable that I would like to use in Dockerfile.B (PATH). Is this possible, and how would I go about doing it?
So far I have tried the following in Dockerfile.A:
RUN export PATH=/my/new/dir:$PATH
ENV PATH=/my/new/dir:$PATH
RUN echo "PATH=/my/new/dir:$PATH" >/etc/profile
And in Dockerfile.B, respectively:
Just use tools in the path to see if they were available (they were not)
ENV PATH
RUN source /etc/profile
I realized that every RUN command is executed in it's own environment, and that is probably why the ENV keyword exists, to make it possible to treat environments independently of the RUN commands. But I am not sure what that means for my case.
So how can I do this?
Works as expected for me.
Dockerfile.A
FROM alpine:3.6
ENV TEST=VALUE
Build it.
docker build -t imageA .
Dockerfile.B
FROM imageA
CMD echo $TEST
Build it.
$ docker build -t imageB .
Run it
$ docker run -it imageB
VALUE

Build the docker image with environment variables

so the objective is to have a different image for prod and testing so there are certain variables change accordingly so I need to set env variables during the build.
# Dockerfile
ENV Somename: $value
...
docker build --build-arg Somename=value -t test .
docker run -d -p port:port test
this work flow is not taking the env variables
First you need to consume the build-arg inside you dockerfile using the ARG command.
FROM alpine
# consume the build arg
ARG somename
# persist the env variable in the built image
ENV somename=$somename
# somename will appear as an env variable
RUN echo $somename
RUN env
And running the build command docker build --build-arg somename=hello . will show you an env variable somename=hello
your syntax is not correct, do not put
:
it is either
ENV somename somevalue
or
ENV somename=somevalue
Check the doc
https://docs.docker.com/engine/reference/builder/#env

Numeric value not able to set as env using Dockerfile #27284

I'm trying to build a dockerimage from docker file, i'm setting an ENV in the Dockerfile(ENV GOROOT "/usr/share/go/1.6").. But I couldn't find the same ENV value inside the container. The value is set as "/usr/share/go". "/1.6" is missing.
Steps to reproduce the issue:
build the Dockerfile https://gist.github.com/anumantharaja/afa1fc1684a58b2646e2ae2d80489686
Run the image
and inside the container give echo $GOROOT
Result i received:
i received /usr/share/go
Result i expected:
i expected /usr/share/go/1.6
Dockerfile
FROM busybox
### Add Env
ENV GOPATH /root/.gopkg/
ENV GOROOT /usr/share/go/1.6
ENV PATH $PATH:$GOROOT/bin
Build the image
docker build -t rom .
Start a container from the image:
docker run -d -it rom /bin/sh
5dea13c392bcf8740d918be61e8d0c22d20835353a020111f467b928a8990e08
Go inside container
docker exec -it 5dea13c392bcf8740d918be61e8d0c22d20835353a020111f467b928a8990e08 /bin/sh
Echo the env var
/ # echo $GOROOT
/usr/share/go/1.6
This is not an answer. Just to show him that it worked in my case.
Maybe it isn't working with his base image.

Resources