簡體   English   中英

docker-compose:縮放具有不同主機卷的容器 map

[英]docker-compose : Scaling containers with distinct host volume map

  1. 在這里,我部署了 2 個帶有 --scale 標志的容器
docker-compose up -d --scale gitlab-runner=2

2. 正在部署兩個容器,名稱分別為 scalecontainer_gitlab-runner_1 和 scalecontainer_gitlab-runner_2。

  1. 我想為每個容器 map 不同的體積。
/srv/gitlab-runner/config_${DOCKER_SCALE_NUM}:/etc/gitlab-runner
  1. 收到此錯誤:
WARNING: The DOCKER_SCALE_NUM variable is not set. Defaulting to a blank string.
  1. 有什么辦法,我可以為單獨的容器 map 不同的體積。
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"

我不認為你可以,這里有一個公開的請求。 在這里,我將嘗試描述一種獲得您想要的東西的替代方法。

嘗試從鏈接到所需目錄的容器中創建符號鏈接。 您可以通過從 docker API 讀取容器名稱並取最后一段來確定容器的“編號”。 為此,您必須將 docker 套接字安裝到容器中,這具有很大的安全隱患

設置

這是一個獲取容器編號的簡單腳本(Credit 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"

然后我們有一個簡單的入口點文件,它獲取容器編號,如果不存在則創建特定的配置目錄,並將其特定的配置目錄鏈接到已知位置(本例中的/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 來構建我們的鏡像,我們需要設置入口點並安裝curlpython以使其工作。 同時復制我們的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"]

最后,一個簡單的 compose 文件指定我們的服務。 請注意,安裝了 docker 套接字以及./config ,這是我們不同的配置目錄 go 的位置。

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 有自己的入口點文件,因此您可能需要鏈接它們。
  • 您需要根據您的特定設置/位置調整此示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM