0

I had created a Dockerfile for node js project with nginx(without docker-compose.yml) Actually I have to run it on gcp, so, I don't need that file. I have to run the project through dockerfile only

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;"]

When I am going to up it on gcp I am getting the error

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.

1 Answers1

0

Here you can go. Modify it accordingly:

# 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;"]

You can build the Docker image using the following command:

docker build -t my-node-app .

Then, you can run the Docker container using the following command:

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

This will start the Nginx server and serve your Node.js application on port 80.