我有一個 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
任何提示將不勝感激。
uj5u.com熱心網友回復:
如果您希望 docker-compose 檔案從 .env 檔案中讀取變數,然后將其傳遞給 Dockerfile,您可以按照以下設定進行操作:
這是您的 .env 檔案包含的內容:
# nginx
NGINX_VERSION=1.21.3
這可以是您的 Dockerfile:
ARG NGINX_VERSION
FROM nginx:${NGINX_VERSION}
...
最后這將是您的 docker-compose.yml 檔案:
nginx:
build:
context: .
dockerfile: Dockerfile
args:
NGINX_VERSION: ${NGINX_VERSION}
...
最后運行:
docker-compose up
確保您的 .env 檔案與 Dockerfile 和 docker-compose.yml 檔案位于同一目錄中。
uj5u.com熱心網友回復:
您已args在 docker-compose 檔案中指定,但我ARG在您的 Dockerfile 中沒有看到相應的內容?您還需要了解ARG 和 FROM 如何互動
這是一個最小的示例,我能夠重新創建您的錯誤,然后ARG在FROM陳述句之前添加
version: "3.8"
services:
test:
build:
context: ./
dockerfile: Dockerfile
args:
- ALPINE_VERSION=3.14
ARG ALPINE_VERSION=3
FROM alpine:${ALPINE_VERSION}
ENTRYPOINT [ "sh" ]
丟失的錯誤 ARG
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 "alpine:": invalid reference format
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/340972.html
