在通过dockerfile建立码头图像时,可以通过cmd行传递ENV变量吗? [英] Can we pass ENV variables through cmd line while building a docker image through dockerfile?

查看:274
本文介绍了在通过dockerfile建立码头图像时,可以通过cmd行传递ENV变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一项任务,涉及使用 Dockerfile ,以 centos 为基础构建码头图像。 docker文件中的一个步骤需要设置 http_proxy https_proxy ENV变量,以便在代理服务器后工作。

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.

由于这个Docker文件将被具有不同代理的多个团队使用,我想避免为每个团队编辑Dockerfile。相反,我正在寻找一个解决方案,允许我在构建时传递ENV变量,例如

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

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+ )它像环境变量一样工作。

Containers can be built using build arguments (in Docker 1.9+) which work like environment variables.

这是一种方法:

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。

对于您的特定问题:

FROM debian
ENV http_proxy ${http_proxy}

然后运行:

docker build --build-arg http_proxy = 10.11.24.31。

请注意,如果您使用 docker-撰写,您可以将这些构建参数指定在 docker-compose.yml 文件,但不在命令行上。但是,您可以在 docker中使用变量替换 - compose.yml 文件,它使用环境变量

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.

这篇关于在通过dockerfile建立码头图像时,可以通过cmd行传递ENV变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆