Connect to postgresql container from another container (Docker) Connect to postgresql container from another container (Docker) docker docker

Connect to postgresql container from another container (Docker)


do not use depends_on. try it with "links"

    version: '2'    services:      server:        build: .        ports:          - 3030:3030        links:          - database        #environment could be usefull too        environment:            DATABASE_HOST: database        command: ["./setup/wait-for-postgres.sh", "localhost:5432", "--", "node", "src"]      database:        image: postgres        environment:          - "POSTGRES_USER=postgres"          - "POSTGRES_PASSWORD=postgres"          - "POSTGRES_DB=tide_server"        ports:          - 5432:5432

for more informations https://docs.docker.com/compose/compose-file/#links


The problem here is the host itself.

psql -h "$host" -U "" -c '\l'

You are passing a wrong HOSTNAME "localhost:5432" / "192.168.64.2:5432"

What I did is setup a ~/.pgpass that haslocalhost:5432:DB:USER:PASSWORD

and instead of passing "localhost:5432", omit the port. Just use "localhost"

This works for me ...


May be an old thread to answer but I have been using depends_on with the following docker-compose file

version: '3.4'volumes:  postgres_data:      driver: localservices:  postgres:      image: postgres      volumes:        - ./postgres_data:/var/lib/postgresql:rw        - ./deployments:/opt/jboss/wildfly/standalone/deployments:rw      environment:        POSTGRES_DB: keycloak        POSTGRES_USER: keycloak        POSTGRES_PASSWORD: password      ports:        - 5432:5432   keycloak:      image: jboss/keycloak      environment:        POSTGRES_ADDR: postgres        POSTGRES_DATABASE: keycloak        POSTGRES_USER: keycloak        POSTGRES_PASSWORD: password        KEYCLOAK_USER: admin        KEYCLOAK_PASSWORD: Pa55w0rd      ports:        - 8080:8080        - 9990:9990      depends_on:        - postgres