Connecting pgadmin to postgres in docker

2019-08-06 20:36发布

I have a docker-compose file with services for python, nginx, postgres and pgadmin:

services:
  postgres:
    image: postgres:9.6
    env_file: .env
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5431:5431"

  pgadmin:
    image: dpage/pgadmin4
    links:
      - postgres
    depends_on:
      - postgres
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: pwdpwd
    volumes:
      - pgadmin:/root/.pgadmin
    ports:
      - "5050:80"

  backend:
    build:
      context: ./foobar  # This refs a Dockerfile with Python and Django requirements
    command: ["/wait-for-it.sh", "postgres:5431", "--", "/gunicorn.sh"]
    volumes:
      - staticfiles_root:/foobar/static
    depends_on:
      - postgres

  nginx:
    build:
      context: ./foobar/docker/nginx
    volumes:
      - staticfiles_root:/foobar/static
    depends_on:
      - backend
    ports:
      - "0.0.0.0:80:80"

volumes:
  postgres_data:
  staticfiles_root:
  pgadmin:

When I run docker-compose up and visit localhost:5050, I see the pgadmin interface. When I try to create a new server there, with localhost or 0.0.0.0 as host name and 5431 as port, I get an error "Could not connect to server". If I remove these and instead enter postgres in the "Service" field, I get the error "definition of service "postgres" not found". How can I connect to the database with pgadmin?

1条回答
Ridiculous、
2楼-- · 2019-08-06 21:29

Be careful that the default postgres port is 5432 not 5431. You should update the port mapping for the postgres service in your compose file. The wrong port might be the reason for the issues you reported. Change the port mapping and then try to connect to postgres:5432. localhost:5432 will not work.

查看更多
登录 后发表回答