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)
May be you the "environment" option solves your problem. In your docker compose file would looks like:
docker-compose.yml
Dockerfile
Maybe you can try this:
Before you call
RUN
,ADD
the .env file into the imagethen prefix your RUN statement:
This produces the following output:
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 thebuild:
section, which includes that very interesting possibility: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:Before calling docker-compose up, you need to make sure your proxy environment variables are set:
Then your
Dockerfile
built by thedocker-compose
process will pick up automatically the proxy variable values, even though thedocker-compose.yml
does not include any hard-coded specific values.This example fixes YUM.