4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Dockerで特定のコンテナサイズを自由に拡張する設定する devicemapper編

Last updated at Posted at 2019-03-26

どうも、はにおかさいです。

Dockerでおのおののコンテナの容量を設定したいと思うことはありませんか?
無尽蔵に使われても困ります。逆に不足しても困ります。
linux quotaでフォルダに制限をかけるのはスマートとは言えないやり方。dockerでなんかうまくリソース割当できないかな、、

ですので、ぐぐりました。が、日本語の情報は皆無、英語もあまりなく。
なんとか見つけた情報をまとめます。

#やりかた
作成時のコマンドはこれだけ。 --storage-opt size= という呪文。
docker create -it --storage-opt size=5G debian /bin/bash

#でも...

docker_info
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true

この環境だと...

--storage-opt is supported only for overlay over xfs with 'pquota' mount option

Quotas are support on btrfs, overlay2+xfs, and and devicemapper. 引用元

ext4+overlayfsではだめなよう。
変更しよう。

#ストレージ・ドライバの変更
クリーンである必要がありそう。今までのデータは消えると思う。

cloud-configをつくる
core20190325 core # vi cloud-config.yml
#cloud-config
coreos:
  units:
    - name: docker.service
      drop-ins:
        - name: 10-devicemapper-storage-driver.conf
          content: |
            [Service]
            Environment="DOCKER_OPTS=--storage-driver=devicemapper"
      command: start
読み込む!
core20190325 core # coreos-cloudinit -from-file=./cloud-config.yml
2019/03/26 06:59:59 Checking availability of "local-file"
2019/03/26 06:59:59 Fetching user-data from datasource of type "local-file"
2019/03/26 06:59:59 Fetching meta-data from datasource of type "local-file"
2019/03/26 06:59:59 Parsing user-data as cloud-config
2019/03/26 06:59:59 Merging cloud-config from meta-data and user-data
2019/03/26 06:59:59 Writing drop-in unit "10-devicemapper-storage-driver.conf" to filesystem
2019/03/26 06:59:59 Writing file to "/etc/systemd/system/docker.service.d/10-devicemapper-storage-driver.conf"
2019/03/26 06:59:59 Wrote file to "/etc/systemd/system/docker.service.d/10-devicemapper-storage-driver.conf"
2019/03/26 06:59:59 Wrote drop-in unit "10-devicemapper-storage-driver.conf"
2019/03/26 06:59:59 Ensuring runtime unit file "etcd.service" is unmasked
2019/03/26 06:59:59 Ensuring runtime unit file "etcd2.service" is unmasked
2019/03/26 06:59:59 Ensuring runtime unit file "fleet.service" is unmasked
2019/03/26 06:59:59 Ensuring runtime unit file "locksmithd.service" is unmasked
2019/03/26 06:59:59 Calling unit command "start" on "docker.service"
2019/03/26 06:59:59 Result of "start" on "docker.service": done
restart
core20190325 core # systemctl restart docker
check
core20190325 core # docker info
・・・
Server Version: 18.06.1-ce
Storage Driver: devicemapper
 Pool Name: docker-8:9-7828170-pool
 Pool Blocksize: 65.54kB
 Base Device Size: 10.74GB
 Backing Filesystem: xfs
 Udev Sync Supported: true
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Data Space Used: 11.8MB
・・・・
Kernel Version: 4.19.25-coreos
Operating System: Container Linux by CoreOS 2023.5.0 (Rhyolite)
・・・・

WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use.
         Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.

c.f.警告について(https://qiita.com/ngyuki/items/a6ef879a285573efd59f)

#試す

testrun
core20190325 core #  docker run --storage-opt size=5g -t -i ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
898c46f3b1a1: Pull complete 
63366dfa0a50: Pull complete 
041d4cd74a92: Pull complete 
6e1bee0f8701: Pull complete 
Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
Status: Downloaded newer image for ubuntu:latest
/run/torcx/bin/docker: Error response from daemon: devmapper: Container size cannot be smaller than 10.74GB.
See '/run/torcx/bin/docker run --help'.

注意が必要なのは、dockerのbase size以上の大きさ(デフォルト値は10GBほど)しか指定できないということです。それ以下の場合はbase sizeをあらかじめ小さくしておく必要があります。

再挑戦
core20190325 core #  docker run --storage-opt size=20g -t -i ubuntu /bin/bash
root@a6e9cb6bb256:/# 
root@a6e9cb6bb256:/# df -h
Filesystem                                                                                       Size  Used Avail Use% Mounted on
/dev/mapper/docker-8:9-7828170-0b1691fd49ededc7317c29a26f6b0c217e423e6ff2d176ab217ee3bd5facd592   20G  130M   20G   1% /
・・・

うまくいきました。

チェック
core20190325 core # docker inspect a6e9cb6bb256
[
    {
        "Id": "a6e9

            "StorageOpt": {
                "size": "20g"
            },

設定上でも20gとなっています

#まとめ
dockerでも容量制限を伴う運用が簡単にできるようになりました。
これを使えば、低オーバーヘッドな仮想マシンを構築することもできそうです。OpenVZの代わりに。

#Ref

#See Also

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?