How to connect to localhost postgres database from docker container?

I’m configured my project to docker. I have database that have been used in non-docker period and now I want to connect my docker-compose db service to it. But when I write docker-compose up – existing database not used – new one created instead (I suspect, docker container simply doesn’t see the database). If I do nonsense please let me know. Maybe I shoud migrate my server db into container.

Here is my docker-compose.yml:

  • Set `--net-alias` using Docker API?
  • Docker: What are the best practices when tagging images for an environment
  • Docker save not working on Ubuntu 16.04
  • meteor webpack did not hot reload
  • How to give docker container write/chmod permissions on mapped volume?
  • Access Right Issue with Docker WordPress Volume mounted on network share
  • services:
      db:
        restart: always
        image: postgres:latest
        environment:
          - POSTGRES_DB=mydb
          - POSTGRES_PASSWORD=p@ssw0rd
          - POSTGRES_USER=root
        ports:
          - "5432:5432"
        volumes:
          # We'll mount the 'postgres-data' volume into the location Postgres stores it's data:
          - postgres-data:/var/lib/postgresql/data
      web:
        build: .
        command: bash -c "python manage.py collectstatic --noinput && ./manage.py migrate && ./run_gunicorn.sh"
        volumes:
          - .:/code
          - /static:/static
        ports:
          - 443:443
        depends_on:
          - db
      nginx:
        restart: always
        image: nginx:latest
        ports:
          - 80:80
        volumes:
          - ./misc/nginx.conf:/etc/nginx/conf.d/default.conf
          - /static:/static
        depends_on:
      - web
    

  • No module named rest_framework . Django + Docker
  • Docker compose redundancy?
  • Windows Container - How to commit a container
  • How ensure at least one service is running on every Docker Swarm node
  • Overriding image version in docker-compose file
  • Docker container crashes on start: java.net.NoRouteToHostException: No route to host
  • 2 Solutions collect form web for “How to connect to localhost postgres database from docker container?”

    I think, the canonic approach is to have your DB engine running in container while storing the data on the persistent storage (map the volume to your hard disk).
    So I would use the Postgres in docker as ServerDB, as you suggested.

    If you only want your application connect to the external database, declare it as an external host:

    version: '2'
    services:
    
      web:
        build: .
        command: bash -c "python manage.py collectstatic --noinput && ./manage.py migrate && ./run_gunicorn.sh"
        volumes:
          - .:/code
          - /static:/static
        ports:
          - 443:443
        extra_hosts:
          - "db:192.168.1.2"
    
      nginx:
        restart: always
        image: nginx:latest
        ports:
          - 80:80
        volumes:
          - ./misc/nginx.conf:/etc/nginx/conf.d/default.conf
          - /static:/static
        depends_on:
          - web
    

    Just be sure your application reference the database as db and replace the ip I put there with your host ip.

    Regards

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.