You need to enable JavaScript to run this app. Docker --build-arg传递版本参数不生效问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Docker --build-arg传递版本参数不生效问题求助

Docker Build Arg未生效:容器显示默认应用版本问题

问题详情

构建Docker镜像时通过--build-arg VERSION=2.0.0指定应用版本,但运行容器后页面显示默认版本production.v1.0,而非传入的2.0.0。

操作步骤:

  • 构建命令:docker build --build-arg VERSION=2.0.0 -f Dockerfile10 -t local/base:v10 .
  • 运行容器:docker run -d -p 8090:8080 --name web10 local/base:v10
  • 访问验证:curl http://localhost:8090,返回的应用版本不符合预期。

涉及文件:

Dockerfile

# Stage 1: Build the application
FROM node:alpine AS builder

# Set the argument for the application version
ARG VERSION

# Install tools and dependencies
RUN apk add --update curl && \
    rm -rf /var/cache/apk/*

# Set the working directory
WORKDIR /usr/app

# Copy application files
COPY ./package.json ./

# Install dependencies
RUN npm install

# Copy application files
COPY ./index.js ./

# Stage 2: Final image
FROM alpine

# Copy files from the build stage
COPY --from=builder /usr/app .

# Set the working directory
WORKDIR /usr/app

# Install Node.js
RUN apk --no-cache add nodejs npm

# Install Express.js
RUN npm install express

# Information about the internal container port on which the application listens
EXPOSE 8080

# Default command to start the container
CMD ["node", "index.js"]

# Set the environment variable for the application version
ENV VERSION=production.${VERSION:-v1.0}

应用代码(index.js)

const express = require('express');
const os = require('os');

const app = express();
const port = 8080;

app.get('/', (req, res) => {
  let response = `Server IP Address: ${getIPAddress()}\n`;
  response += `Server Name (hostname): ${os.hostname()}\n`;
  response += `Application Version: ${process.env.VERSION}\n`;
  res.send(response);
});

app.listen(port, () => {
  console.log(`The application is available on port ${port}`);
});

function getIPAddress() {
  const interfaces = os.networkInterfaces();
  for (const dev in interfaces) {
    const iface = interfaces[dev].filter(details => details.family === 'IPv4' && !details.internal);
    if (iface.length > 0) return iface[0].address;
  }
  return '0.0.0.0';
}

原因分析

Docker的ARG参数是阶段隔离的:在builder阶段定义的ARG VERSION仅在该阶段有效,进入Stage 2(FROM alpine之后)后,之前的ARG参数会丢失。因此,最终阶段的ENV VERSION=production.${VERSION:-v1.0}中,${VERSION}为空,触发默认值v1.0,导致显示production.v1.0。

解决方案

在**最终构建阶段(Stage 2)**重新声明ARG VERSION,让构建时传入的--build-arg值传递到该阶段,从而正确替换ENV中的变量。

修改后的Dockerfile:

# Stage 1: Build the application
FROM node:alpine AS builder

# Set the argument for the application version
ARG VERSION

# Install tools and dependencies
RUN apk add --update curl && \
    rm -rf /var/cache/apk/*

# Set the working directory
WORKDIR /usr/app

# Copy application files
COPY ./package.json ./

# Install dependencies
RUN npm install

# Copy application files
COPY ./index.js ./

# Stage 2: Final image
FROM alpine

# 关键:在当前阶段重新声明ARG VERSION,接收构建时传入的参数
ARG VERSION

# Copy files from the build stage
COPY --from=builder /usr/app .

# Set the working directory
WORKDIR /usr/app

# Install Node.js
RUN apk --no-cache add nodejs npm

# Install Express.js
RUN npm install express

# Information about the internal container port on which the application listens
EXPOSE 8080

# Default command to start the container
CMD ["node", "index.js"]

# Set the environment variable for the application version
ENV VERSION=production.${VERSION:-v1.0}

验证步骤

  1. 重新构建镜像:docker build --build-arg VERSION=2.0.0 -f Dockerfile10 -t local/base:v10 .
  2. 启动容器:docker run -d -p 8090:8080 --name web10 local/base:v10
  3. 访问验证:curl http://localhost:8090,此时返回的应用版本应为production.2.0.0,符合预期。

内容的提问来源于stack exchange,提问作者Miłosz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 10:56:20