【发布时间】:2021-04-29 21:28:15
【问题描述】:
我是 docker 新手,并试图通过 documentation 来学习它。因为我需要使用 docker 镜像为 nginx 服务器创建 NextJS 构建,所以我遵循了以下过程。
- 安装
nginx -
Seeding the port
80 to 3000in the default config。 - 将
out目录符号链接到基础 nginx 目录 - CMD 负责输出目录的生产构建和符号链接。
FROM node:alpine AS deps
RUN apk add --no-cache libc6-compat git
RUN apt-get install nginx -y
WORKDIR /sample-app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
FROM node:alpine AS builder
WORKDIR /sample-app
COPY . .
COPY --from=deps /sample-app/node_modules ./node_modules
RUN yarn build
FROM node:alpine AS runner
WORKDIR /sample-app
ENV NODE_ENV production
RUN ls -SF /sample-app/out /usr/share/nginx/html
RUN -p 3000:80 -v /sample-app/out:/usr/share/nginx/html:ro -d nginx
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /sample-app/out
USER nextjs
CMD ["nginx -g daemon=off"]
在以sudo docker build . -t sample-app 运行 docker build shell 脚本命令时,它会抛出错误 The command '/bin/sh -c apt-get install nginx -y' returned a non-zero code: 127
【问题讨论】:
-
如果您仍然要安装 GNU libc,请考虑使用基于 Debian 或 Ubuntu 的映像,而不是 Alpine。对于最终图像,还可以考虑重用标准
nginx图像;将应用程序编译为静态文件后,您就不需要在其中包含 Node。