logo
down
shadow

Create dynamic environment variables at build time in Docker


Create dynamic environment variables at build time in Docker

By : 김진훈
Date : November 22 2020, 10:31 AM
To fix this issue Docker v1.9 or newer
If you are using Docker v1.9 or newer, this is possible via support for build time arguments. Arguments are declared in the Dockerfile by using the ARG statement.
code :
ARG REQUIRED_ARGUMENT
ARG OPTIONAL_ARGUMENT=default_value
$ docker build --build-arg REQUIRED_ARGUMENT=this-is-required .


Share : facebook icon twitter icon
How do I set environment variables during the build in docker

How do I set environment variables during the build in docker


By : Henry Tabima Giraldo
Date : March 29 2020, 07:55 AM
Hope this helps ARG is for setting environment variables which are used during the docker build process - they are not present in the final image, which is why you don't see them when you use docker run.
You use ARG for settings that are only relevant when the image is being built, and aren't needed by containers which you run from the image. You can use ENV for evnvironment variables to use during the build and in containers.
code :
FROM ubuntu                                                                                                            
ARG BUILD_TIME=abc                                                                                                     
ENV RUN_TIME=123                                                                                                       
RUN touch /env.txt                                                                                                     
RUN printenv > /env.txt 
> docker run temp cat /env.txt                                                                                         
HOSTNAME=b18b9cafe0e0                                                                                                  
RUN_TIME=123                                                                                                           
HOME=/root                                                                                                             
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin                                                      
BUILD_TIME=def                                                                                                         
PWD=/ 
Build the docker image with environment variables

Build the docker image with environment variables


By : rkohardy
Date : March 29 2020, 07:55 AM
wish help you to fix your issue First you need to consume the build-arg inside you dockerfile using the ARG command.
code :
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
Passing environment variable to docker image at build time with docker-compose

Passing environment variable to docker image at build time with docker-compose


By : user1459385
Date : March 29 2020, 07:55 AM
around this issue Your example is working for me.
Have you tried deleting the images and building again? Docker won't re-build your image despite environment variables changed if the image is in cache.
code :
 docker-compose down --rmi all
$ cat Dockerfile
FROM alpine
ARG BUILD_VERSION
ENV BUILD_VERSION=$BUILD_VERSION
RUN echo Build Time: $BUILD_VERSION

$ cat docker-compose.yml
version: '3'
services:
  app:
    build:
      context: .
      args:
        - BUILD_VERSION
$ export BUILD_VERSION=122221
$ docker-compose up --build
Creating network "a_default" with the default driver
Building app
Step 1/4 : FROM alpine
latest: Pulling from library/alpine
8e3ba11ec2a2: Pull complete
Digest: sha256:7043076348bf5040220df6ad703798fd8593a0918d06d3ce30c6c93be117e430
Status: Downloaded newer image for alpine:latest
---> 11cd0b38bc3c
Step 2/4 : ARG BUILD_VERSION
---> Running in b0a1a79967a0
Removing intermediate container b0a1a79967a0
---> 9fa331d63f6d
Step 3/4 : ENV BUILD_VERSION=$BUILD_VERSION
---> Running in a602c27689a5
Removing intermediate container a602c27689a5
---> bf2181423c93
Step 4/4 : RUN echo Build Time: $BUILD_VERSION                <<<<<< (*)
---> Running in 9d828cefcfab
Build Time: 122221
Removing intermediate container 9d828cefcfab
---> 2b3afa3d348c
Successfully built 2b3afa3d348c
Successfully tagged a_app:latest
Creating a_app_1 ... done
Attaching to a_app_1
a_app_1 exited with code 0
$ cat docker-compose
version: '3'
services:
  app1:
    build:
      context: .
      args:
        - BUILD_VERSION=$APP1_BUILD_VERSION
  app2:
    build:
      context: .
      args:
        - BUILD_VERSION=$APP2_BUILD_VERSION
$ export APP1_BUILD_VERSION=1.1.1
$ export APP2_BUILD_VERSION=2.2.2
$ docker-compose build
Building app1
Step 1/4 : FROM alpine
latest: Pulling from library/alpine
8e3ba11ec2a2: Pull complete
Digest: sha256:7043076348bf5040220df6ad703798fd8593a0918d06d3ce30c6c93be117e430
Status: Downloaded newer image for alpine:latest
---> 11cd0b38bc3c
Step 2/4 : ARG BUILD_VERSION
---> Running in 0b66093bc2ef
Removing intermediate container 0b66093bc2ef
---> 906130ee5da8
Step 3/4 : ENV BUILD_VERSION=$BUILD_VERSION
---> Running in 9d89b48c875d
Removing intermediate container 9d89b48c875d
---> ca2480695149
Step 4/4 : RUN echo Build Time: $BUILD_VERSION
---> Running in 52dec27874ec
Build Time: 1.1.1
Removing intermediate container 52dec27874ec
---> 1b3654924297
Successfully built 1b3654924297
Successfully tagged a_app1:latest
Building app2
Step 1/4 : FROM alpine
---> 11cd0b38bc3c
Step 2/4 : ARG BUILD_VERSION
---> Using cache
---> 906130ee5da8
Step 3/4 : ENV BUILD_VERSION=$BUILD_VERSION
---> Running in d29442339459
Removing intermediate container d29442339459
---> 8b26def5ef3a
Step 4/4 : RUN echo Build Time: $BUILD_VERSION
---> Running in 4b3de2d223e5
Build Time: 2.2.2
Removing intermediate container 4b3de2d223e5
---> 89033b10b61e
Successfully built 89033b10b61e
Successfully tagged a_app2:latest
docker build failed : Not able to set the environment variables

docker build failed : Not able to set the environment variables


By : user1918614
Date : March 29 2020, 07:55 AM
I wish this help you
How to verify that these still exist for the rest of the RUN commands in dockerfile?
code :
RUN ["C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat", "x86_amd64"]
RUN qmake app.pro -spec win32-msvc "CONFIG+=debug" "CONFIG+=qml_debug"
qmake app.pro -spec win32-msvc "CONFIG+=debug" "CONFIG+=qml_debug"
C:\\Qt\\Tools\\QtCreator\\bin\\jom.exe qmake_all
C:\\Qt\\Tools\\QtCreator\\bin\\jom.exe
C:\\Qt\\Tools\\QtCreator\\bin\\jom.exe install
RUN "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat" x86_amd64 && ^
    qmake app.pro -spec win32-msvc "CONFIG+=debug" "CONFIG+=qml_debug"  && \
    C:\\Qt\\Tools\\QtCreator\\bin\\jom.exe qmake_all && \
    C:\\Qt\\Tools\\QtCreator\\bin\\jom.exe && \
    C:\\Qt\\Tools\\QtCreator\\bin\\jom.exe install
How to source a script with environment variables in a docker build process?

How to source a script with environment variables in a docker build process?


By : user2715380
Date : March 29 2020, 07:55 AM
hope this fix your issue Each Dockerfile RUN step runs a new container and a new shell. If you try to set an environment variable in one shell, it will not be visible later on. For example, you might experiment with this Dockerfile:
code :
FROM busybox
ENV FOO=foo1
RUN export FOO=foo2
RUN export BAR=bar
CMD echo FOO is $FOO, BAR is $BAR
# Prints "FOO is foo1, BAR is "
FROM busybox
ENV FOO=foo2
ENV BAR=bar
CMD echo FOO is $FOO, BAR is $BAR
# Prints "FOO is foo2, BAR is bar"
#!/bin/sh
# Read in the file of environment settings
. /opt/wherever/env
# Then run the CMD
exec "$@"
FROM busybox
WORKDIR /app
COPY entrypoint.sh .
COPY more_stuff .
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["/app/more_stuff/my_app"]
Related Posts Related Posts :
  • Docker.IO Filesystem Consistancy
  • Gitlab runner: failure to log in to GitLab Container Registry
  • why docker virtualization is faster vs VM
  • Docker: Caused by: java.nio.file.FileSystemException: ./conf/flow.xml.gz: Resource busy
  • Does Docker use different tools to run containers on different Operating Systems?
  • Kubernetes Setting Up Flannel Pod Network
  • Create Custom Docker Alpine Image
  • Is it possible to create a docker container that contains one or more containers?
  • Editing files from dockerfile
  • What are the different ways of implementing Docker FROM?
  • fig invalid repository name error
  • Why am I getting an RTNETLINK Operation Not Permitted when using Pipework with Docker containers?
  • Specify the order Dockers run on Kubernetes pod
  • How do I reload haproxy.cfg on the default Dockerfile?
  • Can Docker containers (NOT Docker images) be moved?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org