Docker Community Forums

Share and learn in the Docker community.

Cant connect to from Container to Container

Hey there, i have a docker dev enviroment on my Macbook.
The database is a postgres container, with the following ip and port mapping:

IP: “”,
Ports: 5432/tcp,>5432/tcp

A java backend is connecting with this database, if i run the backend on my host with “localhost:5433” everything is fine.
But when i build it to a container an i change the connection string to “” i get an error:
“The connection attempt failed”

Worked alot on it but dont get it, am i doing something wrong?

My docker-compose file looks like this:

version: '2'
    container_name: api
    image: api:${version}
    restart: always

    container_name: webserver
        - api
    image: nginx:${version}
        - 80:80
    restart: always

the postgresql database is a separte container startet via docker run.


Not enough details to show you what’s wrong.
Display your docker-compose.yml file.
Containers talk to each other by the service names you define in your docker-compose.yml file.

1 Like

If you are starting the postgres container separate from the docker-compose.yml file, it will inaccessible to the containers in the docker-compose.yml file looking at the docker-compose.yml file you displayed here.

You will need to create a network and then attach all containers to that network including the docker container run for the postgres database by specifying a --network argument and the api container in your docker-compose.yml file (using a network definition).


Create a network named my_network

🐳  gforghetti:[~/Downloads] $ docker network create **my_network**

Start the PostgreSQL database server in a container and attach it to the Network my_network

🐳  gforghetti:[~/Downloads] $ docker container run -it --detach --name my_db --network my_network --env POSTGRES_DB=MY_DB --env POSTGRES_USER=my_user --env POSTGRES_PASSWORD=secret postgres:latest

docker-compose.yml file

Note: Your docker-compose file version 2 is too back level.
Note: The networks

🐳  gforghetti:[~/Downloads] $ cat docker-compose.yml
version: '3.3'

    external: true

    container_name: api
    image: ubuntu:latest
    command: sleep infinity
    restart: always
      - my_network
    container_name: webserver
        - api
    image: nginx:latest
        - 80:80
    restart: always
      - my_network

Bring up the nginx and api stack with docker-compose

🐳  gforghetti:[~/Downloads] $ docker-compose up -d
Creating api ... done
Creating webserver ... done

Display the containers

🐳  gforghetti:[~/Downloads] $ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
dd84c3f42082        nginx:latest        "nginx -g 'daemon of…"   8 seconds ago       Up 7 seconds>80/tcp       webserver
879cfa4fe070        ubuntu:latest       "sleep infinity"         9 seconds ago       Up 8 seconds                                 api
1ef488b18b59        postgres:latest     "docker-entrypoint.s…"   20 seconds ago      Up 19 seconds       5432/tcp   my_db

Bring up a shell prompt in the api container and install the PostgreSQL client so I can test the connect to the PostgreSQL database running in the my_db container.

🐳  gforghetti:[~/Downloads] $ docker container exec -it api bash
root@879cfa4fe070:/# apt-get update -qq > /dev/null 2>&1
root@879cfa4fe070:/# apt-get install postgresql-client -y > /dev/null 2>&1

Connect to the PostgreSQL database using the hostname of my_db

root@879cfa4fe070:/# psql --host=my_db --username=my_user --dbname=MY_DB
Password for user my_user:
psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1), server 11.2 (Debian 11.2-1.pgdg90+1))
WARNING: psql major version 10, server major version 11.
         Some psql features might not work.
Type "help" for help.

MY_DB=# select version();
 PostgreSQL 11.2 (Debian 11.2-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
(1 row)

MY_DB=# \q
1 Like

Thank you for helping me ,I appreciate it. This works!