1

I would like to communicate between two containers. I've read that networks should be configured either locally or in bridge mode. However, in this example below, adminer has access to mysql, but no network were configured.

I tried to add another service ubuntu install nmap and docker-compose run ubuntu nmap 127.0.0.1, but MySQL isn't accessible. How does adminer can reach db container in this example?

version: '3.1'

services:

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

  db:
    image: mysql:5.6
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
nowox
  • 25,978
  • 39
  • 143
  • 293
  • 3
    https://docs.docker.com/network/ https://docs.docker.com/compose/networking/ – KamilCuk May 25 '23 at 14:44
  • Does this answer your question? [Create networks automatically in Docker Compose](https://stackoverflow.com/questions/45255066/create-networks-automatically-in-docker-compose) – STh May 25 '23 at 14:58

1 Answers1

2

In this example, the "adminer" container can reach out the db container without configuring a network because they are both defined within the same default network created by docker compose. docker-compose automatically creates a bridge network for the containers defined in the same docker-compose.yml file.

When containers are connected to the same network, they can communicate with each other using their service names as hostnames. In this case, the adminer container can reach the db container by using the hostname db as the MySQL server address.

Regarding the ubuntu service and using nmap to scan 127.0.0.1, it will not work because 127.0.0.1 refers to the loopback interface of the ubuntu container itself. It won't be able to reach the db container using 127.0.0.1. If you want to test network connectivity between containers, you should use the service names or container names as hostnames within the same network.

Maninder
  • 1,539
  • 1
  • 10
  • 12