skip to Main Content

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

2

Answers


  1. In your example you can enter adminer via your browser with following URL:

    http://127.0.0.1:8080
    

    Then you can access mysql on the web interface.
    The password is "example" as configured in your db image configuration.

    If you add another service "ubuntu" then you can enter the container with:

    docker-compose exec ubuntu /bin/bash
    

    And if you have mysql installed on this container you can reach mysql with:

    mysql -h db -u root -pexample
    

    So you see the hostname is the servicename of your mysql service in the docker-compose file.

    Login or Signup to reply.
  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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search