AWS・UbuntuServer20.04LTSでDockerを動かす

前提として、AWS上で UbuntuServer20.04LTS をインスタンスとして起動して、一通り初期設定しているところから記載します。

ほぼDocker公式にある手順に沿ってやってみた。

1.事前の準備

Ubuntu の最新のパッケージ情報を再取得 – update サブコマンド
Ubuntu のパッケージの更新 – dist-upgrade サブコマンド
Ubuntu の依存関係で必要なくなったパッケージを削除 – autoremove サブコマンド

$ sudo apt update
$ sudo apt dist-upgrade
$ sudo apt autoremove

を実行して、事前準備とします。

2.Docker CE を導入

  • 公式サイトにあるインストールスクリプトを使う https://docs.docker.com/engine/install/ubuntu/
    ※ なぜか、Webサイトの内容をコピペしたらエラーになったので、apt-get を apt に変えて実施したらできた。

ubuntu@ip-10-0-0-161:~$ sudo apt 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 (20201027ubuntu0.20.04.1).
ca-certificates set to manually installed.
curl is already the newest version (7.68.0-1ubuntu2.2).
curl set to manually installed.
software-properties-common is already the newest version (0.98.9.3).
software-properties-common set to manually installed.
The following NEW packages will be installed:
apt-transport-https gnupg-agent
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 6944 B of archives.
After this operation, 206 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal-updates/universe amd64 apt-transport-https all 2.0.2ubuntu0.1 [1708 B]
Get:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 gnupg-agent all 2.2.19-3ubuntu2 [5236 B]
Fetched 6944 B in 0s (243 kB/s)
Selecting previously unselected package apt-transport-https.
(Reading database … 119896 files and directories currently installed.)
Preparing to unpack …/apt-transport-https_2.0.2ubuntu0.1_all.deb …
Unpacking apt-transport-https (2.0.2ubuntu0.1) …
Selecting previously unselected package gnupg-agent.
Preparing to unpack …/gnupg-agent_2.2.19-3ubuntu2_all.deb …
Unpacking gnupg-agent (2.2.19-3ubuntu2) …
Setting up apt-transport-https (2.0.2ubuntu0.1) …
Setting up gnupg-agent (2.2.19-3ubuntu2) …

  • GPG 公開鍵のインストール

ubuntu@ip-10-0-0-161:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
OK

  • 指紋9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88の最後の8文字を検索して、指紋付きのキーがあることを確認します 

ubuntu@ip-10-0-0-161:~$ 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]

  • aptコマンド用のリポジトリを設定しますが、dockerではstable、edge、testが公開されています。

ubuntu@ip-10-0-0-161:~$ sudo add-apt-repository \
> “deb [arch=amd64] https://download.docker.com/linux/ubuntu \
> $(lsb_release -cs) \
> stable”

Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Hit:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:4 https://download.docker.com/linux/ubuntu focal InRelease [36.2 kB]
Hit:5 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:6 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [3684 B]
Fetched 305 kB in 0s (632 kB/s)
Reading package lists… Done

  • docker-ceのインストール
    公式サイトの説明によると、パッケージインデックスを更新し、Docker Engineとcontainerdの最新バージョンをインストールするやり方。特定のバージョンをインストールする方法も載っていた。

ubuntu@ip-10-0-0-161:~$ sudo apt-get update

Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal InRelease [265 kB] Hit:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal-updates InRelease Hit:3 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal-backports InRelease Hit:4 https://download.docker.com/linux/ubuntu focal InRelease Hit:5 http://security.ubuntu.com/ubuntu focal-security InRelease Fetched 265 kB in 1s (486 kB/s) Reading package lists… Done

ubuntu@ip-10-0-0-161:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io

Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
aufs-tools cgroupfs-mount pigz
The following NEW packages will be installed:
aufs-tools cgroupfs-mount containerd.io docker-ce docker-ce-cli pigz
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 91.2 MB of archives.
After this operation, 410 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://download.docker.com/linux/ubuntu focal/stable amd64 containerd.io amd64 1.3.7-1 [24.3 MB]
Get:2 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-cli amd64 5:19.03.13~3-0~ubuntu-focal [44.2 MB]
Get:3 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 pigz amd64 2.4-1 [57.4 kB]
Get:4 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce amd64 5:19.03.13~3-0~ubuntu-focal [22.6 MB]
Get:5 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 aufs-tools amd64 1:4.14+20190211-1ubuntu1 [104 kB]
Get:6 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 cgroupfs-mount all 1.4 [6320 B]
Fetched 91.2 MB in 2s (59.6 MB/s)
Selecting previously unselected package pigz.
(Reading database … 119904 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.14+20190211-1ubuntu1_amd64.deb …
Unpacking aufs-tools (1:4.14+20190211-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.3.7-1_amd64.deb …
Unpacking containerd.io (1.3.7-1) …
Selecting previously unselected package docker-ce-cli.
Preparing to unpack …/4-docker-ce-cli_5%3a19.03.13~3-0~ubuntu-focal_amd64.deb …
Unpacking docker-ce-cli (5:19.03.13~3-0~ubuntu-focal) …
Selecting previously unselected package docker-ce.
Preparing to unpack …/5-docker-ce_5%3a19.03.13~3-0~ubuntu-focal_amd64.deb …
Unpacking docker-ce (5:19.03.13~3-0~ubuntu-focal) …
Setting up aufs-tools (1:4.14+20190211-1ubuntu1) …
Setting up containerd.io (1.3.7-1) …
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Setting up docker-ce-cli (5:19.03.13~3-0~ubuntu-focal) …
Setting up pigz (2.4-1) …
Setting up cgroupfs-mount (1.4) …
Setting up docker-ce (5:19.03.13~3-0~ubuntu-focal) …
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 systemd (245.4-4ubuntu3.3) …
Processing triggers for man-db (2.9.1-1) …
Processing triggers for libc-bin (2.31-0ubuntu9.1) …

  • hello-world イメージを実行して、DockerEngineが正しくインストールされていることを確認します。

ubuntu@ip-10-0-0-161:~$ sudo docker run hello-world

Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582838d92970a09f323
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/

  • Dockerエンジンの動作状況を表示

ubuntu@ip-10-0-0-161:~$ sudo docker info

Client:
Debug Mode: false

Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 1
Server Version: 19.03.13
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 5.4.0-1025-aws
Operating System: Ubuntu 20.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 978.6MiB
Name: ip-10-0-0-161
ID: VJMF:TE7K:H6OL:OCZY:VO3U:HHAH:PD7F:DOYK:M55G:W5W6:5JCW:2B6H
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support

  • WordPressのDockerイメージを探索   ※ [OK]は公式イメージ

ubuntu@ip-10-0-0-161:~$ sudo docker search wordpress

NAME DESCRIPTION STARS OFFICIAL AUTOMATED
wordpress The WordPress rich content management system… 3853 [OK]
bitnami/wordpress Bitnami Docker Image for WordPress 153 [OK]
appcontainers/wordpress Centos/Debian Based Customizable WordPress C… 34 [OK]
bitnami/wordpress-nginx Bitnami Docker Image for WordPress with NGINX 22 [OK]
aveltens/wordpress-backup Easily backup and restore your WordPress blo… 18 [OK]
conetix/wordpress-with-wp-cli WordPress with wp-cli integration 17 [OK]
centurylink/wordpress WordPress image with MySQL removed. 14 [OK]
arm32v7/wordpress The WordPress rich content management system… 14
appsvcorg/wordpress-alpine-php This is a WordPress Docker image which can … 11
wodby/wordpress-php PHP for WordPress 6 [OK]
dalareo/wordpress-ldap WordPress images with LDAP support automatic… 6 [OK]
wordpressdevelop/php PHP images for the WordPress local developme… 5
arm64v8/wordpress The WordPress rich content management system… 4
wodby/wordpress-nginx Nginx for WordPress 4 [OK]
wordpressdevelop/cli WP-CLI images for the WordPress local develo… 4
ansibleplaybookbundle/wordpress-ha-apb An APB which deploys WordPress HA 4 [OK]
gwee/wordpress-sendmail-docker wordpress with sendmail 3 [OK]
wordpressdevelop/phpunit PHPUnit images for the WordPress local devel… 3
ppc64le/wordpress The WordPress rich content management system… 1
dsteinkopf/wordpress wordpress clone plus some php extensions 1 [OK]
demyx/wordpress Non-root Docker image running Alpine Linux, … 1
bakudankun/wordpress-ja WordPress Japanese Edition forked from offic… 1 [OK]
i386/wordpress The WordPress rich content management system… 1
amd64/wordpress The WordPress rich content management system… 0
arm32v5/wordpress The WordPress rich content management system… 0

  • WordPressの最新の公式Dockerイメージをダウンロード

ubuntu@ip-10-0-0-161:~$ sudo docker pull wordpress

Using default tag: latest
latest: Pulling from library/wordpress
852e50cd189d: Pull complete
0266fc315b01: Pull complete
4c8a5fa787a1: Pull complete
46fc127c1884: Pull complete
f768b7fadf16: Pull complete
345b578c1a78: Pull complete
90aafe41e78d: Pull complete
af01fae4e5fc: Pull complete
9e463236c8bc: Pull complete
cd12b0a220f2: Pull complete
d9f76220cef4: Pull complete
c09cf9a96ea9: Pull complete
6dda8bec068c: Pull complete
fd36a10c28fe: Pull complete
e11e483ab28e: Pull complete
ab40cb22800e: Pull complete
b08636db59dd: Pull complete
d2c7678bc02b: Pull complete
f903e014412a: Pull complete
114ff1eb7cb0: Pull complete
Digest: sha256:92e97d9b3147038e3cc541a224cc951bef597061827e23a208a24c36bff1c1fe
Status: Downloaded newer image for wordpress:latest
docker.io/library/wordpress:latest

  • コンテナ作成・起動

ubuntu@ip-10-0-0-161:~$ sudo docker run -d -p 80:80 wordpress
※ NextCloud でWebアクセスを使っていたので、エラーになった。
c8d56f026fe07a3efcae9769f6b57ae8a2a9667414b97f8947e3223ae21d3d81
docker: Error response from daemon: driver failed programming external connectivity on endpoint suspicious_moore (0e86dd5c07f41eb476942b1f6a0c0b596d84b4f130388048542d5799945c8704): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use.

ubuntu@ip-10-0-0-161:~$ sudo docker run -d -p 8080:80 wordpress
※ WordPressはWebアクセスを8080に変えたらできた!
522f26d41164248dfb59707a42478b3cd3b23c72d3d896205401c208b9f8c174

  • コンテナの状況確認

ubuntu@ip-10-0-0-161:~$ sudo docker ps -a

CONTAINER ID  IMAGE   COMMAND      CREATED STATUS           PORTS  NAMES
522f26d41164  wordpress  ”docker-entrypoint.s… ” About an hour ago Up About an hour 0.0.0.0:8080->80/tcp peaceful_galileo
c8d56f026fe0  wordpress  ”docker-entrypoint.s… ” About an hour ago Created               suspicious_moore
be03995bb678  hello-world “/hello”        2 hours ago Exited (0) 2 hours ago             brave_shirley

  • ubuntuユーザをDockerグループに追加して、管理者権限を持たせる

ubuntu@ip-10-0-0-161:~$ sudo usermod -a -G docker ubuntu

  • CPU使用率を指定して、起動  ※ デフォルトが1024

ubuntu@ip-10-0-0-161:~$ sudo docker run –cpu-shares=2048 -d -p 8080:80 wordpress

  • メモリサイズを指定して、起動  ※ m:Mバイト g:Gバイト k:Kバイト b:バイト

ubuntu@ip-10-0-0-161:~$ sudo docker run –memory 512m -d -p 8080:80 wordpress

  • CPUコア指定  -cpuset-cpus=コアID  コアID=1,2,3,4,1-3など
  • リソースの状態確認

ubuntu@ip-10-0-0-161:~$ sudo docker ps -a
コンテナID        CPU使用率 コンテナが利用しているメモリ使用率/上限サイズ
                            メモリ使用率 送受信データ量 読み書きデータ量
CONTAINER ID NAME      CPU % MEM USAGE / LIMIT  MEM %  NET I/O BLOCK I/O PIDS
187f2349e6c6 hardcore_bardeen 0.00% 19.04MiB / 978.6MiB   1.95%  1.02kB / 0B 97MB / 0B 6

  • メモリサイズ変更

sudo docker update -m 512m コンテナID

  • Dockerネットワーク表示

ubuntu@ip-10-0-0-161:~$ sudo docker network ls

NETWORK ID     NAME  DRIVER SCOPE
5fbcfebc4c33  bridge  bridge local  ← 仮想ブリッジ
29ae5a2f132a  host    host     local
bea847d5882d   none   null      local