LoginSignup
13
12

More than 1 year has passed since last update.

Ubuntu20.04 に Docker を導入する (Also Ubuntu 22.04)

Last updated at Posted at 2021-06-13

インストールがうまくいったというエビデンス。
(2022/7/30 追記:Ubuntu 22.04においても同様に導入できることを確認しました)
(2022/8/17 追記: ユーザ権限を設定 および docker-compose インストールについて追記しました)

環境

  • Ubuntu 20.04 LTS Desktop ja / Ubuntu 22.04 LTS Desktop ja
  • Docker Community Edition (CE)

インストール方針

  • sudo を使って Docker コマンドを動かす
  • Ubuntu 標準レポジトリの Docker は使わない
  • 公式 Docker レポジトリを使用して最新版を apt でインストールする

Docker インストール

まずは

「Ubuntu 20.04へのDockerのインストールおよび使用方法 | DigitalOcean」
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04-ja
または
「How To Install and Use Docker on Ubuntu 22.04 | DigitalOcean」
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-22-04

「ステップ1— Dockerをインストールする」

の通りやっていきます。

下準備


$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common


docker の レポジトリを追加して使えるようにします。

Ubuntu20.04 の場合


$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
$ sudo apt update

Ubuntu22.04の場合

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt update

インストール候補を確認します。


$ apt-cache policy docker-ce
docker-ce:
  インストールされているバージョン: (なし)
  候補:               5:20.10.7~3-0~ubuntu-focal
  バージョンテーブル:
     5:20.10.7~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.6~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.5~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.4~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.3~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.2~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.1~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.0~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.15~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.14~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.13~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.12~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.11~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.10~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.9~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages

Docker公式のレポジトリになってますね。では、Dockerをインストールします。


$ sudo apt install docker-ce

デーモンが起動していることを確認します。


$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-06-12 23:30:57 JST; 57s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 167273 (dockerd)
      Tasks: 13
     Memory: 43.4M
     CGroup: /system.slice/docker.service
             └─167273 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

 6月 12 23:30:56 nanbuwks-ThinkPad-X230 dockerd[167273]: time="2021-06-12T23:30:56.464426542+09:00" level=warning msg="Your kernel does not support CPU realtime sc>
 6月 12 23:30:56 nanbuwks-ThinkPad-X230 dockerd[167273]: time="2021-06-12T23:30:56.464488380+09:00" level=warning msg="Your kernel does not support cgroup blkio we>
 6月 12 23:30:56 nanbuwks-ThinkPad-X230 dockerd[167273]: time="2021-06-12T23:30:56.464508737+09:00" level=warning msg="Your kernel does not support cgroup blkio we>
 6月 12 23:30:56 nanbuwks-ThinkPad-X230 dockerd[167273]: time="2021-06-12T23:30:56.464847077+09:00" level=info msg="Loading containers: start."
 6月 12 23:30:56 nanbuwks-ThinkPad-X230 dockerd[167273]: time="2021-06-12T23:30:56.805499806+09:00" level=info msg="Default bridge (docker0) is assigned with an IP>
 6月 12 23:30:57 nanbuwks-ThinkPad-X230 dockerd[167273]: time="2021-06-12T23:30:57.081790887+09:00" level=info msg="Loading containers: done."
 6月 12 23:30:57 nanbuwks-ThinkPad-X230 dockerd[167273]: time="2021-06-12T23:30:57.202347194+09:00" level=info msg="Docker daemon" commit=b0f5bc3 graphdriver(s)=ov>
 6月 12 23:30:57 nanbuwks-ThinkPad-X230 dockerd[167273]: time="2021-06-12T23:30:57.202660215+09:00" level=info msg="Daemon has completed initialization"
 6月 12 23:30:57 nanbuwks-ThinkPad-X230 systemd[1]: Started Docker Application Container Engine.
 6月 12 23:30:57 nanbuwks-ThinkPad-X230 dockerd[167273]: time="2021-06-12T23:30:57.277265072+09:00" level=info msg="API listen on /run/docker.sock"
$ 


使ってみる

hello-world

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:7d246653d0511db2a6b2e0436cfd0e52ac8c066000264b3ce63331ac66dca625
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

動作しています。

$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
c113fde343e6   hello-world    "/hello"                 47 seconds ago   Exited (0) 44 seconds ago             festive_mirza
khani

テストを停止し削除します。


$ sudo docker rm c113fde343e6
c113fde343e6
$ sudo docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:7d246653d0511db2a6b2e0436cfd0e52ac8c066000264b3ce63331ac66dca625
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
$ 

テストその2
「git-artes/docker-gnuradio」
https://github.com/git-artes/docker-gnuradio

にある、「A docker container with the release version of GNU Radio (3.8 right now) in the latest Ubuntu (and a 3.7 version in Ubuntu 18.04)」

を使ってみます。

(2022/09/14追記):途中で以下のように tzdata がハングしてしまいました。これは docker のインストールの問題ではなく、Dockerfile で対応が必要なようです。単純なテストであれば先の hello-world でのテストに留めておくのがよさそうです。

Configuring tzdata
------------------

Please select the geographic area in which you live. Subsequent configuration
questions will narrow this down by presenting a list of cities, representing
the time zones in which they are located.

  1. Africa   3. Antarctica  5. Arctic  7. Atlantic  9. Indian    11. US
  2. America  4. Australia   6. Asia    8. Europe    10. Pacific  12. Etc
Geographic area: 6

(追記終わり)


$ git clone https://github.com/git-artes/docker-gnuradio.git
Cloning into 'docker-gnuradio'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 17 (delta 6), reused 14 (delta 3), pack-reused 0
Unpacking objects: 100% (17/17), 4.27 KiB | 729.00 KiB/s, done.
$ cd docker-gnuradio/

$ ls -alh
合計 64K
drwxrwxr-x  5 nanbuwks nanbuwks 4.0K  6月 12 23:35 .
drwxr-xr-x 49 nanbuwks nanbuwks  36K  6月 12 23:35 ..
drwxrwxr-x  8 nanbuwks nanbuwks 4.0K  6月 12 23:35 .git
-rw-rw-r--  1 nanbuwks nanbuwks  846  6月 12 23:35 LICENSE
-rw-rw-r--  1 nanbuwks nanbuwks 2.9K  6月 12 23:35 README.md
drwxrwxr-x  2 nanbuwks nanbuwks 4.0K  6月 12 23:35 gnuradio-releases
drwxrwxr-x  2 nanbuwks nanbuwks 4.0K  6月 12 23:35 gnuradio-releases-37
$ cd gnuradio-releases
$ sudo docker build -t ubuntu:gnuradio-releases .

(時間かかります)


$ sudo docker run --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" --device /dev/snd -v persistent:/home/gnuradio/persistent --group-add=audio -it ubuntu:gnuradio-releases bash
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

gnuradio@nanbuwks-ThinkPad-X230:~$ cat /etc/issue
Ubuntu 20.04.2 LTS \n \l

gnuradio@nanbuwks-ThinkPad-X230:~$ ls
persistent

動きました

ユーザ権限を設定

$ sudo usermod -aG docker $USER

として再起動します。

docker-compose インストール


$ sudo apt install docker-compose

インストール後の使い方🔰

$ docker stop $(docker ps -q)

で止めた後、再度Docker環境に入ろうとしても

$ docker exec -it ardublock2204 /bin/bash
Error response from daemon: Container a58cbb91a560faf5c7e9c59f290922ab5a6b3bfc0350e3296cfd23411a2fb860 is not running

not runnning となる。

$ docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED        STATUS                        PORTS     NAMES
a58cbb91a560   ubuntu:22.04    "bash"                   3 days ago     Exited (137) 10 minutes ago             ardublock2204

Exited となっているので、以下のようにして起動すると入れるようになる。

$ docker start ardublock2204
13
12
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
13
12