Pass host environment variables to dockerfile

28,930

Solution 1

I was experiencing the same issue. My solution was to provide the variable inside of a docker-compose.yml because yml supports the use of environment variables.

In my opinion this is the most efficient way for me because I didn't like typing it over and over again in the command line using something like docker run -e myuser=$USER . . .

Declaring ENV myuser=$USER will NOT work, in the container, $myuser will be set to null.

So your docker-compose.yml could look something like this:

version: '3'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
       - "myuser=${USER}"

and can be run with the short command docker-compose up

To check that the variable has been applied, run docker exec -it container-name printenv to list all variables in the container.

Solution 2

When you start your docker container you can pass environment variables using the -e option like so:

docker run -it <image> -e USER=$USER /bin/bash 

Solution 3

I had a similar use-case where I wanted to be able to specify a version for the image. This has a slight extra requirement that you must specify the ARG before the FROM.

First we specify IMAGE_VERSION in the Dockerfile, as per the question I'm also including a USER arg that we can pass in too:

# give it a default of latest
# declare the ARG before FROM
ARG IMAGE_VERSION=latest
FROM centos:${IMAGE_VERSION}
ARG myuser  
CMD echo $myuser

Then, as from the Docker compose docs on args:

Add build arguments, which are environment variables accessible only during the build process.

You can add these in your docker-compose.yml for example:

version: '3'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        "myuser=${USER}"
        IMAGE_VERSION

Then as long as you have IMAGE_VERSION set in your environment it will be passed through.

Share:
28,930

Related videos on Youtube

Nathan Arthur
Author by

Nathan Arthur

Updated on July 09, 2022

Comments

  • Nathan Arthur
    Nathan Arthur almost 2 years

    How can I pass a host environment variable (like user and hostname) to a dockerfile?

    For example, if my username is taha:

    echo $USER
    taha
    

    How do I write my Docker file to get the same output?

    FROM centos:centos7   
    ARG myuser=$USER  
    CMD echo $myuser
    
  • Admin
    Admin almost 7 years
    I guess I need to use Docker-compose, I m new to this, I try it and tell if it works, hopefully it does :)
  • Admin
    Admin almost 7 years
    FROM centos:centos7 ENV myuser=$USER CMD echo $myuser but it doesn't print the username
  • Ankur Bhatia
    Ankur Bhatia over 5 years
    This won't take the value of $USER from the host machine.
  • hzitoun
    hzitoun over 4 years
    Great, what to do when you ENV var is a path to a very sensitive file in your host machine (don't want to copy the file to the container)? Create a volume to access the file?
  • Joshua Coady
    Joshua Coady over 4 years
    You can simplify that to docker run -it <image> -e USER /bin/bash
  • Joshua Coady
    Joshua Coady over 4 years
    @hzitoun yes, depending on your situation, mount it for access