Home > Net >  Docker compose networking only works on Mac not Ubuntu Linux
Docker compose networking only works on Mac not Ubuntu Linux

Time:09-19

I have the following compose file

version: '3'
services:
  db:
    image: "postgres:12.4"
    ports:
      - "15432:5432"
    env_file:
      - ./db/database.env
    volumes:
      - ./db/data:/var/lib/postgresql/data
      - ./db/init:/init
      - ./db/init/run.sh:/docker-entrypoint-initdb.d/initialise.sh
  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    network_mode: bridge
    environment:
      PGADMIN_DEFAULT_EMAIL: xxx
      PGADMIN_DEFAULT_PASSWORD: xxx
    ports:
      - "5050:80"
    extra_hosts:
      - "host.docker.internal:host-gateway"

Now when I run this locally on my Mac, all is good. I get a database which my node API can reach on localhost, and I get a pgadmin, which can connect to the database by using host.docker.internal.

When I run this on ubuntu linux, however, pgadmin tells me it can't connect to the database. Bear in a mind the non-docker node api can connect to the db just fine on localhost.

I've tried:

  1. with and without the extra-hosts bit
  2. on pgadmin trying to connect to the db with 'localhost', '127.0.0.1', 'host.docker.internal', '172.17.0.1' and the public IP
  3. changing network mode to bridge, host and removing it entirely, all while testing the aforementioned host names.

CodePudding user response:

This is a minimal setup that works. You can use it as a starting point:

version: '3'

services:
  db:
    image: "postgres:12.4"
    environment:
      POSTGRES_PASSWORD: "password"

  pgadmin:
    image: "dpage/pgadmin4"
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: xxxxxx
    ports:
      - "5050:80"

Then navigate to http://localhost:5050 and login with [email protected] / xxxxxx.

Add a new server where the host is db (service name in docker-compose.yml), user is postgres and password is password (set in docker-compose.yml).

Notes:

You use pgadmin to access the database so no need to expose database ports.

Services see each other by name: pgadmin and db in your case.

Services run on a separate, "hidden" network and you only access to that is through a mapped port: 5050 in this case.

  • Related