Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make environmental variables available to Docker RUN commands from docker-compose?

I have a Dockerised application which I would like to run in both proxy and non-proxy host environments. I'm trying to resolve this problem by copying the normal environment variables, such as http_proxy, into the containers if and only if they exist in the host.

I can get 90% of the way there by running

set | grep -i _proxy=>proxies.env

in a top-level script, and then having, in my docker-compose.yml:

myserver:
  build: ./myserver
  env_file:
   - proxies.env

This copies the host's environmental proxy variables, if any, into the server container, and it works in the sense that these variables are available at container run time, in other words by the stage that the Dockerfile CMD or ENTRYPOINT executes.

However I have one container which needs to run npm as a build step, ie from a RUN command in the Dockerfile, and these variables appear not to be present at this stage, so npm can't find the proxy and hangs. In other works, if I have

RUN set

in my Dockerfile, I can't see any variables from proxies.env, but if I do

docker exec -it myserver /bin/bash

and then run set, I can see everything from proxies.env.

Can anyone recommend a way to make these variables visible at container build time, without having to hard-code them, so that my docker-compose.yml and Dockerfile will still work both for hosts with proxies and hosts without proxies?

(Running with centos 7, docker-compose 1.3.1 and docker 1.7.0)

like image 653
Francis Norton Avatar asked Jul 03 '15 15:07

Francis Norton


People also ask

Can you use environment variables in Docker Compose?

In Docker Compose, IT admins can use environment variables to generalize configurations for different situations, deployment environments and security contexts without editing the main project file(s) manually. Variables can either be passed as command-line arguments -- suitable for only a few parameters -- or via a .

How do I pass an environment variable in docker Run command?

Using –env, -e When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e).

How do I set an environment variable in docker run?

Set environment variables (-e, --env, --env-file) You can also load the environment variables from a file. This file should use the syntax <variable>=value (which sets the variable to the given value) or <variable> (which takes the value from the local environment), and # for comments.

How do I override Docker Compose environment variables?

Overriding a single value in your docker-compose . env file is reasonably simple: just set an environment variable with the same name in your shell before running your docker-compose command.


1 Answers

Update 2016, docker-compose 1.6.2, docker 1.10+, with a docker-compose.yml version 2:

You now have the args: sub-section of the build: section, which includes that very interesting possibility:

Build arguments with only a key are resolved to their environment value on the machine Compose is running on.

See PR 2653 (January 2016)

As a result, a way to introduce the proxy variables without hard-coding them in the docker-compose.yml file itself is with that precise syntax:

version: '2'
services:
  myservice:
    build:
      context: .
      args:
        - http_proxy
        - https_proxy
        - no_proxy

Before calling docker-compose up, you need to make sure your proxy environment variables are set:

export http_proxy=http://username:[email protected]:port
export https_proxy=http://username:[email protected]:port
export no_proxy=localhost,127.0.0.1,company.com

docker-compose up

Then your Dockerfile built by the docker-compose process will pick up automatically the proxy variable values, even though the docker-compose.yml does not include any hard-coded specific values.

like image 156
VonC Avatar answered Sep 28 '22 02:09

VonC