我们可以在通过 dockerfile 构建 docker 映像的同时通过 cmd 行传递 ENV 变量吗? [英] Can we pass ENV variables through cmd line while building a docker image through dockerfile?

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

问题描述

我正在处理一项任务,该任务涉及使用 Dockerfile 构建一个以 centOs 为基础的 docker 映像.dockerfile 中的步骤之一需要设置 http_proxyhttps_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.

由于这个 Dockerfile 将被具有不同代理的多个团队使用,我想避免必须为每个团队编辑 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?

推荐答案

可以使用 build arguments(在 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-compose 构建容器,则可以 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 构建 docker 映像的同时通过 cmd 行传递 ENV 变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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