Can we pass ENV variables through cmd line while building a docker image through dockerfile?

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.,

  • Splitting up a Rails and React app into separate Docker containers?
  • Starting Xserver in Docker Ubuntu container
  • use docker supervisor automatically run a script after start with a variable given in container start?
  • Incrementally build Docker image hierarchy with Makefile
  • Exposing Docker Container Ports
  • Gitlab CI and Docker throws error when migrating
  • sudo docker build -e http_proxy=somevalue .

    I’m not sure if there is already an option that provides this. Am I missing something?

  • Docker 1.9.0 “bridge” versus a custom bridge network results in difference in hosts file and SSH_CLIENT env variable
  • How to install the latest version of Docker on CentOS 7
  • How to observe scheduled tasks output with Django celery and Docker
  • Does docker CMD carry on from upper images to be passed as argument to ENTRYPOINT
  • Error 500 access in rails app with puma and nginx
  • Managing container dependencies on startup
  • 2 Solutions collect form web for “Can we pass ENV variables through cmd line while building a docker image through dockerfile?”

    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.

    I faced the same situation.

    According to Sin30’s answer pretty solution is using shell,

    CMD ["sh", "-c", "cd /usr/local/etc/php && ln -sf php.ini-$APP_ENV php.ini"]
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.