Docker Community Forums

Share and learn in the Docker community.

Postgres and PgAdmin Docker Compose Static Ip Connection Error

I’m trying to up postgres and pgadmin using the docker compose. However, I would like to run postgres with static ip so as not to make the server connection over pgAdmin every time. When I don’t use static ip, it connects to the server succesfully, but after adding static ip, sever connection fails.

docker-compose.yml - with static ip configuration;

version: '3.2'

services:
  postgresql:
    container_name: postgres-latest
    image: postgres
    networks:
      default:
      outside:
        ipv4_address: 192.168.220.220
    expose:
      - "5432"
    ports:
      - 5432:5432
    volumes:
      - ./volumes/postgres-data:/var/lib/postgresql/data
    environment:
      - 'POSTGRES_USER=postgres'
      - 'POSTGRES_PASSWORD=docker'
      - 'POSTGRES_DB=jira_db'
      - 'POSTGRES_ENCODING=UTF-8'

  pgadmin:
    container_name: pgadmin4-latest
    image: dpage/pgadmin4
    restart: always
    depends_on:
      - postgresql
    ports:
      - 5454:5454/tcp
    environment:
      - PGADMIN_DEFAULT_EMAIL=ali.turkkan@hotmail.com.tr
      - PGADMIN_DEFAULT_PASSWORD=docker
      - PGADMIN_LISTEN_PORT=5454
    volumes:
      - ./volumes/pgadmin-data:/var/lib/pgadmin
    networks:
      - default

volumes:
  postgres-data:
    external: false
  pgadmin-data:
    external: false

networks:
  outside:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: 192.168.220.0/24

docker-compose - without static ip configuration;

version: '3.2'

services:
  postgresql:
    container_name: postgres-latest
    image: postgres
    networks:
      - jiranet
    expose:
      - "5432"
    ports:
      - 5432:5432
    volumes:
      - ./volumes/postgres-data:/var/lib/postgresql/data
    environment:
      - 'POSTGRES_USER=postgres'
      - 'POSTGRES_PASSWORD=docker'
      - 'POSTGRES_DB=jira_db'
      - 'POSTGRES_ENCODING=UTF-8'

  pgadmin:
    container_name: pgadmin4-latest
    image: dpage/pgadmin4
    restart: always
    depends_on:
      - postgresql
    ports:
      - 5454:5454/tcp
    environment:
      - PGADMIN_DEFAULT_EMAIL=ali.turkkan@hotmail.com.tr
      - PGADMIN_DEFAULT_PASSWORD=docker
      - PGADMIN_LISTEN_PORT=5454
    volumes:
      - ./volumes/pgadmin-data:/var/lib/pgadmin
    networks:
      - jiranet

volumes:
  postgres-data:
    external: false
  pgadmin-data:
    external: false

networks:
  jiranet:
    driver: bridge

While I can’t connect to pgadmin with the static ip that I set in the first configuration, I can connect to the server using the ip address automatically determined in the second settings. What could be the reason?