使用 nginx 服务器构建 NextJS Docker 镜像

时间:2021-04-29 12:50:17

标签: docker nginx next.js

我是 Docker 的新手,并试图通过它的 documentation 来学习它。因为我需要使用 docker 镜像为 nginx 服务器创建 NextJS 构建,所以我遵循了以下过程。

  1. 安装 nginx
  2. Seeding the port 80 to 3000 in the default config
  3. out 目录符号链接到基础 nginx 目录
  4. 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

1 个答案:

答案 0 :(得分:1)

我对 alpine 镜像没有太多经验,但我认为您必须使用 apk(Alpine Package Keeper)来安装软件包

尝试apk add nginx而不是apt-get install nginx -y