Nodejs container unreachable "the connection was reset"

Hi, I am rather new to Docker and managed to build a workaround for my app. For the front I’m using nodejs container, but I faced an issue as I had to make a second nodejs container with its own ports for sake of project migration. As I’ve run docker-compose up with the new container, my nginx, nodejs containers were unreachable by browser, though according to docker ps all of the containers were running and binded to 0.0.0.0:their_port.

I’ve tried to revert all changes I made but it didn’t work out. I also applied the docker system prune -a, though nothing has changed with the problem. I managed to make the nginx container work fine by renaming COMPOSE_PROJECT_NAME, but the nodejs container still responds with “the connection was reset”.

My docker-compose file:

version: '3'

services:
  nginx:
    build:
      context: ./.docker/build
      dockerfile: nginx.dockerfile
      args:
        USER_ID: 1000
        GROUP_ID: 1001
    restart: always
    ports:
      - 80:80
      - 9000:9000
    volumes:
      - ./config/nginx:/etc/nginx/conf.d
       - ${APP_PATH_HOST_BACK}:${APP_PATH_CONTAINER_BACK}
    working_dir: ${APP_PATH_CONTAINER_BACK}
    depends_on:
      - mariadb
    links:
      - mariadb
      - nodejs

  php:
    build:
      context: ./.docker/build
      dockerfile: php.dockerfile
      args:
          USER_ID: 1000
          GROUP_ID: 1001
    restart: always
    volumes:
      - ${APP_PATH_HOST_BACK}:${APP_PATH_CONTAINER_BACK}
    working_dir: ${APP_PATH_CONTAINER_BACK}
    depends_on:
    - mariadb

  mariadb:
    image: mariadb:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: password
    volumes:
      - ${DB_PATH_HOST}:/var/lib/mysql

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
    environment:
      ADMINER_DEFAULT_SERVER: mariadb

  nodejs:
    build:
        dockerfile: nodejs.dockerfile
        args:
          APP_PATH_HOST_FRONT: ${APP_PATH_HOST_FRONT}
          APP_PATH_CONTAINER_FRONT: ${APP_PATH_CONTAINER_FRONT}
    ports:
        - 3000:3000
        - 3001:3001
    restart: always
    # volumes:
    #   - ${APP_PATH_HOST_FRONT}:${APP_PATH_CONTAINER_FRONT}
    working_dir: ${APP_PATH_CONTAINER_FRONT}

  redis:
    build:
        context: ./.docker/build
        dockerfile: redis.dockerfile
    command: redis-server --save 20 1 --loglevel warning --requirepass password
    ports:
        - 6379:6379
    restart: always
    volumes:
      - ${APP_PATH_HOST_REDIS}:${APP_PATH_CONTAINER_REDIS}
      # - ./config/redis/redis.conf:/usr/local/etc/redis/redis.conf

nodejs dockerfile:

FROM node:16-alpine

ARG APP_PATH_HOST_FRONT

ARG APP_PATH_CONTAINER_FRONT

WORKDIR ${APP_PATH_CONTAINER_FRONT}

COPY ${APP_PATH_HOST_FRONT} ${APP_PATH_CONTAINER_FRONT}

RUN npm install

#EXPOSE 3000:3000

CMD npm run dev

I’m running on:
Docker Desktop v4.17.1
Docker: v20.10.23
Docker Compose: v2.15.1
Windows 10 Home, WSL2

What could be the case of the issue?

That only shows the port forwards, not the port that the process inside the container is listening on. In your code you have two port definitions for nodejs but you haven’t mentioned which one you try to use. Probably port 3000 since this is the port in the comment in the Dockerfile… Is nodejs actually running properly inside the container and accessible from the inside on the port 3000? If it is listening only on that port on localhost, that will not be enough. It has to listen on 0.0.0.0 similar to what you see from the host in the output of docker ps.

What was your problem with nginx? How changing the project name helped? Was the old project name the same as another project?

Nodejs utilizes several ports, I just removed second(3001) to simplify testing. Nodejs is running and accessible though docker exec -it with all required content inisde.

Yes, nodejs is deployed primarily to localhost:3000. I unsure what you mean, I have to to define IP in the docker-compose file for the container?

My docker ps look as follows:

It had same issue as nodejs container. I wasn’t able to reach it from the browser, even nginx default message on the root. As I’ve changed project name from “project” to “project1” it started to work as should.

I see. I can’t explain that now.

I don’t know how it is done in case of NodeJS, but if npm run dev run sa process that listens only on localhost, you need to find out the right parameters of enrvironment variables to change that behavior.

If you want to know whethe the process is listening on localhost, you can use a debug container and run in the network namespace of the nodejs container so you can use netstat to list the ip addresses and ports that are used inside the container

docker run --rm --network container:project-nodejs-1 nicolaka/netshoot netstat -nat

I totally forgot to mention, I have Nuxt 2 deployed on 3000 port. Also, as I have met with this problem, I noticed that I couldn’t use syntax like(instead of pointing WORKDIR and COPY in Dockerfile):

  nodejs:
    build:
        dockerfile: nodejs.dockerfile
        # args:
        #   APP_PATH_HOST_FRONT: ${APP_PATH_HOST_FRONT}
        #   APP_PATH_CONTAINER_FRONT: ${APP_PATH_CONTAINER_FRONT}
    ports:
        - 3000:3000
        - 3001:3001
    restart: always
    volumes:
      - ${APP_PATH_HOST_FRONT}/package.json:${APP_PATH_CONTAINER_FRONT}/package.json
      - ${APP_PATH_HOST_FRONT}/node_modules:${APP_PATH_CONTAINER_FRONT}/node_modules
      - ${APP_PATH_HOST_FRONT}:${APP_PATH_CONTAINER_FRONT}
    working_dir: ${APP_PATH_CONTAINER_FRONT}

That would return Tracker "idealTree" already exists Though such approach worked well before.

EDIT:

Thanks a lot for pointing out the problem. For some reason Nuxt 2 refused to work(though not sure why) with old config. As I’ve added to nuxt.config.js:

  server: {
    host: '0.0.0.0'
  },

Everything started to work well like before.

1 Like

Thx! I’ve been looking for 2 days, why my app express doesn’t work with docker with the same symptoms & the solution has been update the server to listent on the 0.0.0.0 host!