I am trying to pass a variable to a simple docker script
I tried the method described here .
FROM golang:latest
ARG buildtime_variable=default_value
ENV env_var_name=$buildtime_variable
RUN echo $env_var_name
I tried building it with
docker build --build-arg buildtime_variable=a_value .
And I get the results
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM golang:latest ---> da66b002dd02
Step 2/4 : ARG buildtime_variable=default_value ---> Running in 91055d467539 Removing intermediate container 91055d467539 ---> 1241ad5c9f12
Step 3/4 : ENV env_var_name=$buildtime_variable ---> Running in c61292041ccf Removing intermediate container c61292041ccf ---> 4eeac4402f5b
Step 4/4 : RUN echo $env_var_name ---> Running in 80ba16d2ee9c Removing intermediate container 80ba16d2ee9c ---> b814420cc448 Successfully built b814420cc448
I was expecting it to echo a_value, instead it does no such thing, its like the variable didn't get correctly transmitted.
I tried it with RUN echo A
which produced the expected result of echoing A
Is it time to use an env-file? or do I not need to resort to that?
EDIT
Just did an update, my version info is
docker version Client: Docker Engine - Community Version:
18.09.2 API version: 1.39 Go version: go1.10.8 Git commit: 6247962 Built: Sun Feb 10 04:12:31 2019 OS/Arch: windows/amd64 Experimental: falseServer: Docker Engine - Community Engine: Version: 18.09.2 API version: 1.39 (minimum version 1.24) Go version:
go1.10.6 Git commit: 6247962 Built: Sun Feb 10 04:28:48 2019 OS/Arch: windows/amd64 Experimental:
false
EDIT
I just tried switching to Linux containers and trying an example from here
FROM ubuntu
ARG CONT_IMG_VER
ENV CONT_IMG_VER v1.0.0
RUN echo $CONT_IMG_VER
docker build --build-arg CONT_IMG_VER=v2.0.1 .
This actually prints out a value!
unfortunately this is the default value v1.0.0
instead of the variable defined by --build-arg
Also, I need this to work with a windows container.
This is not addressing the actual question, but I feel it solves the problem using the recommended way of dockerising .
As I understand your need, you want to specify an environment variable before building the container and want this varible to be there whenever you start the container.
You can do this using docker-compose. There we can specify an .env file containing all the environment variables we want.
Create a docker-compose.yml
file:
version: '3'
services:
app:
image: golang:latest
container_name: golang_app
env_file:
- variables.env
command: ping -t localhost
Create a variables.env
file in the same folder with contents:
env_var_name=a_value
You can run the container as docker-compose up
.
Now if you go inside the container using docker exec -it golang_app cmd
and
type
. echo $env_var_name
you should see
a_value
Edit : On windows env variables are accessed as %var%. So try echo %env_var_name%
inside the container.
SET
gives the list of all env variables. You can see that env_var_name
is listed with the value of a_value
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.