简体   繁体   中英

Connect SQL IDE to docker db container?

I am using docker with three services:

version: '2'
services:
  nginx:
    image: nginx:latest
    container_name: nz01
    ports:
      - "8000:8000"
    volumes:
      - ./src:/src
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
  web:
    build: .
    container_name: dz01
    depends_on:
      - db
    volumes:
      - ./src:/src
    expose:
      - "8000"
  db:
    image: postgres:latest
    container_name: pz01
    volumes:
      - ./postgres-data:/var/lib/postgresql/data

All seems to work pretty well, and I know that db service is running on 172.18.0.2 but when I use Datagrip to connect to the DB to work faster with table creation and inserts and things like that It does not work.

My configuration at django app is:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'pz01',
        'PORT': 5432,
    }
} 

The configuration at Datagrip:

'DB_NAME': 'postgres',
'USER': 'postgres',
'HOST': 'pz01',
'PORT': 5432,

I also tried with host as the ip, and localhost and neither works. The password as null, because the db service log shows that by default it does not have password.

How can I connect to my docker db from my macbook?

EDIT: As @Dihgg says I fixed the port at DB service.

I set at

db:
    image: postgres:latest
    container_name: pz01
    ports:
     - "5433:5432"
    volumes:
      - ./postgres-data:/var/lib/postgresql/data

The port is not 5432:5432 because when this is the value I get:

ERROR: for db  Cannot start service db: b'driver failed programming external connectivity on endpoint pz01 (146bb3cdada29f75766aa888143b4af17b267d13096db60fb0ccdaedb710e77e): Error starting userland proxy: Bind for 0.0.0.0:5432 failed: port is already allocated'
    ERROR: Encountered errors while bringing up the project.

Despite that, I can't connect from Datagrip, when I use pz01 as host the error is(trying with 5432 or 5433 port and no password set):

Host is unknown

When I use as host the ip the error is: Connection failed

Try expose the port in the db container

db:
    image: postgres:latest
    container_name: pz01
    ports:
        - "5432:5432"
    volumes:
    - ./postgres-data:/var/lib/postgresql/data

And then, use the localhost:5432 to connect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM