Docker-compose: sharing a container between multiple projects using the same container_name

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

Using docker-compose v3 files, I just 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 , but with app2 instead of app1 .

When starting docker-compose up --no-recreate for app2 I get an error message:

 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 split the MySQL container between multiple docker projects?

+4
source share
3 answers

We solve it using the third project, which includes all the common services that our microservices use, such as mysql, kafka and redis.

in each of our files containing dockers, we added external links to these services.

Its a little dirty, but it works well.

You can follow this issue on github, https://github.com/docker/compose/issues/2075 this speaks of the same issue you are struggling with.

+1
source

If your container was created only from another layout file, you can use external links in docker layout.

If you want docker files to be files to create the mysql container, I suggest you look at Split configuration between files and projects . In this case, you can create a base file that defines mysql and extends / merges it in both applications, creating files.

0
source

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

To do this, create a network:

docker network creates a common

Assign your network to the mysql container:

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

For any other container that needs mysql access, just add the same network definitions as above:

 version: '3' services: app1: ... networks: - shared app2: ... networks: - shared ... networks: shared: external: name: shared 
0
source

Source: https://habr.com/ru/post/1274725/


All Articles