liveBook · Manning

Container-to-container communication

As explained in Road to Kubernetes

Container-to-container communication is the exchange of data or service requests between separately running containers.

Container-to-container communication lets separately running containers exchange data or service requests. Docker Compose creates a shared network so one service can reach another by its service name rather than localhost. section 6.4

A practical Docker Compose workflow is:

  • Define the communicating services in compose.yaml. section 6.4.1
  • Start the services with docker compose up; Compose creates their default network. section 6.4.1
  • Enter one container with docker compose exec and call the other by hostname, such as curl http://my-nginx-service:80. section 6.4.1
  • Keep internal services private by omitting host-port declarations when outside access is unnecessary. section 6.4.1

A web container calls NGINX

A web service can request http://my-nginx-service or http://my-nginx-service:80 from inside the Compose network. NGINX returns its standard HTML response, even though the NGINX service exposes no port to the host machine. section 6.4.1

Listing 6.9 NGINX service in Docker Compose
services:
  my-nginx-service:
    image: nginx
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      - PORT=8080
    volumes:
      - mywebdata:/app/data

volumes:
  mywebdata:

Use the Compose listing to see a web service and an NGINX service sharing the automatically created network. section 6.4.1

How Road to Kubernetes covers it

How deep it goes
Explains localhost, service-name hostnames, automatic networks, internal ports, and a runnable curl test, then stops short of Kubernetes implementation.
Examples
Demonstrates a Python web service calling an NGINX container with curl over a Compose-created network.
What you can do after
Apply it via Docker Compose by naming services, starting their network, and testing requests from one container to another.
How it connects
Connects Docker Compose networking with localhost, stateful applications, stateless applications, NGINX, and Kubernetes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.