使用nginx为node js项目创建一个docker文件,不使用docker-compose,YML

jxct1oxe  于 2023-04-29  发布在  Nginx
关注(0)|答案(1)|浏览(205)

我用nginx为node js项目创建了一个Dockerfile(没有docker-compose)。yml)实际上我必须在gcp上运行它,所以,我不需要那个文件。我必须只通过dockerfile运行项目

FROM node:16 as nodework

WORKDIR /auto-notification

COPY package*.json ./

RUN npm install

COPY . .

#EXPOSE 8080

CMD ["npx","nodemon"]

#nginx block
FROM nginx:1.23-alpine
WORKDIR /user/nginx
RUN rm -rf ./*
COPY --from=nodework /auto-notification .
#EXPOSE 8080
ENTRYPOINT ["nginx", "-g", "daemon off;"]

当我在gcp上升级它时,我得到了错误

ERROR: (gcloud.run.services.update) The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.
0yycz8jy

0yycz8jy1#

给你相应修改:

# Use an official Node.js runtime as a parent image
FROM node:14-alpine AS build

# Set the working directory to /app
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy the rest of the application files to the working directory
COPY . .

# Build the application
RUN npm run build

# Use an official Nginx runtime as a parent image
FROM nginx:1.21-alpine

# Copy the built application from the previous stage to the Nginx image
COPY --from=build /app/dist /usr/share/nginx/html

# Copy the Nginx configuration file to the Nginx image
COPY nginx.conf /etc/nginx/nginx.conf

# Expose port 80
EXPOSE 80

# Start Nginx
CMD ["nginx", "-g", "daemon off;"]

您可以使用以下命令构建Docker镜像:

docker build -t my-node-app .

然后,您可以使用以下命令运行Docker容器:

docker run -p 80:80 my-node-app

这将启动Nginx服务器并为您的Node提供服务。js应用程序在端口80上。

相关问题