docker-compose :缩放具有不同主机卷映射的容器
docker-compose : Scaling containers with distinct host volume map
- 在这里,我部署了 2 个带有 --scale 标志的容器
docker-compose up -d --scale gitlab-runner=2
2.Two 个容器正在部署,名称分别为 scalecontainer_gitlab-runner_1 和 scalecontainer_gitlab-runner_2
- 我想为每个容器映射不同的卷。
/srv/gitlab-runner/config_${DOCKER_SCALE_NUM}:/etc/gitlab-runner
- 出现此错误:
WARNING: The DOCKER_SCALE_NUM variable is not set. Defaulting to a blank string.
- 有什么办法,我可以为单独的容器映射不同的卷。
services:
gitlab-runner:
image: "gitlab/gitlab-runner:latest"
restart: unless-stopped
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- /srv/gitlab-runner/config_${DOCKER_SCALE_NUM}:/etc/gitlab-runner
version: "3.5"
我认为你做不到,有一个关于此 here 的公开请求。在这里,我将尝试描述一种获得您想要的东西的替代方法。
尝试从 link 到您想要的目录的容器中创建一个符号 link。您可以通过从 docker API 读取容器名称并获取最后一段来确定容器构建后的“编号”。为此,您必须将 docker 套接字安装到具有 big security implications.
的容器中
设置
这是一个获取容器编号的简单脚本(来源 Tony Guo)。
get-name.sh
DOCKERINFO=$(curl -s --unix-socket /run/docker.sock http://docker/containers/$HOSTNAME/json)
ID=$(python3 -c "import sys, json; print(json.loads(sys.argv[1])[\"Name\"].split(\"_\")[-1])" "$DOCKERINFO")
echo "$ID"
然后我们有一个简单的入口点文件,它获取容器编号,如果不存在则创建特定的配置目录,然后link将其特定的配置目录保存到已知位置(/etc/config
在这个例子中)。
entrypoint.sh
#!/bin/sh
# Get the number of this container
NAME=$(get-name)
CONFIG_DIR="/config/config_${NAME}"
# Create a config dir for this container if none exists
mkdir -p "$CONFIG_DIR"
# Create a sym link from a well known location to our individual config dir
ln -s "$CONFIG_DIR" /etc/config
exec "$@"
接下来我们有一个 Dockerfile 来构建我们的镜像,我们需要设置入口点并安装 curl
和 python
才能正常工作。同时复制我们的 get-name.sh
脚本。
Dockerfile
FROM alpine
COPY entrypoint.sh entrypoint.sh
COPY get-name.sh /usr/bin/get-name
RUN apk update && \
apk add \
curl \
python3 \
&& \
chmod +x entrypoint.sh /usr/bin/get-name
ENTRYPOINT ["/entrypoint.sh"]
最后,一个指定我们服务的简单组合文件。请注意,docker 套接字已安装,./config
也是我们不同的配置目录所在的位置。
docker-compose.yml
version: '3'
services:
app:
build: .
command: tail -f
volumes:
- /run/docker.sock:/run/docker.sock:ro
- ./config:/config
例子
# Start the stack
$ docker-compose up -d --scale app=3
Starting volume-per-scaled-container_app_1 ... done
Starting volume-per-scaled-container_app_2 ... done
Creating volume-per-scaled-container_app_3 ... done
# Check config directory on our host, 3 new directories were created.
$ ls config/
config_1 config_2 config_3
# Check the /etc/config directory in container 1, see that it links to the config_1 directory
$ docker exec volume-per-scaled-container_app_1 ls -l /etc/config
lrwxrwxrwx 1 root root 16 Jan 13 00:01 /etc/config -> /config/config_1
# Container 2
$ docker exec volume-per-scaled-container_app_2 ls -l /etc/config
lrwxrwxrwx 1 root root 16 Jan 13 00:01 /etc/config -> /config/config_2
# Container 3
$ docker exec volume-per-scaled-container_app_3 ls -l /etc/config
lrwxrwxrwx 1 root root 16 Jan 13 00:01 /etc/config -> /config/config_3
备注
- 我认为 gitlab/gitlab-runner 有自己的入口点文件,因此您可能需要链接它们。
- 您需要根据您的具体情况调整此示例 setup/locations。
- 在这里,我部署了 2 个带有 --scale 标志的容器
docker-compose up -d --scale gitlab-runner=2
2.Two 个容器正在部署,名称分别为 scalecontainer_gitlab-runner_1 和 scalecontainer_gitlab-runner_2
- 我想为每个容器映射不同的卷。
/srv/gitlab-runner/config_${DOCKER_SCALE_NUM}:/etc/gitlab-runner
- 出现此错误:
WARNING: The DOCKER_SCALE_NUM variable is not set. Defaulting to a blank string.
- 有什么办法,我可以为单独的容器映射不同的卷。
services:
gitlab-runner:
image: "gitlab/gitlab-runner:latest"
restart: unless-stopped
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- /srv/gitlab-runner/config_${DOCKER_SCALE_NUM}:/etc/gitlab-runner
version: "3.5"
我认为你做不到,有一个关于此 here 的公开请求。在这里,我将尝试描述一种获得您想要的东西的替代方法。
尝试从 link 到您想要的目录的容器中创建一个符号 link。您可以通过从 docker API 读取容器名称并获取最后一段来确定容器构建后的“编号”。为此,您必须将 docker 套接字安装到具有 big security implications.
的容器中设置
这是一个获取容器编号的简单脚本(来源 Tony Guo)。
get-name.sh
DOCKERINFO=$(curl -s --unix-socket /run/docker.sock http://docker/containers/$HOSTNAME/json)
ID=$(python3 -c "import sys, json; print(json.loads(sys.argv[1])[\"Name\"].split(\"_\")[-1])" "$DOCKERINFO")
echo "$ID"
然后我们有一个简单的入口点文件,它获取容器编号,如果不存在则创建特定的配置目录,然后link将其特定的配置目录保存到已知位置(/etc/config
在这个例子中)。
entrypoint.sh
#!/bin/sh
# Get the number of this container
NAME=$(get-name)
CONFIG_DIR="/config/config_${NAME}"
# Create a config dir for this container if none exists
mkdir -p "$CONFIG_DIR"
# Create a sym link from a well known location to our individual config dir
ln -s "$CONFIG_DIR" /etc/config
exec "$@"
接下来我们有一个 Dockerfile 来构建我们的镜像,我们需要设置入口点并安装 curl
和 python
才能正常工作。同时复制我们的 get-name.sh
脚本。
Dockerfile
FROM alpine
COPY entrypoint.sh entrypoint.sh
COPY get-name.sh /usr/bin/get-name
RUN apk update && \
apk add \
curl \
python3 \
&& \
chmod +x entrypoint.sh /usr/bin/get-name
ENTRYPOINT ["/entrypoint.sh"]
最后,一个指定我们服务的简单组合文件。请注意,docker 套接字已安装,./config
也是我们不同的配置目录所在的位置。
docker-compose.yml
version: '3'
services:
app:
build: .
command: tail -f
volumes:
- /run/docker.sock:/run/docker.sock:ro
- ./config:/config
例子
# Start the stack
$ docker-compose up -d --scale app=3
Starting volume-per-scaled-container_app_1 ... done
Starting volume-per-scaled-container_app_2 ... done
Creating volume-per-scaled-container_app_3 ... done
# Check config directory on our host, 3 new directories were created.
$ ls config/
config_1 config_2 config_3
# Check the /etc/config directory in container 1, see that it links to the config_1 directory
$ docker exec volume-per-scaled-container_app_1 ls -l /etc/config
lrwxrwxrwx 1 root root 16 Jan 13 00:01 /etc/config -> /config/config_1
# Container 2
$ docker exec volume-per-scaled-container_app_2 ls -l /etc/config
lrwxrwxrwx 1 root root 16 Jan 13 00:01 /etc/config -> /config/config_2
# Container 3
$ docker exec volume-per-scaled-container_app_3 ls -l /etc/config
lrwxrwxrwx 1 root root 16 Jan 13 00:01 /etc/config -> /config/config_3
备注
- 我认为 gitlab/gitlab-runner 有自己的入口点文件,因此您可能需要链接它们。
- 您需要根据您的具体情况调整此示例 setup/locations。