1

I have two containers (flask application) on the same network. In my case, the two containers can communicate with IP address but it doesn't work as expected with the container name.

I have read on the documentation that containers can not only communicate by IP address, but can also resolve a container name to an IP address. This capability is called automatic service discovery.

I don't understand what I am missing.

Thank you for the replies.

EDIT : How I run the containers :
docker run -p 5001:5001 --net testnetwork --rm
docker run -p 5000:5000 --net testnetwork --rm

Baptiste
  • 115
  • 1
  • 11
  • 1
    Can you share how are you running containers? Dockerfile, command, docker compose used... – J.F. Jan 02 '21 at 23:14
  • Have you added the containers to the same network? You can inspect the network and see if both of the containers are in the same network using `docker network inpect`. Then you can make them communicate using the container names. – Fatih Aktaş Jan 02 '21 at 23:51
  • @FatihAktaş as i said "I have two containers (flask application) on the same network" – Baptiste Jan 03 '21 at 16:51

3 Answers3

0

If you are running multiple docker containers I will recommend using docker-compose, because by default they are created in the same network, and you can communicate between them by using the names you gave them. For example, lets suppose you have a docker-compose file as follows

version: '3.1'

services:

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: database
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8081:80

volumes:
  db:

If you launch this, you will get that you can communicate to MySQL from PhpMyAdmin by simply typing its name("db"), no IP needed. enter image description here

The thing about using IP is that they change unless you assign them manually, you can find more info on how to do this in Assign static IP to Docker container

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Hello the point is I want to understand why it doesn't work not to have an another to do it. According to the documentation when containers are on the same network, the name of the container should be the host. – Baptiste Jan 03 '21 at 16:53
  • @Baptiste In that case you should be running your containers with the flag `--name` otherwise their names change every time they are run. – Richard Montoya Jan 04 '21 at 15:09
0

Based no documentation to communicate by container-name they must be linked

Containers can communicate via their IP addresses by default. To communicate by name, they must be linked.

Ref.: https://docs.docker.com/engine/reference/commandline/run/

Oleg Andreyev
  • 647
  • 5
  • 20
0

According to what I have found, you need to explicitly name the containers for the automatic service discovery to work in the user-defined network. Try adding a --name containername1 and --name containername2 when starting up the containers.

You can also use the hostname to make the containers communicate in the same network. link

A container’s hostname defaults to be the container’s ID in Docker. You can override the hostname using --hostname. When connecting to an existing network using docker network connect, you can use the --alias flag to specify an additional network alias for the container on that network.

Look up the contianer IDs using Docker ps. It is the first column.

Fatih Aktaş
  • 1,446
  • 13
  • 25