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

后端 未结 3 1519
梦谈多话
梦谈多话 2020-12-07 13:20

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 docke

3条回答
  •  眼角桃花
    2020-12-07 13:31

    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.

提交回复
热议问题