'My backend app cannot connect to the dockerized postgres database on the server but in my local system it can connect

compose file:

This works in my local system without error. But in the server next-siteName-backend cannot connect to the postgres:

version: '3.7'

services:
  redis: 
    image: redis:7.0-rc1-alpine3.15
    container_name: next_redis
    restart: always
    networks:
      - next-siteName

  my-postgres:
    image: postgres:13
    container_name: next_postgres
    restart: always
    volumes:
      - next-siteName-postgres:/data/postgres
    env_file:
      - .postgresql.env
    networks:
      - next-siteName

  next-siteName-backend:
    image: next-siteName:latest
    container_name: next_siteName_backend
    depends_on:
      - my-postgres
      - redis
    restart: always
    networks:
      - next-siteName
    build:
      context: .
      dockerfile: next.Dockerfile
    environment:
      - DATABASE_URL=postgresql://postgres:123456@next_postgres:5432/siteName_backend?schema=public&connection_limit=10&pool_timeout=20
      - REDIS_HOST=redis
    env_file:
      - .env

networks:
  next-siteName:
    external: 
      name: next-siteName

volumes:
  next-siteName-postgres:

And here is the .postgresql.env:

POSTGRES_PASSWORD=123456
POSTGRES_USER=postgres
POSTGRES_DB=flyver_backend
PGDATA=/var/lib/postgresql/data/pgdata

docker version prints in my local system:

Client: Docker Engine - Community
 Version:           20.10.14
 API version:       1.41
 Go version:        go1.16.15
 Git commit:        a224086
 Built:             Thu Mar 24 01:48:07 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.14
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.15
  Git commit:       87a90dc
  Built:            Thu Mar 24 01:45:56 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.5.11
  GitCommit:        3df54a852345ae127d1fa3092b95168e4a88e2f8
 runc:
  Version:          1.0.3
  GitCommit:        v1.0.3-0-gf46b6ba
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0


Solution 1:[1]

The solution is simple. But a little confusing. It has something to do with docker version. On my ubuntu server docker version prints this:

Client: Docker Engine - Community
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:45:33 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.12
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.12
  Git commit:       459d0df
  Built:            Mon Dec 13 11:43:42 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.13
  GitCommit:        9cc61520f4cd876b86e77edfeb88fbcd536d1f9d
 runc:
  Version:          1.0.3
  GitCommit:        v1.0.3-0-gf46b6ba
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

So I have to do change the postgres service name from my-postgres to next_postgres. As you see sometimes things gets too mysterious IMO.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kasir Barati