我想创建一个 Dockerfile,以便通过 NPM 从 NuxtJS 页面构建一个 Web 服务器。 使用 Dockerfile 进行构建没有问题,但一旦我通过 docker compose 启动容器,就会收到错误。在这里查看我的 Dockerfile:
FROM node:18-alpine
WORKDIR /usr/src/app
COPY ./data/package*.json ./
RUN npm install
COPY ./data .
EXPOSE 3000
ENV NODE_ENV=production
RUN addgroup -g 1001 -S appuser && \
adduser -u 1001 -S appuser -G appuser
RUN chown -R appuser:appuser /usr/src/app
USER appuser
CMD ["npm", "start"]
感谢您的帮助。
这是我的输出:
root@raspberrypi:/home/nuxtjs-docker-deployment# docker compose build --no-cache
[+] Building 301.1s (12/12) FINISHED docker:default
=> [web internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 845B 0.0s
=> [web internal] load metadata for docker.io/library/node:18-alpine 0.9s
=> [web internal] load .dockerignore 0.0s
=> => transferring context: 68B 0.0s
=> [web 1/7] FROM docker.io/library/node:18-alpine@sha256:b1a0356f7d6b86c958a06949d3db3f7fb27f95f627aa6157cb98bc65c801efa2 0.0s
=> [web internal] load build context 5.2s
=> => transferring context: 1.59MB 5.0s
=> CACHED [web 2/7] WORKDIR /usr/src/app 0.0s
=> [web 3/7] COPY ./data/package*.json ./ 0.6s
=> [web 4/7] RUN npm install 71.5s
=> [web 5/7] COPY ./data . 32.3s
=> [web 6/7] RUN addgroup -g 1001 -S appuser && adduser -u 1001 -S appuser -G appuser 2.2s
=> [web 7/7] RUN chown -R appuser:appuser /usr/src/app 159.2s
=> [web] exporting to image 28.5s
=> => exporting layers 28.5s
=> => writing image sha256:18688212afa360bbeacd24b0b4ec9e064b56370276e2bd25cb9f0d42e7530783 0.0s
=> => naming to docker.io/library/nuxtjs-docker-deployment-web 0.0s
root@raspberrypi:/home/nuxtjs-docker-deployment# docker compose down -v && docker compose up -d
[+] Running 2/2
✔ Container nuxtjs-docker-deployment-web-1 Removed 0.0s
✔ Network nuxtjs-docker-deployment_default Removed 0.4s
[+] Running 1/2
⠸ Network nuxtjs-docker-deployment_default Created 3.3s
✔ Container nuxtjs-docker-deployment-web-1 Started 3.1s
root@raspberrypi:/home/nuxtjs-docker-deployment# docker logs -f c27d9889469d
> start
> nuxt start
sh: nuxt: Permission denied
root@raspberrypi:/home/nuxtjs-docker-deployment#
我解决了这个问题,将这一行添加到 Dockerfile 中
RUN chmod +x node_modules/.bin/nuxt