docker-compose:使用相同的container_name在多个项目之间共享容器 [英] docker-compose: Sharing container between multiple projects by using the same container_name
问题描述
我想对同一服务器上运行的多个项目使用MySQL docker容器。
使用docker-compose v3文件,我只是拥有相同的mysql容器配置在每个项目中,它们都具有相同的 container_name
:
版本: 3
服务:
app1:
图片:foo
链接:
-mysql
mysql:
图像:mysql / mysql:5.7
container_name:shared_mysql
第二个应用程序具有类似的 docker-compose.yml
文件,但具有 app2
而不是 app1
。
运行 docker-compose up --no-recreate
时, app2
,出现错误:
创建shared_mysql ...错误
错误:对于shared_mysql
无法为服务mysql创建容器:冲突。
容器 deadbeef已使用容器名称 / shared_mysql。
必须删除(或重命名)该容器才能重用该名称。
我该怎么做才能在多个Docker项目之间共享MySQL容器?
您可以简单地避免在两个docker-compose.yml文件之一中重新定义mysql,并将mysql和其他容器连接到同一网络。 p>
为此,创建一个网络:
docker network create共享
将网络分配给mysql容器:
版本:'3'
服务:
mysql:
...
网络:
-共享
网络:
共享:
外部:
名称:共享
对于需要访问mysql的任何其他容器,只需添加与上述相同的网络定义即可:
version:'3 '
服务:
app1:
...
网络:
-共享
app2:
...
网络:
-sha红色
...
网络:
共享:
外部:
名称:共享
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?
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
这篇关于docker-compose:使用相同的container_name在多个项目之间共享容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!