0

I have this docker-compose file where I have a couple of services:

version: "3.8"

services:
  app:
    container_name: mobile_app_api
    env_file:
      - .env.development
    command: npm run watch:dev
    build:
      context: .
      target: development
      dockerfile: ./Dockerfile
    restart: always
    volumes:
      - /usr/src/app/node_modules
      - ./:/usr/src/app
    depends_on:
      - postgres
      - redis
    extra_hosts:
      - host-machine:host-gateway
    network_mode: host

  redis:
    image: "redis:alpine"
    container_name: mobile_app_cache

  postgres:
    image: postgres
    container_name: mobile_app_database
    restart: "unless-stopped"
    environment:
      TZ: 'GMT'
      PGTZ: 'GMT'
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mobile_app_database
    volumes:
      - /:/data/postgres
    network_mode: host

Api is supposed to work on localhost:3333, but when I start this with docker-compose up --build in terminal I get this:

mobile_app_api | server running on http://[::1]:3333

And here is 2 things.

  1. Why does it work on IPv6?
  2. How can I change host and make it work on localhost?

PS. My computer is MacOS, but in settings I have turned off IPv6.

I was trying to use host.docker.internal (and gateway.docker.internal) in my compose file, but it didn't work:

extra_hosts:
      - host.docker.internal:host-gateway
dokichan
  • 857
  • 2
  • 11
  • 44
  • This is something in your application configuration, not in the Docker setup. It must listen on IPv4 `0.0.0.0` or IPv6 `::` (either is fine, even if you think you've disabled IPv6) or it will be unreachable from outside Docker; see for example [Containerized Node server inaccessible with server.listen(port, '127.0.0.1')](https://stackoverflow.com/questions/35414479/containerized-node-server-inaccessible-with-server-listenport-127-0-0-1). – David Maze Jul 06 '22 at 12:43
  • On a MacOS host `network_mode: host` does not work and you also need to remove that setting. You also do not need to specify `extra_hosts:` or `container_name:`. You should not need to override the image's command or the code built into the image, and I'd also recommend removing the main container's `command:` and `volumes:`. – David Maze Jul 06 '22 at 12:45

0 Answers0