docker-compose 运行多个任务而不共享依赖项



情况:我有一个selenium app(在python中(,它将自己连接到网站上的一个帐户,以便下载多个CSV文件。 要运行它,我使用docker(和docker-compose(这是我docker-compose.yml文件

version: '3'
services:
selenium:
build:
context: .
dockerfile: compose/selenium/Dockerfile
ports:
- "4444:4444"
volumes:
- /dev/shm:/dev/shm
- download-folder:/home/seluser/downloads
enma:
build:
context: .
dockerfile: compose/enma_daio/Dockerfile
depends_on:
- selenium
volumes:
- download-folder:/data/selenium-downloads
env_file:
- .env
restart: always
volumes:
download-folder:

我的selenium's Dockerfile只是使用官方硒 docker 映像创建downloads文件夹的一种方式

FROM selenium/standalone-chrome
RUN mkdir -p /home/seluser/downloads

为了运行我的任务,我使用:

docker-compose run -d enma daio arg0 arg1 arg2

顺便说一下,我还使用了一个 entrypoint.sh:

#!/bin/bash
set -e
cd /app
# Selenium takes a bit of time before being up so we wait until we can reach it
function selenium_ready(){
curl selenium:4444 &>/dev/null
}
until selenium_ready; do
>&2 echo "Waiting for selenium..."
sleep 1
done

if [ "$1" = 'daio' ]; then
shift
exec python enma.py $@
fi
exec "$@"

问题:当我同时运行多个实例(在同一网站上的不同帐户上(时,它们共享same selenium container,因此same volume。所有下载的文件都混合在一起,我不知道哪个文件来自哪个run

我想做什么:我想在每次运行新任务时创建另一个selenium container。或者找到另一种方式来使用不同的音量。

这听起来像你应该在执行docker-compose run时将--project-namep标志传递给docker-compose。

默认情况下,docker-compose 会根据您的项目名称创建卷和容器名称,默认使用当前目录的名称。因此,在您的情况下,您将有一个卷名<cwd>_download-folder.容器名称为<cwd>_selenium<cwd>_enma.

如果要在每个docker-compose run上创建新卷和新selenium容器,只需覆盖其项目名称即可。

所以如果你这样做

$ docker-compose -p name1 run -d enma daio arg0 arg1 arg2

$ docker-compose -p name2 run -d enma daio arg0 arg1 arg2

您最终将获得两个创建的卷和四个容器。这似乎适合您的需求,这将消除enma容器共享同一卷。

仅供参考,您可以查看通过运行docker volume ls创建了哪些卷。

希望这有帮助。

最新更新