Search code examples
dockernext.jsdocker-composedevcontainer

Docker & Nextjs - ECONNREFUSED 127.0.0.1:<random port> after Docker Desktop update


after updating Docker Desktop (on latest macOS) from 4.29 to 4.30 we've started experiencing ECONNREFUSED errors on random ports when trying to develop our Nextjs 13 app. We use devcontainers for the development, and the setup is fairly simple - see docker-compose below:

services:
  2on2off:
    container_name: xxx_app
    build:
      context: .
      dockerfile: Dockerfile
      args:
        VARIANT: 18-bullseye
    init: true
    env_file:
      - ../.env
    command: sleep infinity
    depends_on:
      - db
    volumes:
      - ..:/workspace
      - pnpm_store:/workspace/.pnpm-store
      - node_modules:/workspace/node_modules
      - api_node_modules:/workspace/api/node_modules
      - functions_node_modules:/workspace/functions/node_modules
    network_mode: service:db

  db:
    container_name: xxx_db
    image: postgres:14.6-bullseye
    environment:
      POSTGRES_USER: xxx
      POSTGRES_PASSWORD: xxx
      POSTGRES_DB: xxx
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  pnpm_store:
  node_modules:
  api_node_modules:
  functions_node_modules:
  db_data:

pnpm dev starts the development server, but anytime the connection is made we (localhost:3000) get error below. The interesting thing is that the port changes on the start of development server.

Error: connect ECONNREFUSED 127.0.0.1:36821
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 36821
}

I've looked into the changelogs of Docker Desktop & it's dependencies but I wasn't able to find anything. The same goes for Nextjs. Downgrade to 4.29 resolves the issue, but that's not the long term solution. We've tried changing the network settings in docker-compose, but without any result. What could be the cause?


Solution

  • Ok, so turns out it was Next.js v13.3.1 issue. I've set up new app for testing inside the same devcontainer, and it worked just fine. It used v13.5.6, so I've upgraded the main app to this version and the problem is gone.