4

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

2
  • How are you running the ping command? From inside a third container? If so, is it defined in service1.yml, service2.yml or elsewhere? Commented Mar 31, 2018 at 20:20
  • also, how is your network defined? Commented Mar 31, 2018 at 20:34

1 Answer 1

1

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, e.g:

    version: '2'
    services:
      mysql1:
        image: "mysql"
    [...]
    
  2. Use a fully-qualified name when connecting to each container, i.e. 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.