0

It appears I can't access 127.0.0.1:5555 from my company_tasks container:

version: "3.9"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    restart: always
    ports:
      - "5555:5432"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  rabbit:
    image: rabbitmq
    ports:
      - "5673:5672"
    depends_on:
      - db
  company_tasks:
    build: .
    command: >
      bash -c "pip install psycopg2-binary &&
               pip install -r pip_requirements.txt &&
               python manage.py migrate &&
               python manage.py runserver &&
               celery -A company_tasks worker -l info -B"
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
      - db

Though I can access it from the host system with:

psql -h 127.0.0.1 -p 5555 -U postgres

Company_tasks is a django app and it complains:

company_tasks_1  | django.db.utils.OperationalError: connection to server at "127.0.0.1", port 5555 failed: Connection refused
company_tasks_1  |      Is the server running on that host and accepting TCP/IP connections?

my django settings.py:

DATABASES = {
    'default': {
        'ENGINE': "django.db.backends.postgresql",
        'NAME': 'company_tasks',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '5555',
    }
}

How Can I make database container visible to company_tasks container?

I've tried setting up bridge networks but it didn't work. I can't use 5432 port on the host because another instance of postgres is taking it.

konradz
  • 23
  • 1
  • 8
  • 1
    The `company_tasks` container thinks 127.0.0.1 is itself, not a different container or the host that's running the container. Use the Compose service name `db` as a host name, and use the standard PostgreSQL port 5432 (`ports:` are ignored in this case). You do not need to manually set up `networks:`, override `container_name:`, or use the obsolete `links:` option. Also see [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation. – David Maze Mar 26 '22 at 17:59

0 Answers0