简体   繁体   中英

Docker container hostname with docker-compose

I have multiple services running each a mysql database. I'm using docker-compose to deploy my apps in docker. I need that all these services are in the same network. I use different docker-compose file for each service.

My issue is: How can I prevent to container from being named the same (in docker dns) if they have the same name in different compose files.

Exemple:

service1.yml

version: '2'
services:
  mysql:
    image: "mysql"
    network:
      - anetwork
[...]

service2.yml

version: '2'
services:
  mysql:
    image: "mysql"
    network:
      - anetwork
[...]

This two files are in separate folders. After launching all docker-compose up. The containers appears with docker ps . On named service1_mysql_1 and the other service2_mysql_1 . But when I ping mysql dns name inside the network anetwork , both responds...

How should I fix this ? I am using bad practices ?

I have already tried: - Changing the name in each compose files

I think that the options are either:

  1. Change the name to be different in each compose file, which I understand you have already tried, eg:

     version: '2' services: mysql1: image: "mysql" [...] 
  2. Use a fully-qualified name when connecting to each container, ie service1_mysql_1 and service2_mysql_1 in this case. Not ideal as it is generated but this name can be fixed by setting it explicitly using the container_name option:

     version: '2' services: mysql: image: "mysql" container_name: my-service1-mysql [...] 

    Then a container that needs to connect to this database on the anetwork can connect using the hostname my-service1-mysql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM