Docker-为不同项目重用docker-compose配置 [英] Docker - Reuse docker-compose configurations for different projects
问题描述
我有一个大型Docker项目,其中配置了 nginx,apache2,清漆,redis
的Dockerfile,并且在经过数周的更改和测试后运行良好。
I have a large Docker project with Dockerfiles for nginx, apache2, varnish, redis
configured and working well after weeks of changes and testing.
我现在处于将项目设置为使用docker-compose和override.yml文件进行简单设置的位置:
I am now at a point where I setup the projects to use docker-compose and override.yml files for easy setup:
我正在尝试使用相同的项目多个项目(网站)的docker-compose设置
正常启动(使用 docker-compose.yml和可选的docker-compose.override.yml
)
docker-compose up -d
自定义启动(使用特定的docker-compose文件)
docker-compose -f docker-compose.yml -f custom / docker-compose.website1.yml up -d
Custom startup (using specific docker-compose files)
docker-compose -f docker-compose.yml -f custom/docker-compose.website1.yml up -d
这两种方法都可以正常启动:
docker-compose ps
Both these methods starts up fine:
docker-compose ps
忽略他们是退出0的事实-我使用 docker-compose stop停止了它们,容器工作正常
nginx-proxy / usr / bin / supervisord出口0
红色is-cache / usr / bin / supervisord退出0
varnish-cache / usr / bin / supervisord退出0
web-server-apache2 / usr / bin / supervisord退出0
Ignore the fact that they are Exit 0 - I stopped them using docker-compose stop, the containers work fine
nginx-proxy /usr/bin/supervisord Exit 0
redis-cache /usr/bin/supervisord Exit 0
varnish-cache /usr/bin/supervisord Exit 0
web-server-apache2 /usr/bin/supervisord Exit 0
现在,我希望第二个项目(网站)使用相同的docker / docker-compose配置设置:
Now I want a second project (website) to use the same docker/docker-compose configuration setup:
docker -compose -f docker-compose.yml -f anothercustomfolder / docker-compose.website2.yml up -d
令我惊讶的是docker-compose 重新创建的容器,并且不创建一组新的容器:
To my surprise docker-compose recreated containers and do not create a new set of containers:
有关如何设置内容的信息,请参见当前设置部分。
Creating network "delete-network-frontend" with the default driver
Recreating nginx-proxy ... done
Recreating varnish-cache ... done
Recreating web-server ... done
Recreating redis-cache ... done
在第二个设置中运行 docker-compose ps
时文件夹:
When running docker-compose ps
in the second setup folder:
请注意,名称与上面的名称不同(这是第二个测试设置)
Name Command State Ports
------------------------------------------------------------------------------------------------
nginx-proxy-delete /usr/bin/supervisord Up 0.0.0.0:443->443/tcp,
0.0.0.0:80->80/tcp
redis-cache-delete /usr/bin/supervisord Up 0.0.0.0:6379->6379/tcp
varnish-cache-delete /usr/bin/supervisord Up 0.0.0.0:6081->6081/tcp,
0.0.0.0:6082->6082/tcp
web-server- /usr/bin/supervisord Up 0.0.0.0:8080->8080/tcp
apache2-delete
看来docker-compose做了两件事:1.重新创建(替换)项目1容器,使用项目1容器名称来表示它们是重新创建,然后2.删除项目1容器,将其重命名为项目2容器。
It appears docker-compose did two things : 1. Recreate (replace) the project 1 containers, used the project 1 container names to mention that they were 'recreated', and 2. Remove the project 1 containers, renamed it to project 2 containers.
- 我创建了一个完整的Dockerfile项目,该项目配置了docker-compose.yml和两个覆盖的docker-compose文件(
docker-compose.website1.yml和
docker-compose.website2.yml `)。 - 我为工作中的Dockerfile / docker-compose.yml项目制作了完整副本,并创建了一个新文件夹:换句话说,这两个都将使用相同的docker设置,但使用不同的docker-compose.yml覆盖文件。
/ var / www / docker / site1
/ var / www / docker / site2
- I created a full Dockerfile project configured with docker-compose.yml and two override docker-compose files (
docker-compose.website1.yml and
docker-compose.website2.yml`). - I made a complete copy of the working Dockerfile / docker-compose.yml project and created a new folder: In other words both these will use the same docker setup but use different docker-compose.yml override files.
/var/www/docker/site1
/var/www/docker/site2
TLDR:如何使用有效的 docker-compose
项目位于同一主机操作系统上的多个项目...不替换其他项目的容器。
TLDR: How do I use a working docker-compose
project on the same host operating system for multiple projects... without it replacing another project's containers.
我希望能够同时看到(同时使用)时间,例如能够看到:
忽略端口在此处相同的事实,我知道它们不会同时运行,我将更新项目docker-compose .yml自定义文件
I want to be able to see (use both) at the same time, and for instance be able to see this: Ignore the fact that the ports are the same here, I am aware they won't run at the same time, I will update the project docker-compose.yml custom files when this works
docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------------
nginx-proxy /usr/bin/supervisord Up 0.0.0.0:443->443/tcp,
0.0.0.0:80->80/tcp
redis-cache /usr/bin/supervisord Up 0.0.0.0:6379->6379/tcp
varnish-cache /usr/bin/supervisord Up 0.0.0.0:6081->6081/tcp,
0.0.0.0:6082->6082/tcp
web-server- /usr/bin/supervisord Up 0.0.0.0:8080->8080/tcp
apache2
nginx-proxy /usr/bin/supervisord Up 0.0.0.0:443->443/tcp,
0.0.0.0:80->80/tcp
redis-cache-delete /usr/bin/supervisord Up 0.0.0.0:6379->6379/tcp
varnish-cache-delete /usr/bin/supervisord Up 0.0.0.0:6081->6081/tcp,
0.0.0.0:6082->6082/tcp
web-server- /usr/bin/supervisord Up 0.0.0.0:8080->8080/tcp
apache2-delete
如果有人问:为什么不放将网站放入同一个(一个)容器中?
对于有人可能会问到的可能性,我知道我可以将多个网站添加到 / etc / apache2 /启用了站点的
(或nginx),并在每个站点的Dockerfile中使用 ADD
添加自定义配置文件,但是我无法使用该方法进行测试不同的轻微设置。
If anyone asks: Why not just put the websites into the same (one) container??
For the possibility someone might ask this, I know I can add multiple websites into the /etc/apache2/sites-enabled
(or nginx) and add custom configuration files using ADD
in Dockerfile for each site, but using that method I cannot test different slight setups.
不同的设置可以通过引用替代docker-组成文件中的另一个不同的图像来使用。
例如,我可以创建一个Dockerfile,该文件安装所需的所有php7.3库并在其上运行Magento 2.3 ,然后使用另一个Dockerfile 测试php7.4,并还有一个可以在PHP5.6安装上运行较旧的Magento 1站点,依此类推。
For instance I can create a Dockerfile that installs all php7.3 libraries required and run Magento 2.3 on it, then have another Dockerfile to test php7.4, and have another to run an older Magento 1 site on a PHP5.6 installation and so on.
推荐答案
感谢来自 David Maze 的建议,我在配置docker-compose安装程序以与多个项目一起工作方面更加费劲。
Thanks to advice from David Maze, I struggled further with configuring the docker-compose setup to work with multiple projects.
基于docker-compos的信息e v1.25.0(2020年7月)
当您要重复使用(持久)容器(启动/停止而不是仅使用容器)时,此讨论特别重要向上/向下-删除)
最初在我的问题中指出-如果尝试使用 docker-compose up -d 创建容器code>该工具目前尚不支持某些陷阱。
As initially pointed out in my question - if you try to create containers using docker-compose up -d
there are some pitfalls which the tool simply does not support right at the moment.
当前点数DOCKER-COMPOSE实施:
- 如果您仅使用覆盖的
docker-compose * .yml
具有不同的容器名称(每个项目),并且文件位于 相同文件夹
- If you just use overridden
docker-compose*.yml
with different container_names (per 'project') with files in the same folder
docker-compose up
只会替换我的问题中说明的现有容器。
- 您可以执行以下操作:
docker-compose -p CUSTOM_PROJECT_NAME -f file1 .yml -f file2 .yml up -d
,但是:
- You can do the following:
docker-compose -p CUSTOM_PROJECT_NAME -f file1.yml -f file2.yml up -d
, but:
这本身没用-这些容器将仅在您要停止它们之前起作用。 一旦您要执行
docker-compose start
(重新启动现有容器集),便会开始工作。 只会失败,并出现错误:无容器可启动
This on its own is useless - these containers will only work until you want stop them. As soon as you want to do
docker-compose start
(to restart existing container set) it will simply fail withError: No containers to start
- 如果您使用两个不同的文件夹使用相同的docker-compose项目(即克隆的项目):例如
./dc-project1
./ dc-project2
,但在<$内使用container_name
字段c $ c> docker-compose。*。yml文件:
- If you use two different folders with the same docker-compose project (ie cloned project): for instance
./dc-project1
./dc-project2
but usingcontainer_name
field insidedocker-compose.*.yml file
:
何时您尝试在
内运行
。/dc-project1 和docker-compose -f f1.yml -f f2.yml up -d
./ dc-project2
文件夹中的内容相同,您将收到以下错误:您必须删除(或重命名) )能够重复使用的容器帽子名称
。
When you try to run
docker-compose -f f1.yml -f f2.yml up -d
inside./dc-project1
and the same inside./dc-project2
folder, you will get the following error:You have to remove (or rename) that container to be able to reuse that name
.
- Docker网络的类似问题使用覆盖文件时,将在docker-compose中发生:
删除了大多数自定义设置,以使网络设置更加清晰:
- Similar issues with your Docker network will occur with docker-compose when you use overridden files: Removed most of the custom settings to make the network setting clearer:
网络将从
docker-compose up
中的覆盖文件正确附加,但如果您想docker-compose start
,它将在默认docker中寻找您的默认网络名称: -compose.yml
或什至docker-compose.override.yml
文件(如果存在)。换句话说-它会忽略您的自定义docker-compose覆盖文件(请参见以下示例):
Network will be attached correctly from your overridden file on
docker-compose up
, but as soon as you want todocker-compose start
it looks for your default network name: in the defaultdocker-compose.yml
or even thedocker-compose.override.yml
file if it exists. In other words - it ignores your custom docker-compose override files (see example below):
docker-compose .yml
:
docker-compose.yml
:
networks:
network_frontend:
name: stage6-network-frontend
customfolder / docker-compose.custom.yml
:
customfolder/docker-compose.custom.yml
:
networks:
network_frontend:
name: magento2.3-network-frontend
解决方案
示例
目标:以使 docker-compose开始/停止
可以使用同一个docker-compose项目与多个设置(又称为项目/网站/工具)正确工作。
SOLUTION
Example
Objective : to get docker-compose start/stop
to work correctly with multiple setups (aka projects/websites/tools)using the same docker-compose project.
假设您具有以下docker-compose文件:
Suppose you have the following docker-compose files:
**主文件:** docker-compose.yml
:
**Main file: ** docker-compose.yml
:
web_server:
image: current_timezone/full-supervisord-web-server/php7.3:1.00
container_name: web-server-apache2
networks:
- network_frontend
build:
context: "./all-services/"
dockerfile: ./web-server/Dockerfile.webserver.apache2
args:
volumes:
- website_data:/var/www/html
ports:
- "8080:8080"
networks:
network_frontend:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.100.0.0/16
name: stage6-network-frontend
driver_opts:
# Custom name for host-side network : for instance on Ubuntu : ip addr | ifconfig
com.docker.network.bridge.name: docker-custom # Seems limit of 15 characters only
然后是替代文件: customfolder / magento2.override.yml
:
web_server:
container_name: web-server-apache2-magento2.3.5
networks:
- network_frontend
build:
args:
volumes:
- website_data:/var/www/html
ports:
- "8080:8080"
networks:
network_frontend:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.100.0.0/16
driver_opts:
# Custom name for host-side network : for instance on Ubuntu : ip addr | ifconfig
com.docker.network.bridge.name: d-glo-femag2_35 # Seems limit of 15 characters only
name: glo-magento2.3-network-frontend
执行以下操作:
-
复制完整的Docker项目(将Dockerfiles / ADDs / docker-compose.yml文件等)放入新的单独文件夹中:
/ var / docker / project1
/ var / docker / project2
请确保在您的替代docker-compose.yml 中的 container_name
条目在两个项目之间是唯一的。
Make sure that the container_name
entries in your override docker-compose.yml are unique between the two projects.
在项目 project1
中运行 docker-compose -f docker-compose.yml -f customfolder / magento2。 Override.yml up -d
&& docker-compose stop
,导航到 project2
并执行相同的操作。
In project project1
run docker-compose -f docker-compose.yml -f customfolder/magento2.override.yml up -d
&& docker-compose stop
, navigate to project2
and do the same.
使用 -p
标志,因为David Maze建议不能单独使用,JSON文件是仍然在 docker-compose start / stop
Using -p
flag as David Maze suggested does not work on its own, JSON files are still sourced as ./foldername
on docker-compose start/stop
- 由于网络在
启动/停止
上存在类似问题,因此您可以正确使用您在覆盖文件中定义的自定义名称,....不幸的是,您需要将主库docker-compose.yml
更新为覆盖的文件!
- Since networks are having similar issues on
start/stop
, before you can correctly use your custom name defined in your override file, .... unfortunately you need to update the main basedocker-compose.yml
to the overridden file!
扩展说明:无法从
调用正确的自定义网络名称docker-compose start
,因此,由于docker-compose忽略start
上的覆盖文件,因此您需要确保更新基本文件docker-compose.yml
或docker-compose.override.yml
具有您的自定义网络名称。
Extended explanation: There is no way to call the correct custom network name from
docker-compose start
, so since docker-compose ignores the overridden files onstart
, you need to make sure to update the base filedocker-compose.yml
ordocker-compose.override.yml
has your custom network name.
如果在使用 up -d
之前尚未更新名称,则需要替换每个 / var / lib / docker / containers / * / config.v2.json
的内容。
In case you have not updated the names before using up -d
, you will need to replace the content of each /var/lib/docker/containers/*/config.v2.json
.
例如,您可以执行此操作:您必须先停止docker
For example you could do this: you have to stop docker first
sudo service docker stop
find /var/lib/docker/containers/ -type f -name "config.v2.json" -exec sed -i "s|wrong-network-name|overridden-network-name|g" '{}' \;
sudo service docker start
- 如果操作正确,则应该具有唯一的容器名称,并且可以正确访问每个文件夹,而不会破坏彼此的容器:
docker-compose start
,docker-compose stop
,docker-compose ps
- IF done correctly, you should have unique container names, and each folder can be accessed separately correctly now without it breaking each other's containers:
docker-compose start
,docker-compose stop
,docker-compose ps
注意:您仍然需要导航到单独的文件夹来运行这些命令
这篇关于Docker-为不同项目重用docker-compose配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!