I am working on a task that involves building a docker image with centOs as its base using a Dockerfile . One of the steps inside the dockerfile needs http_proxy and https_proxy ENV variables to be set in order to work behind the proxy.
As this Dockerfile will be used by multiple teams having different proxies, I want to avoid having to edit the Dockerfile for each team. Instead I am looking for a solution which allows me to pass ENV variables at build time, e.g.,
sudo docker build -e http_proxy=somevalue .
I'm not sure if there is already an option that provides this. Am I missing something?
Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.
Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs. Let's see how we can use it to pass value to our greetings script. There are two different ways to do it.
You can now drop into your Docker image and start interactively running commands! You can keep running these steps, commenting out your Dockerfile, dropping into a shell, and figuring out problematic commands, until your Docker images builds perfectly.
Containers can be built using build arguments
(in Docker 1.9+) which work like environment variables.
Here is the method:
FROM php:7.0-fpm ARG APP_ENV=local ENV APP_ENV ${APP_ENV} RUN cd /usr/local/etc/php && ln -sf php.ini-${APP_ENV} php.ini
and then build a production container:
docker build --build-arg APP_ENV=prod .
For your particular problem:
FROM debian ENV http_proxy ${http_proxy}
and then run:
docker build --build-arg http_proxy=10.11.24.31 .
Note that if you build your containers with docker-compose
, you can specify these build-args in the docker-compose.yml
file, but not on the command-line. However, you can use variable substitution in the docker-compose.yml
file, which uses environment variables.
So I had to hunt this down by trial and error as many people explain that you can pass ARG
-> ENV
but it doesn't always work as it highly matters whether the ARG is defined before or after the FROM
tag.
The below example should explain this clearly. My main problem originally was that all of my ARGS were defined prior to FROM
which resulted all the ENV
to be undefined always.
# ARGS PRIOR TO FROM TAG ARE AVAIL ONLY TO FROM for dynamic a FROM tag ARG NODE_VERSION FROM node:${NODE_VERSION}-alpine # ARGS POST FROM can bond/link args to env to make the containers environment dynamic ARG NPM_AUTH_TOKEN ARG EMAIL ARG NPM_REPO ENV NPM_AUTH_TOKEN ${NPM_AUTH_TOKEN} ENV EMAIL ${EMAIL} ENV NPM_REPO ${NPM_REPO} # for good measure, what do we really have RUN echo NPM_AUTH_TOKEN: $NPM_AUTH_TOKEN && \ echo EMAIL: $EMAIL && \ echo NPM_REPO: $NPM_REPO && \ echo $HI_5 # remember to change HI_5 every build to break `docker build`'s cache if you want to debug the stdout ..... # rest of whatever you want RUN, CMD, ENTRYPOINT etc..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With