ConnectionError in Docker with Nodejs (Hapi.js) and Prisma

Viewed 187

I would like to put my Nodejs app into a docker. When deploying it via npm run build and start I can send requests to it.
But when creating a docker image I getting problems:
First I have an EXPOSE 8080 in my Dockerfile. Then I am running docker run -p=3000:8080 --env-file .env my-docker-file. After that I am getting the info that the server is running on http://localhost:3000.
I know localhost:3000 ist just in the docker file. But at least the docker is running.

When I use the command http localhost:3000 (or the browser) I am getting http: error: ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')) while doing a GET request to URL: http://localhost:3000/.

Does someone have an idea what's going wrong??? I have no clue.

tanks to all hints that directs me into the right direction.

My Dockerfile:

## this is the stage one , also know as the build step

FROM node:12.17.0-alpine as builder

WORKDIR /app

COPY package*.json ./
COPY prisma ./prisma/
COPY tsconfig.json .
COPY src ./src/
COPY tests ./tests/

RUN npm install
RUN npx prisma generate

COPY . .

RUN npm run build

## this is stage two , where the app actually runs

FROM node:12.17.0-alpine

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist

EXPOSE 8080
CMD npm start

2 Answers

If you use a Dockerfile, first you better to build your image.

FROM node:12.17.0-alpine as builder

WORKDIR /app

COPY . .

RUN npm install
RUN npx prisma generate

RUN npm run build

## this is stage two , where the app actually runs

FROM node:12.17.0-alpine

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist

EXPOSE 8080
CMD ["npm","start"]

From the location where your Dockerfile located:

docker build -t your-image-name .

docker run -p 3000:8080 --env-file .env your-image-name

Did you check the IP address?

When i first deploy my Node project to Docker, i couldn't access it too, because my Node project was listening for localhost requests. But if you don't specify your network as host, your Docker container will have some other IP address in your subnet.

I've changed my Node projects listening IP address to 0.0.0.0 and after that i could connect to my Node project running in a Docker container.

Related