[DOCKER] About communication between containers

Overview

The container defined in docker-compose.yml is placed under the same network. Containers can access each other by that name.

/var/www/container1# ping container2
64 bytes from ...abridgement

docker-compose.yml I want to access containers 1 and 2 at such times.

docker-compose.yml


version: "3"

services:
  container1:
    build:
      context: ./docker/container1
    ports:
      - "3000:3000"

  container2:
    build:
      context: ./docker/container2
    ports:
      - "8080:80"

why

A DNS server in the network is standing and resolving names. Normally, the local ip of the host machine cannot be accessed from the container, but the DNS server resolves the name with the port assigned to each container ✨ (You can see the DNS server running with the dig command)

bonus

How to resolve the name by writing ip and domain directly in hosts in the container. Since the IP address of the host machine changes dynamically with ip4 and ip6 depending on the wifi setting, the development environment may not work unintentionally.

docker-compose.yml


# ports:
#    - "8080:80"
extra_hosts:
      - "localhost-dev:${IP address of host machine}"

Recommended Posts

About communication between containers
[K8s] I tried communication between pods!
About =
About the difference between irb and pry