データが主食

データエンジニアの備忘録。分析だったり、読んだ本のメモだったり。

Dockerサーバー構築

つまり

EC2(ubuntuインスタンス)上でDockerデーモンを起動しておいて、クライアントから利用する方法を調べた時のメモです。

f:id:ktr89:20200425152936p:plain

dockerインストール

sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -g docker ubuntu
docker --version
Docker version 19.03.6, build 369ce74a3c

2375ポート経由で利用できるようにする

dockerd | Docker Documentation

/etc/docker/daemon.json に下記のようなjsonファイルを作ることで設定できそうに見えます。

{
    "hosts": [
        "tcp://0.0.0.0:2375"
    ]
}

これでは、dockerdの起動時に怒られます。

$ sudo systemctl restart docker
Job for docker.service failed because the control process exited with error code.
See "systemctl status docker.service" and "journalctl -xe" for details.
ubuntu@ip-172-31-31-162:~$ journalctl -xe
Apr 25 06:36:22 ip-172-31-31-162 systemd[1]: docker.service: Scheduled restart job, restart counter is at 3.
-- Subject: Automatic restarting of a unit has been scheduled
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Automatic restarting of the unit docker.service has been scheduled, as the result for
-- the configured Restart= setting for the unit.
Apr 25 06:36:22 ip-172-31-31-162 systemd[1]: Stopped Docker Application Container Engine.
-- Subject: Unit docker.service has finished shutting down
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit docker.service has finished shutting down.
Apr 25 06:36:22 ip-172-31-31-162 systemd[1]: Closed Docker Socket for the API.
-- Subject: Unit docker.socket has finished shutting down
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit docker.socket has finished shutting down.
Apr 25 06:36:22 ip-172-31-31-162 systemd[1]: Stopping Docker Socket for the API.
-- Subject: Unit docker.socket has begun shutting down
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit docker.socket has begun shutting down.
Apr 25 06:36:22 ip-172-31-31-162 systemd[1]: Starting Docker Socket for the API.
-- Subject: Unit docker.socket has begun start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit docker.socket has begun starting up.
Apr 25 06:36:22 ip-172-31-31-162 systemd[1]: Listening on Docker Socket for the API.
-- Subject: Unit docker.socket has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit docker.socket has finished starting up.
--
-- The start-up result is RESULT.
Apr 25 06:36:22 ip-172-31-31-162 systemd[1]: docker.service: Start request repeated too quickly.
Apr 25 06:36:22 ip-172-31-31-162 systemd[1]: docker.service: Failed with result 'exit-code'.
Apr 25 06:36:22 ip-172-31-31-162 systemd[1]: Failed to start Docker Application Container Engine.
-- Subject: Unit docker.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit docker.service has failed.
--
-- The result is RESULT.

原因と解決策

ubuntu@ip-172-31-31-162:~$ sudo systemctl status docker |grep Loaded
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)

/lib/systemd/system/docker.service を開いてみると

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

ここに -H オプションがあるのが原因のようです。

ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 --containerd=/run/containerd/containerd.sock

のように直接書き換えることで、TCP経由でdockerサーバーを利用できるようになります。

書き換え後は下記のコマンドを実行してください。

sudo systemctl daemon-reload

クライアントからの使い方

DOCKER_HOST='tcp://{EC2_IP}:2375' docker build .

みたいな感じで環境変数 DOCKER_HOST を設定することで、dockerサーバーを利用できるようになります。