LoginSignup
0
3

More than 3 years have passed since last update.

Docker を使用するための最初の設定

Last updated at Posted at 2019-12-08

手元で空いている端末が SD カードの容量が少ない Raspberry Pi 3 B+ (以下 Raspberry Pi)だけだったので、別途 Ubuntu サーバ上に Docker CE をインストールしておき、 Raspberry Pi には docker-ce-cli だけをインストールして、コンテナを作成できるようにします

Ubuntu サーバ上での設定

Ubuntu サーバへは Raspberry Pi から SSH でログインできるように事前に準備しておきます

/etc/systemd/system/docker.service.d/override.conf を作成して、--data-root 等の環境にあった設定をしておきます

docker-ce-cli は Raspberry Pi 上で実行しますが、ここでは -H tcp://0.0.0.0:2375 のように直接外部からアクセスするための -H オプションの指定は不要です


$ cat /etc/systemd/system/docker.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// --data-root /var/lib/docker

後は、設定を反映させて docker.service を再起動しておきます


$ systemctl daemon-reload 
$ systemctl restart docker.service

Raspberry Pi 上での設定

docker-ce-cli の deb を取得して、インストールします


$ curl -LO https://download.docker.com/linux/raspbian/dists/buster/pool/stable/armhf/docker-ce-cli_19.03.5~3-0~raspbian-buster_armhf.deb
$ sudo dpkg -i docker-ce-cli_19.03.5~3-0~raspbian-buster_armhf.deb

これで docker-ce-cli は使用できるようになりましたが、Raspberry Pi 上に Docker daemon はいないためこのままでは使用できません


$ docker info
Client:
 Debug Mode: false

Server:
ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
errors pretty printing info

そのため、docker context でエンドポイントが Ubuntu サーバのコンテキストを作成して、そのコンテキストに切り替えます


$ docker context create --docker "host=ssh://hexa@192.168.1.2" main
main
Successfully created context "main"
$ docker context use main
main
Current context is now "main"

接続してコンテナを作成できるか確認してみます


$ docker container run --rm -it centos:7 bash -c 'cat /etc/redhat-release'
CentOS Linux release 7.7.1908 (Core)

接続できました

コンテキストは複数作成しておけますし、環境に合わせて簡単に切り替えが可能なため、覚えておくととても便利です


$ docker context use sub
sub
Current context is now "sub"
$ docker context ls
dNAME                DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT   ORCHESTRATOR
default             Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                         swarm
main                                                          ssh://hexa@192.168.1.2
sub *                                                         ssh://ubuntu@192.168.2.2
0
3
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
0
3