我有两个 docker 容器,A 和 B,它们是通过 docker-compose 文件启动的。我希望能够使用容器 A 中的“docker-compose -f docker-compose.B.yaml down”命令来关闭容器 B。
这可能吗?
我尝试挂载容器 B 的 docker compose 文件和
/var/run/docker.sock
。但是当我使用 python 中的子进程(再次从容器 A 内)运行命令时,我收到“未找到资源错误”。
码头工人:
services:
Service A:
image: imageA:rel_1.0.0
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /path/to/docker-compose.B.yaml:/projectA/docker-compose.B.yaml
.
.
.
蟒蛇:
#docker-compose.py
import subprocess
compose_file_path = '/projectA_directory/docker-compose.B.yaml'
command = f"docker-compose -f {compose_file_path} down"
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
logger.info(f"Command output: {result.stdout}")
logger.error(f"Command error: {result.stderr}")
错误:
ContainerA | ERROR | 2024-07-29T19:56:51-0600 | docker-compose.py:62 | Command error: Warning: No resource found to remove for project "projectA".
Docker compose 为您的容器分配一个“项目名称”,默认情况下,该名称基于包含您的
compose.yaml
文件的目录名称。也就是说,如果有:
myproject/
compose.yaml
并且
compose.yaml
看起来像这样:
services:
serviceA:
image: docker.io/alpinelinux/darkhttpd
serviceB:
image: docker.io/alpinelinux/darkhttpd
然后当我从
docker compose up
目录中运行 myproject
时:
cd myproject
docker compose up -d
我最终得到以下容器:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f1c8998b15f alpinelinux/darkhttpd "darkhttpd /var/www/…" 19 seconds ago Up 18 seconds 8080/tcp myproject-serviceA-1
05c2ee47abd5 alpinelinux/darkhttpd "darkhttpd /var/www/…" 19 seconds ago Up 18 seconds 8080/tcp myproject-serviceB-1
每个容器都分配有一些标签:
$ docker container inspect myproject-serviceA-1 | jq '.[0].Config.Labels'
{
"com.docker.compose.config-hash": "d1e91247ae0afe7dc2cb0ce473930c5c59d82e5089711008808219feaf90fdfc",
"com.docker.compose.container-number": "1",
"com.docker.compose.depends_on": "",
"com.docker.compose.image": "sha256:a8b7188f53f3112dbe425a0eae4074d46d47e5c08380b918ba211b6b2fb30218",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "myproject",
"com.docker.compose.project.config_files": "/home/lars/tmp/containers/myproject/compose.yaml",
"com.docker.compose.project.working_dir": "/home/lars/tmp/containers/myproject",
"com.docker.compose.service": "serviceA",
"com.docker.compose.version": "2.29.1"
}
com.docker.compose.project
标签是 docker compose 识别属于当前项目的容器的方式。如果您重命名目录,如下所示:
cd ..
mv myproject bobsproject
cd bobsproject
那么 compose 将会有错误的项目名称,并且它将无法与这些容器交互:
$ docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
$ docker compose down
我们可以使用
--project-name
(-p
) 命令行选项设置明确的项目名称:
$ docker compose -p myproject ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
myproject-serviceA-1 docker.io/alpinelinux/darkhttpd "darkhttpd /var/www/…" serviceA 5 minutes ago Up 5 minutes 8080/tcp
myproject-serviceB-1 docker.io/alpinelinux/darkhttpd "darkhttpd /var/www/…" serviceB 5 minutes ago Up 5 minutes 8080/tcp
或者通过设置
COMPOSE_PROJECT_NAME
环境变量:
$ COMPOSE_PROJECT_NAME=myproject docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
myproject-serviceA-1 docker.io/alpinelinux/darkhttpd "darkhttpd /var/www/…" serviceA 7 minutes ago Up 7 minutes 8080/tcp
myproject-serviceB-1 docker.io/alpinelinux/darkhttpd "darkhttpd /var/www/…" serviceB 7 minutes ago Up 7 minutes 8080/tcp
在您的问题中,您似乎正在将您的撰写项目安装在容器中与主机上使用的目录名称不同的目录名称下。您可以通过以下任一方式解决此问题:
--project-name
参数,或者COMPOSE_PROJECT_NAME
环境变量