docker / compose

Define and run multi-container applications with Docker
https://docs.docker.com/compose/
Apache License 2.0
33.62k stars 5.19k forks source link

[BUG] DB connection in docker-compose setup fails - db host not added to hosts file #11293

Closed matt-jay closed 9 months ago

matt-jay commented 9 months ago

Description

I am trying to run a Django app with a PostgreSQL database using docker-compose. However, the Django app container (web) does not appear to be able to connect to the db service. After some troubleshooting, I found out that the db host is not added to the web host's /etc/hosts file. When adding it manually, I can successfully ping the db host from within the web container.

Apparently there is an issue in the network configuration that is created by docker-compose. What might cause it?

Here is my docker-compose.yml file:

    version: '3.7'

    services:

      web:
        build: .

        volumes: 
          - .:/immoscreen

        env_file:
          - .env

        environment: 
          - APP_DB_ENGINE=${APP_DB_ENGINE}
          - APP_DB_HOST=${APP_DB_HOST}
          - APP_DB_PORT=${APP_DB_PORT}
          - APP_DB_NAME=${APP_DB_NAME}
          - APP_DB_USER=${APP_DB_USER}
          - APP_DB_PASSWORD=${APP_DB_PASSWORD}

        image: immoscreen_app

        ports:
          - ${DJANGO_APP_PORT}:${DJANGO_APP_PORT}

        expose: 
          - ${DJANGO_APP_PORT}

        restart: "on-failure"

        depends_on:
          db:
            condition: service_healthy

      db:
        image: postgres

        volumes:
          - ./db/data/:/var/lib/postgresql/data/
          - ./db/init.sql/:/docker-entrypoint-initdb.d/

        env_file:
          - .env

        environment:
          - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
          - APP_DB_NAME=${APP_DB_NAME}
          - APP_DB_USER=${APP_DB_USER}
          - APP_DB_PASSWORD=${APP_DB_PASSWORD}

        ports:
          - 5432:5432

        healthcheck:
          test: ["CMD-SHELL", "pg_isready", "-d", "${APP_DB_NAME}", "-U", "${APP_DB_USER}"]
          interval: 10s
          timeout: 10s
          retries: 5
          start_period: 2s

        hostname: db

The output of docker inspect immohub_immohub_default looks to me like the basic network configuration is fine.

    [
        {
            "Name": "immohub_immohub_default",
            "Id": "61bccd0cc1804b96fd27d4f624d321d4d94aa6564aa2cf463edb2db3a1dd318a",
            "Created": "2023-12-19T17:12:58.672457129Z",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": null,
                "Config": [
                    {
                        "Subnet": "172.19.0.0/16",
                        "Gateway": "172.19.0.1"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Ingress": false,
            "ConfigFrom": {
                "Network": ""
            },
            "ConfigOnly": false,
            "Containers": {
                "a155f3ebbe7bd7b9e0deb3bde0428d6f296413421d6b44d7ba2d1373b9a4402e": {
                    "Name": "immohub-web-1",
                    "EndpointID": "4b60f603770ed3b64f091c0f246623b6dbfbea90470116af4c66dca17aff3a16",
                    "MacAddress": "02:42:ac:13:00:03",
                    "IPv4Address": "172.19.0.3/16",
                    "IPv6Address": ""
                },
                "ef7e0815091098a058f23856725974069511f061bce0d00b4549375a85133bc1": {
                    "Name": "immohub-db-1",
                    "EndpointID": "a758bd36f4dfb64a1144b3cd1a3b75a00f1e4b7b4e67249e214ab2623e733ba7",
                    "MacAddress": "02:42:ac:13:00:02",
                    "IPv4Address": "172.19.0.2/16",
                    "IPv6Address": ""
                }
            },
            "Options": {},
            "Labels": {
                "com.docker.compose.network": "immohub_default",
                "com.docker.compose.project": "immohub",
                "com.docker.compose.version": "2.23.3"
            }
        }
    ]

Steps To Reproduce

Run docker compose -f docker-compose.yml up with the docker-compose.yml file provided in post.

Compose Version

Docker Compose version v2.23.3-desktop.2

Docker Environment

Client:
 Version:    24.0.7
 Context:    desktop-linux
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.12.0-desktop.2
    Path:     /Users/myuser/.docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.23.3-desktop.2
    Path:     /Users/myuser/.docker/cli-plugins/docker-compose
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.0
    Path:     /Users/myuser/.docker/cli-plugins/docker-dev
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.21
    Path:     /Users/myuser/.docker/cli-plugins/docker-extension
  feedback: Provide feedback, right in your terminal! (Docker Inc.)
    Version:  0.1
    Path:     /Users/myuser/.docker/cli-plugins/docker-feedback
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v0.1.0-beta.10
    Path:     /Users/myuser/.docker/cli-plugins/docker-init
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     /Users/myuser/.docker/cli-plugins/docker-sbom
  scan: Docker Scan (Docker Inc.)
    Version:  v0.26.0
    Path:     /Users/myuser/.docker/cli-plugins/docker-scan
  scout: Docker Scout (Docker Inc.)
    Version:  v1.2.0
    Path:     /Users/myuser/.docker/cli-plugins/docker-scout

Server:
 Containers: 2
  Running: 1
  Paused: 0
  Stopped: 1
 Images: 3
 Server Version: 24.0.7
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: d8f198a4ed8892c764191ef7b3b06d8a2eeb5c7f
 runc version: v1.1.10-0-g18a0cb0
 init version: de40ad0
 Security Options:
  seccomp
   Profile: unconfined
  cgroupns
 Kernel Version: 6.5.11-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 10
 Total Memory: 7.663GiB
 Name: docker-desktop
 ID: 9b4dfe62-adc0-4cad-af57-2398d8b63074
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: daemon is not using the default seccomp profile

Anything else?

No response

ndeloof commented 9 months ago

Docker engine includes a DNS service for networks, so that discovery of other services doesn't rely on /etc/hosts entries.

I tried to reproduce with a simplified compose.yaml file:

services:    
      web:
        image: alpine
        command: ping db

      db:
        image: postgres
        ports:
          - 5432:5432
        environment:
          POSTGRES_PASSWORD: test
        hostname: db

and get the expected result:

$ docker compose up --attach web
[+] Running 2/2
 ✔ Container truc-db-1   Running                                                                                                                                                 0.0s 
 ✔ Container truc-web-1  Recreated                                                                                                                                               0.1s 
Attaching to web-1
web-1  | PING db (172.30.0.2): 56 data bytes
web-1  | 64 bytes from 172.30.0.2: seq=0 ttl=64 time=0.653 ms
web-1  | 64 bytes from 172.30.0.2: seq=1 ttl=64 time=0.400 ms
web-1  | 64 bytes from 172.30.0.2: seq=2 ttl=64 time=0.137 ms
web-1  | 64 bytes from 172.30.0.2: seq=3 ttl=64 time=0.620 ms

Can you please try the same ? You also could use docker compose exec web ping db with your application running to run a comparable test

ndeloof commented 9 months ago

Please also inspect immohub-db-1 container to check Networks.Aliases is set as expected

matt-jay commented 9 months ago

Thanks for checking on this, @ndeloof. I successfully reproduced your simplified example. This led me to revisit my docker-compose file and it turned out that the image definition brought the setup off course. When I remove my image: immoscreen_app configuration, everything works as expected. I don't fully understand why, but my problem is solved.