[英]Pass args to the Dockerfile from docker-compose
I have the next configuration of my prod env:我有我的产品环境的下一个配置:
docker-compose-prod.yml docker-compose-prod.yml
version: '3.3'
services:
nginx:
build:
context: nginx
dockerfile: Dockerfile
args:
LANDING_PAGE_DOMAIN: ${LANDING_PAGE_DOMAIN}
container_name: nginx
networks:
- app-network
Dockerfile Dockerfile
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY production/* /etc/nginx/conf.d/
ARG LANDING_PAGE_DOMAIN
RUN sed -i s/{LANDING_PAGE_DOMAIN}/${LANDING_PAGE_DOMAIN}/g /etc/nginx/conf.d/landing.conf
EXPOSE 80 443
So when I try do build it I got next warning:因此,当我尝试构建它时,我收到了下一个警告:
docker-compose -f docker-compose.production.yml build --build-arg LANDING_PAGE_DOMAIN="test.com" nginx
WARNING: The LANDING_PAGE_DOMAIN variable is not set.
警告:未设置 LANDING_PAGE_DOMAIN 变量。 Defaulting to a blank string.
默认为空字符串。
Where I made a mistake?我在哪里做错了?
the key word ARG
has a different scope before and after the FROM
instruction关键字
ARG
在FROM
指令前后有不同的scope
Try using ARG twice in your Dockerfile, and/or you can try the ENV variables尝试在 Dockerfile 中使用 ARG 两次,和/或您可以尝试使用 ENV 变量
ARG LANDING_PAGE_DOMAIN
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY production/* /etc/nginx/conf.d/
ARG LANDING_PAGE_DOMAIN
ENV LANDING_PAGE_DOMAIN=${LANDING_PAGE_DOMAIN}
RUN sed -i s/{LANDING_PAGE_DOMAIN}/${LANDING_PAGE_DOMAIN}/g /etc/nginx/conf.d/landing.conf
EXPOSE 80 443
There are 2 options how to make it work:有两个选项如何使它工作:
Use existing code in docker-compose-prod.yml
and set environment variable LANDING_PAGE_DOMAIN
:使用
docker-compose-prod.yml
中的现有代码并设置环境变量LANDING_PAGE_DOMAIN
:
export LANDING_PAGE_DOMAIN=test.com
then run build step w/o passing the build args:然后在不传递构建参数的情况下运行构建步骤:
docker-compose -f docker-compose.production.yml build nginx
Comment / delete 2 lines from docker-compose-prod.yml
file:从
docker-compose-prod.yml
文件中注释/删除 2 行:
args:
LANDING_PAGE_DOMAIN: ${LANDING_PAGE_DOMAIN}
Then you'll be able to build it with passing arguments at build time:然后你就可以通过在构建时传递 arguments 来构建它:
docker-compose -f docker-compose-prod.yml build --build-arg
LANDING_PAGE_DOMAIN="test.com" nginx
The reason why it currently doesn't work is because those 2 lines in docker-compose-prod.yml
file explicitly sets the LANDING_PAGE_DOMAIN
argument to be populated by ${LANDING_PAGE_DOMAIN}
environment variable.它目前不起作用的原因是因为
docker-compose-prod.yml
文件中的这两行明确地将LANDING_PAGE_DOMAIN
参数设置为由${LANDING_PAGE_DOMAIN}
环境变量填充。
And when you run docker-compose build
with --build-arg
option it doesn't set any env vars but literally passes arguments for build
step.当您使用
--build-arg
选项运行docker-compose build
时,它不会设置任何环境变量,而是通过 arguments 进行build
步骤。
You need to add ARG LANDING_PAGE_DOMAIN=value
to your Dockerfile:您需要将
ARG LANDING_PAGE_DOMAIN=value
添加到 Dockerfile 中:
The ARG instruction lets Dockerfile authors define values that users can set at build-time using the --build-arg flag
ARG 指令允许 Dockerfile 作者定义用户可以在构建时使用 --build-arg 标志设置的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.