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: “172.17.0.2”,
Ports: 5432/tcp, 0.0.0.0:5433->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 “172.17.0.2:5432” 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'
services:
  api:
    container_name: api
    image: api:${version}
    environment:
      - SPRING_PROFILES_ACTIVE=$stage
    restart: always

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

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

Hi.

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).

Example

Create a network named my_network

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

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
1ef488b18b5992831ef47ee16a2d80498276d3a18e3173cc9f12a44c42c8881b

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'

networks:
  my_network:
    external: true

services:
  api:
    container_name: api
    image: ubuntu:latest
    command: sleep infinity
    restart: always
    networks:
      - my_network
  nginx:
    container_name: webserver
    depends_on:
        - api
    image: nginx:latest
    ports:
        - 80:80
    restart: always
    networks:
      - 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        0.0.0.0:80->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();
                                                             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
root@879cfa4fe070:/# 
1 Like

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