ubuntu docker インストール 入手方法と導入手順

Ubuntuのdockerインストール手順

Ubuntuのdockerインストール手順
Ubuntuでは、dockerコマンドをインストールする手順は以下の通りです。

参考)Get Docker Engine – Community for Ubuntu | Docker Documentation

$ sudo apt-get remove docker docker-engine docker.io containerd runc
$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo apt-key fingerprint 0EBFCD88
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

実行ログは以下の通り。

$ sudo apt-get remove docker docker-engine docker.io containerd runc
Reading package lists... Done
Building dependency tree
Reading state information... Done

No apt package "docker", but there is a snap with that name.
Try "snap install docker"

E: Unable to locate package docker
E: Unable to locate package docker-engine
E: Unable to locate package docker.io
E: Couldn't find any package by glob 'docker.io'
E: Couldn't find any package by regex 'docker.io'
E: Unable to locate package containerd
E: Unable to locate package runc
# 旧バージョンはインストールされていなかったので、エラーになっています。
$ sudo apt-get update
Hit:1 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:4 http://archive.canonical.com/ubuntu bionic InRelease [10.2 kB]
Get:5 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:6 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [8570 kB]
Get:7 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/universe Translation-en [4941 kB]
Get:8 http://archive.canonical.com/ubuntu bionic/partner amd64 Packages [2316 B]
Get:9 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [151 kB]
Get:10 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/multiverse Translation-en [108 kB]
Get:11 http://archive.canonical.com/ubuntu bionic/partner Translation-en [1276 B]
Get:12 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [799 kB]
Get:13 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/main Translation-en [283 kB]
Get:14 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [21.9 kB]
Get:15 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/restricted Translation-en [6128 B]
Get:16 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1028 kB]
Get:17 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/universe Translation-en [317 kB]
Get:18 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [8520 B]
Get:19 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/multiverse Translation-en [4044 B]
Get:20 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [2512 B]
Get:21 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports/main Translation-en [1644 B]
Get:22 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [4028 B]
Get:23 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports/universe Translation-en [1856 B]
Get:24 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [575 kB]
Get:25 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [190 kB]
Get:26 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [13.2 kB]
Get:27 http://security.ubuntu.com/ubuntu bionic-security/restricted Translation-en [4292 B]
Get:28 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [622 kB]
Get:29 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [208 kB]
Get:30 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [5904 B]
Get:31 http://security.ubuntu.com/ubuntu bionic-security/multiverse Translation-en [2568 B]
Fetched 18.1 MB in 4s (4607 kB/s)
Reading package lists... Done
$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
ca-certificates is already the newest version (20180409).
ca-certificates set to manually installed.
curl is already the newest version (7.58.0-2ubuntu3.8).
curl set to manually installed.
software-properties-common is already the newest version (0.96.24.32.11).
software-properties-common set to manually installed.
The following packages were automatically installed and are no longer required:
grub-pc-bin libnuma1
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
apt-transport-https gnupg-agent
0 upgraded, 2 newly installed, 0 to remove and 30 not upgraded.
Need to get 6572 B of archives.
After this operation, 196 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 apt-transport-https all 1.6.12 [1692 B]
Get:2 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 gnupg-agent all 2.2.4-1ubuntu1.2 [4880 B]
Fetched 6572 B in 0s (143 kB/s)
Selecting previously unselected package apt-transport-https.
(Reading database ... 60083 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_1.6.12_all.deb ...
Unpacking apt-transport-https (1.6.12) ...
Selecting previously unselected package gnupg-agent.
Preparing to unpack .../gnupg-agent_2.2.4-1ubuntu1.2_all.deb ...
Unpacking gnupg-agent (2.2.4-1ubuntu1.2) ...
Setting up apt-transport-https (1.6.12) ...
Setting up gnupg-agent (2.2.4-1ubuntu1.2) ...
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK
$ sudo apt-key fingerprint 0EBFCD88
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ unknown] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Hit:1 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease
Get:5 https://download.docker.com/linux/ubuntu bionic InRelease [64.4 kB]
Hit:6 http://archive.canonical.com/ubuntu bionic InRelease
Get:7 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages [9852 B]
Fetched 74.3 kB in 1s (134 kB/s)
Reading package lists... Done
$ sudo apt-get update
Hit:1 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 https://download.docker.com/linux/ubuntu bionic InRelease
Hit:5 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:6 http://archive.canonical.com/ubuntu bionic InRelease
Reading package lists... Done
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
grub-pc-bin libnuma1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
aufs-tools cgroupfs-mount libltdl7 pigz
The following NEW packages will be installed:
aufs-tools cgroupfs-mount containerd.io docker-ce docker-ce-cli libltdl7 pigz
0 upgraded, 7 newly installed, 0 to remove and 30 not upgraded.
Need to get 85.5 MB of archives.
After this operation, 384 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/universe amd64 pigz amd64 2.4-1 [57.4 kB]
Get:2 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/universe amd64 aufs-tools amd64 1:4.9+20170918-1ubuntu1 [104 kB]
Get:3 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/universe amd64 cgroupfs-mount all 1.4 [6320 B]
Get:4 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/main amd64 libltdl7 amd64 2.4.6-2 [38.8 kB]
Get:5 https://download.docker.com/linux/ubuntu bionic/stable amd64 containerd.io amd64 1.2.10-3 [20.0 MB]
Get:6 https://download.docker.com/linux/ubuntu bionic/stable amd64 docker-ce-cli amd64 5:19.03.5~3-0~ubuntu-bionic [42.5 MB]
Get:7 https://download.docker.com/linux/ubuntu bionic/stable amd64 docker-ce amd64 5:19.03.5~3-0~ubuntu-bionic [22.8 MB]
Fetched 85.5 MB in 4s (23.2 MB/s)
Selecting previously unselected package pigz.
(Reading database ... 60091 files and directories currently installed.)
Preparing to unpack .../0-pigz_2.4-1_amd64.deb ...
Unpacking pigz (2.4-1) ...
Selecting previously unselected package aufs-tools.
Preparing to unpack .../1-aufs-tools_1%3a4.9+20170918-1ubuntu1_amd64.deb ...
Unpacking aufs-tools (1:4.9+20170918-1ubuntu1) ...
Selecting previously unselected package cgroupfs-mount.
Preparing to unpack .../2-cgroupfs-mount_1.4_all.deb ...
Unpacking cgroupfs-mount (1.4) ...
Selecting previously unselected package containerd.io.
Preparing to unpack .../3-containerd.io_1.2.10-3_amd64.deb ...
Unpacking aufs-tools (1:4.9+20170918-1ubuntu1) ...
Selecting previously unselected package cgroupfs-mount.
Preparing to unpack .../2-cgroupfs-mount_1.4_all.deb ...
Unpacking cgroupfs-mount (1.4) ...
Selecting previously unselected package containerd.io.
Preparing to unpack .../3-containerd.io_1.2.10-3_amd64.deb ...
Unpacking containerd.io (1.2.10-3) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../4-docker-ce-cli_5%3a19.03.5~3-0~ubuntu-bionic_amd64.deb ...
Unpacking docker-ce-cli (5:19.03.5~3-0~ubuntu-bionic) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../5-docker-ce_5%3a19.03.5~3-0~ubuntu-bionic_amd64.deb ...
Unpacking docker-ce (5:19.03.5~3-0~ubuntu-bionic) ...
Selecting previously unselected package libltdl7:amd64.
Preparing to unpack .../6-libltdl7_2.4.6-2_amd64.deb ...
Unpacking libltdl7:amd64 (2.4.6-2) ...
Setting up aufs-tools (1:4.9+20170918-1ubuntu1) ...
Setting up containerd.io (1.2.10-3) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Setting up cgroupfs-mount (1.4) ...
Setting up libltdl7:amd64 (2.4.6-2) ...
Setting up docker-ce-cli (5:19.03.5~3-0~ubuntu-bionic) ...
Setting up pigz (2.4-1) ...
Setting up docker-ce (5:19.03.5~3-0~ubuntu-bionic) ...
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for systemd (237-3ubuntu10.31) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ureadahead (0.100.0-21) ...

関連)pythonのglob.glob

Ubuntuでdoockerインストール/設定

dockerコマンドでpermission denied

dockerコマンドでpermission denied
dockerのバージョンを表示する、docker versionコマンドを実行すると権限がない(permission denied)エラーになります。

$ docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea838
Built: Wed Nov 13 07:29:52 2019
OS/Arch: linux/amd64
Experimental: false
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdock
er.sock/v1.40/version: dial unix /var/run/docker.sock: connect: permission denied

エラーメッセージから、ソケット通信に使うファイル「/var/run/docker.sock」の書き込み権限がないようです。

以下のコマンドを実行すると、一般ユーザ権限でdockerを実行できます。

$ sudo groupadd docker
$ sudo gpasswd -a ${USER} docker
$ newgrp docker
$ sudo chgrp docker /var/run/docker.sock
$ sudo systemctl restart docker.service

実行するとこうなります。

$ sudo groupadd docker
groupadd: group 'docker' already exists
#グループはすでに作られていたのでエラーが出ています。
$ sudo gpasswd -a ${USER} docker
Adding user zenryokuhp to group docker
$ newgrp docker
$ sudo chgrp docker /var/run/docker.sock
$ sudo systemctl restart docker.service

改めてdocker versionを実行すると、permission deniedが出なくなりました。

$ docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea838
Built: Wed Nov 13 07:29:52 2019
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea838
Built: Wed Nov 13 07:28:22 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683

docker起動に必要なコマンドの使い方を解説

docker起動に必要なコマンドの使い方を解説
実行しているコマンドの内容は以下の通りです。

$ sudo groupadd docker

OSのユーザグループにdockerを追加する

 

$ sudo gpasswd -a ${USER} docker

現在のユーザwodockerグループに追加する

 

$ newgrp docker

現在のユーザのグループをdockerに変更

 

$ sudo chgrp docker /var/run/docker.sock

dockerがsocket通信で使用している/var/run/docker.sockファイルのグループをdockerに変更

 

$ sudo systemctl restart docker.dockerd

dockerサービスを再起動