Donald French
Donald French

Reputation: 1801

Docker container to container connect: connection refused

When all are run standalone outside of docker it works with no problem when core attempts to do a get from cerner. However, doing the same when all are dockerized as below I get:

Get http://cerner:8602/api/v1/patient/search: dial TCP 192.168.240.4:8602: connect: connection refused. The .4 is the IP of the cerner container and .2 is the IP of the core container

Cerner is the name of the container being called from core. If I change the name to the ip-address of the host server and use the ports, it works fine also. It just does not allow container to container using the containers DNS or IP. I have attempted with and without the private network and get the same thing.

The containers are all scratch go.

version: '3.7'

services:    caConnector:
      image: vertisoft/ca_connector:latest
      ports:
        - "8601:7001"
      env_file:
        - .env.ca_connector
      networks:
        - core-net

   fhir:
      image: vertisoft/fhir_connector:latest
      container_name: cerner
      ports:
        - "8602:7002"
      env_file:
        - .env.fhir_connector
      networks:
        - core-net

   core:
      image: vertisoft/core:latest
      ports:
        - "8600:7000"
      env_file:
        - .env.core
      networks:
        - core-net

networks:   core-net:
    driver: bridge

Upvotes: 3

Views: 2705

Answers (1)

Adiii
Adiii

Reputation: 59906

You should call the container service with containerPort, not with hostPort in service to service communication. in your case, it should be 7000 to 7002 for any container to connect using container name.

Get http://cerner:8602/api/v1/patient/search: dial TCP 192.168.240.4:8602: connect: connection refused.

As in the error, it tries to attempt connection using publish port.

For example

version: "3"
services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres
    ports:
      - "8001:5432"

When you run docker-compose up, the following happens:

A network called myapp_default is created.

A container is created using web’s configuration. It joins the network myapp_default under the name web. A container is created using db’s configuration. It joins the network myapp_default under the name db.

In v2.1+, overlay networks are always attachable

Each container can now look up the hostname web or db and get back the appropriate container’s IP address. For example, web’s application code could connect to the URL postgres://db:5432 and start using the Postgres database.

It is important to note the distinction between HOST_PORT and CONTAINER_PORT. In the above example, for db, the HOST_PORT is 8001 and the container port is 5432 (postgres default). Networked service-to-service communication use the CONTAINER_PORT. When HOST_PORT is defined, the service is accessible outside the swarm as well.

Within the web container, your connection string to db would look like postgres://db:5432, and from the host machine, the connection string would look like postgres://{DOCKER_IP}:8001.

compose-networking

Upvotes: 4

Related Questions