56

I want to use a MySQL docker container for multiple projects running on the same server.

Using docker-compose v3 files, I simply have the same mysql container configuration in each of the projects, and they have the same container_name:

version: "3"

services:
  app1:
    image: foo
    links:
      - mysql

  mysql:
    image: mysql/mysql:5.7
    container_name: shared_mysql

The second application has a similar docker-compose.yml file, but with app2 instead of app1.

When running docker-compose up --no-recreate for app2, I get an error:

Creating shared_mysql ... error
ERROR: for shared_mysql
Cannot create container for service mysql: Conflict.
The container name "/shared_mysql" is already in use by container "deadbeef".
You have to remove (or rename) that container to be able to reuse that name.

What can I do to share the MySQL container between multiple docker projects?

3 Answers 3

28

You can simply avoid redefining the mysql in one of the two docker-compose.yml files and connect mysql and the other containers to the same network.

In order to do so, create a network:

docker network create shared

Assign your network to your mysql container:

version: '3'
services:
  mysql:
    ...
    networks:
    - shared
networks:
  shared: 
    external:
      name: shared

For any other container that has need to access mysql, just add the same network definition as above:

version: '3'
services:
  app1:
    ...
    networks:
    - shared
  app2:
    ...
    networks:
    - shared
  ...
networks:
  shared: 
    external:
      name: shared
4
  • 10
    This introduces a dependency from the mysql-less compose file to the compose-with-mysql, which would be avoided if the mysql container definition is in both compose files.
    – cweiske
    Commented Aug 28, 2017 at 10:21
  • 1
    Any idea on how to share a container whose network_mode is set as "host" between multiple docker compose projects. My requirement is to share a single nginx reverse proxy container between multiple docker compose projects. Since the reverse proxy container should be run within host network mode, this solution is not worked for me. There was an error saying; ERROR: 'network_mode' and 'networks' cannot be combined Any idea about this scenario? Commented Aug 2, 2018 at 0:31
  • 1
    I don't know about this, but I suggest you post a new question since yours is a slightly different problem.
    – whites11
    Commented Aug 2, 2018 at 8:11
  • 2
    @cweiske, were you able to solve this issue? I am slightly confused after reading your comment. Does it work if the mysql container is defined in both compose files? Commented Sep 18, 2018 at 3:21
7

If your container is only created from other compose file you can use the external links feature in docker-compose.

If you want both docker compose files to be able to create the mysql container, I suggest you look into Share Compose configurations between files and projects. In this case, you can create a base file that only defines mysql and extend/merge it in both apps compose files.

1
  • Sorry, I'm already using --no-recreate. I've updated the question.
    – cweiske
    Commented Aug 28, 2017 at 9:01
5

We solve it by using a third project including all the shared services our micro-services are using like mysql, kafka and redis.

in each one of our docker-compose files, we added a external links to those services.

Its a little bit dirty but it working well.

You can follow this issue on github, https://github.com/docker/compose/issues/2075 that is talking about the same issue you struggling with.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.