0

I'm studying docker, so I got a default react app and I'm trying to implement docker in it, and everything worked fine when I ran on the default port (3000) but when I try to change ports to 8000, firefox gives me an error (connection was reset). I would like to know what is happening for this error to happen.

Dockerfile:

FROM node:13-alpine
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json /app/package.json
RUN npm install --silent
RUN npm install react-scripts@3.4.1 -g --silent
COPY . .

EXPOSE 8000
EXPOSE 8001
CMD ["npm", "start"]

docker-compose.yml:

version: '3.7'
services:
    app:
        container_name: docker
        build:
            context: .
            dockerfile: Dockerfile
        volumes:
            - '.:/app'
            - '/app/node_modules'
        ports:
            - '8000:8000'
        environment:
            - NODE_ENV=development
Heitor Kenzou
  • 69
  • 2
  • 7
  • 1
    You changed the docker side to forward port 8000 on the host to 8000 in the container, but you haven't configured NPM to listen on port 8000 in the container, so it's still using its default . See https://stackoverflow.com/questions/24750253/how-npm-start-runs-a-server-on-port-8000/44473239 – erik258 Nov 24 '21 at 18:05
  • Does this answer your question? [How npm start runs a server on port 8000](https://stackoverflow.com/questions/24750253/how-npm-start-runs-a-server-on-port-8000) – erik258 Nov 24 '21 at 18:05
  • Thank you!! so whenever i want should i change the port in package.json? there is no way to choose port by docker? – Heitor Kenzou Nov 24 '21 at 18:25
  • That link I pasted sad you can use `PORT` environment variable, which you can set in docker-compose right next to NODE_ENV – erik258 Nov 24 '21 at 18:35

1 Answers1

0

add to your package.json

 "scripts": {
    "start": "PORT=8000 react-scripts start",
  },