【发布时间】:2021-12-13 02:01:24
【问题描述】:
我有一个看起来像这样的 docker-compose,部分是:
nginx:
container_name: ${NGINX_CONTAINER_NAME}
ports:
- ${NGINX_HTTP_PORT1}:${NGINX_HTTP_PORT2}
- ${NGINX_HTTPS_PORT1}:${NGINX_HTTPS_PORT2}
build:
dockerfile: Dockerfile
args:
- NGINX_VERSION=${NGINX_VERSION}
volumes:
- ${NGINX_CONF_DIR:-./nginx}:/etc/nginx/conf.d
- ${NGINX_LOG_DIR:-./logs/nginx}:/var/log/nginx
- ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
depends_on:
- wordpress
restart: always
如您所见,我正在尝试将一个名为 NGINX_VERSION 的变量传递到 Dockerfile 中。
这是 .env 的内容:
# nginx
NGINX_VERSION=1.21.3
NGINX_HTTP_PORT1=8085
NGINX_HTTP_PORT2=8085
NGINX_HTTPS_PORT1=443
NGINX_HTTPS_PORT2=443
NGINX_CONTAINER_NAME=dev_nginx
这就是工作 Dockerfile 的样子:
FROM nginx:1.21.3
RUN apt-get update \
&& apt-get -y install openssl \
&& apt-get -y install vim
RUN mkdir -p /etc/openssl/certs
RUN mkdir -p /etc/nginx/snippets
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/openssl/nginx-selfsigned.key -out /etc/openssl/nginx-selfsigned.crt -subj "/C=US/ST=NY/L=NY/O=ACME/OU=CD/CN=WPDeveloper"
问题
当我将 Dockerfile 中的图像名称更改为如下所示时:
FROM nginx:$NGINX_VERSION
或者这个:
FROM nginx:${NGINX_VERSION}
我收到以下错误:
failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to parse stage name "nginx:": invalid reference format
当我将版本号硬编码在 Dockerfile 中时,一切正常。 另外,如果有帮助,我会通过运行以下命令来验证 env 中的值是否被读取:
docker-compose -f .\docker-compose.yml config
在某种程度上,这就是输出的样子 - 请注意它找到了 NGINX VERSION 的正确值
nginx:
build:
dockerfile: Dockerfile
args:
NGINX_VERSION: 1.21.3
container_name: dev_nginx
depends_on:
wordpress:
condition: service_started
networks:
default: null
ports:
- mode: ingress
target: 8085
published: 8085
- mode: ingress
target: 443
published: 443
protocol: tcp
restart: always
volumes:
source: ./nginx
target: /etc/nginx/conf.d
bind:
create_host_path: true
- type: bind
source: ./logs/nginx
target: /var/log/nginx
bind:
create_host_path: true
- type: bind
source: ./wordpress
target: /var/www/html
bind:
create_host_path: true
任何提示将不胜感激。
【问题讨论】:
-
您是否在
FROM行之前声明了ARG?例如,请参阅Use --build-arg value in Dockerfile FROM parameter。 -
@DavidMaze 这就是我错过的!如果您想添加作为答案,我会接受。
标签: docker docker-compose environment-variables