使用docker运行nuxt3

vql8enpb  于 2023-04-20  发布在  Docker
关注(0)|答案(4)|浏览(282)

我用这个命令构建了我的Nuxt3应用程序,它生成了文件夹.output中的所有内容:
npm run build
这是我用来构建docker文件的DockerFile:

FROM node:17-alpine3.14

ADD .output /webapp/.output

EXPOSE 3000

WORKDIR /webapp/.output

ENTRYPOINT ["node", "/webapp/.output/server/index.mjs"]

docker-compose.yml看起来像这样:

version: '3.3'

services:
  app-web:
    build: .
    ports:
      - 3000:3000

一切运行没有任何错误,但我期望看到的应用程序输入地址http://localhost:3000,但什么也没有发生。
任何帮助是高度赞赏。

pprl5pva

pprl5pva1#

以下是工作版本:

FROM node:17-alpine3.14

ADD .output /webapp/.output

EXPOSE 3000

WORKDIR /webapp/.output

ENV NUXT_HOST=0.0.0.0
ENV NUXT_PORT=3000

CMD ["node", "/webapp/.output/server/index.mjs"]
cig3rfwq

cig3rfwq2#

我的方法:
Docker Compose(用于开发)

version: '3'
services:

  web-app:
    image: node:17-alpine
    container_name: web-app
    command: [sh, -c, "npm i && npm run dev"]
    ports:
      - "3000:3000"
    working_dir: '/webapp'
    tty: true
    volumes:
      - './:/webapp/'
    environment:
      HOST: 0.0.0.0
      PORT: 3000

Docker(生产)

FROM node:17-alpine

ENV HOST='0.0.0.0'
ENV PORT='3000'

WORKDIR /webapp

COPY ./ /webapp

RUN npm install
RUN npm run build

EXPOSE 3000

CMD [ "npm", "run", "start" ]
2hh7jdfx

2hh7jdfx3#

这是我们用来运行nuxt 3设置的Dockerfile!

# see https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
ARG NODE_VERSION=node:16.14.2

FROM $NODE_VERSION AS dependency-base

# create destination directory
RUN mkdir -p /app
WORKDIR /app

# copy the app, note .dockerignore
COPY package.json .
COPY package-lock.json .
RUN npm ci

FROM dependency-base AS production-base

# build will also take care of building
# if necessary
COPY . .
RUN npm run build

FROM $NODE_VERSION-slim AS production

COPY --from=production-base /app/.output /app/.output

# Service hostname
ENV NUXT_HOST=0.0.0.0

# Service version
ARG NUXT_APP_VERSION
ENV NUXT_APP_VERSION=${NUXT_APP_VERSION}

# Run in production mode
ENV NODE_ENV=production

# start the app
CMD [ "node", "/app/.output/server/index.mjs" ]

你可以在这里找到一个例子:https://github.com/sidestream-tech/sidebase/blob/main/Dockerfile

fykwrbwg

fykwrbwg4#

这是我创建的Dockerfile。
Github Gist

FROM node:lts-alpine

WORKDIR /app

COPY package*.json ./

RUN npm ci

COPY . .

RUN npm run build

# customize port 80
EXPOSE 80
CMD ["sh", "-c", "NITRO_PORT=80 node .output/server/index.mjs"]

# default port 3000
# EXPOSE 3000
# CMD ["node", ".output/server/index.mjs"]

相关问题