我试图通过传递“--build-arg VERSION =”来构建图像时获取版本,但页面显示默认版本:
应用程序版本将在 docker build… 命令中指定。通过赋予价值
ARG指令定义的VERSION变量
# 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}
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 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之后的结果 -> 它显示IP地址,主机名,但版本是Production.v1.0而不是传入--build-arg的2.0.0
这里有两个问题:
ENV
需要先于 CMD
和 ARG
需要与ENV
处于同一阶段。尝试这样的事情:
FROM node:alpine AS builder
RUN apk add --update curl && \
rm -rf /var/cache/apk/*
WORKDIR /usr/app
COPY ./package.json ./
RUN npm install
COPY ./index.js ./
FROM alpine
WORKDIR /usr/app
COPY --from=builder /usr/app .
RUN apk --no-cache add nodejs npm
RUN npm install express
EXPOSE 8080
ARG VERSION
ENV VERSION=production.${VERSION:-v1.0}
CMD ["node", "index.js"]
我同意对你的问题的评论之一:看起来你真的不需要多阶段构建,因为你的
builder
阶段并没有真正做任何在最后阶段没有完成的事情。
但是,上面的
Dockerfile
至少可以工作。
您将 VERSION 构建参数传递给 Docker 构建过程,然后在 Node.js 应用程序中访问它的方式似乎可能存在问题。
Docker 构建参数:您在 Docker 构建过程中正确传递了 VERSION 参数
docker build --build-arg VERSION=2.0.0 -f Dockerfile10 -t local/base:v10
# Set the argument for the application version
ARG VERSION
...
# Set the environment variable for the application version
ENV VERSION=production.${VERSION:-v1.0}
console.log(`For conformation Application Version (before response): ${process.env.VERSION}`);
重建图像
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