作業メモ: AWS EC2でUbuntu Server 16.04を起動してDockerをインストールする

孤独のIT

数年ぶりにAWSをまともに使ってみようと思い立ったのでやってみる。このブログのWordPressを移行して、CloudFrontとかRDSとかELBとかECSとかEKSを試してみるのが目標。

数年ぶりなので、身体を慣らしつつEC2に現在の環境をなるべくそのまま移行してみる。まずはEC2インスタンスを起動して使えるようにするところまで。

EC2インスタンスを起動

普通にWebブラウザでAWSコンソールから起動しました。数年前の記憶と比べると、結構UI/UXが変わって洗練されてきてますね…

AMIは “Quick Start” にあったUbuntu Server 16.04を選択。Canonicalさんがサポートしてるバージョンなのかな。なんとなくofficialに近いモノを。

初回ログイン

ここでElastic IPを割り当て忘れてたので適当に割り当ててからSSH接続を試みる。

$ ssh ubuntu@xxx.xxx.xxx.xxx

無事に接続できたので、とりあえず sudo apt update してみると、更新があったので更新して再起動。

ubuntu@ip-10-0-1-107:~$ sudo apt update
ubuntu@ip-10-0-1-107:~$ sudo apt upgrade
ubuntu@ip-10-0-1-107:~$ sudo /sbin/shutdown -r now

無事に再起動してきて、kernelが更新されていることを確認した。リモートのサーバを再起動したら無事で済まないことも多いので一安心。

ubuntu@ip-10-0-1-107:~$ uname -a
Linux ip-10-0-1-107 4.4.0-1061-aws #70-Ubuntu SMP Fri May 25 21:47:34 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
ubuntu@ip-10-0-1-107:~$ sudo /sbin/shutdown -r now
sudo: unable to resolve host ip-10-0-1-107

# 再起動後
ubuntu@ip-10-0-1-107:~$ uname -a
Linux ip-10-0-1-107 4.4.0-1065-aws #75-Ubuntu SMP Fri Aug 10 11:14:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Dockerをインストールする

Dockerをどっかーんとインストールします。

Get Docker CE for Ubuntu | Docker Documentation に従って、コマンドを打ってけばインストール完了。かんたん。

ubuntu@ip-10-0-1-107:~$ sudo apt-get install \
>     apt-transport-https \
>     ca-certificates \
>     curl \
>     software-properties-common
sudo: unable to resolve host ip-10-0-1-107
Reading package lists... Done
Building dependency tree       
Reading state information... Done
apt-transport-https is already the newest version (1.2.27).
ca-certificates is already the newest version (20170717~16.04.1).
curl is already the newest version (7.47.0-1ubuntu2.8).
software-properties-common is already the newest version (0.96.20.7).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
## もう全部入ってたわ

ubuntu@ip-10-0-1-107:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo: unable to resolve host ip-10-0-1-107
OK

ubuntu@ip-10-0-1-107:~$ sudo apt-key fingerprint 0EBFCD88
sudo: unable to resolve host ip-10-0-1-107
pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <docker@docker.com>
sub   4096R/F273FCD8 2017-02-22
## 合ってたよ

ubuntu@ip-10-0-1-107:~$ sudo add-apt-repository \
>    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
>    $(lsb_release -cs) \
>    stable"
sudo: unable to resolve host ip-10-0-1-107

ubuntu@ip-10-0-1-107:~$ sudo apt update
(snip)
ubuntu@ip-10-0-1-107:~$ sudo apt install docker-ce
(snip)

ubuntu@ip-10-0-1-107:~$ sudo docker run hello-world
sudo: unable to resolve host ip-10-0-1-107
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete 
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
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/engine/userguide/

## とりあえず動いたよ

次に、Post-installation steps for Linux | Docker Documentationを見ながらグループを設定する。以前まで1つのページに書いてあったのですが、内容増えて分割されたんですね。

ubuntu@ip-10-0-1-107:~$ sudo groupadd docker
sudo: unable to resolve host ip-10-0-1-107
groupadd: group 'docker' already exists
ubuntu@ip-10-0-1-107:~$ sudo usermod -aG docker ubuntu
sudo: unable to resolve host ip-10-0-1-107
ubuntu@ip-10-0-1-107:~$ logout
## もう一回ログインしてから
ubuntu@ip-10-0-1-107:~$ docker run hello-world

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/engine/userguide/

完璧ですね。

おまけ: /etc/hosts にエントリを追加する

sudo する度に sudo: unable to resolve host ip-10-0-1-107 と言われるのが鬱陶しいので /etc/hosts に追加しておく。本当はどうするのが良いんだろ。

ubuntu@ip-10-0-1-107:~$ sudo vi /etc/hosts
### 下記の行を追加
127.0.1.1 ip-10-0-1-107
ubuntu@ip-10-0-1-107:~$ sudo ls
ubuntu@ip-10-0-1-107:~$ 
## 何も言われなくなってめでたし
タイトルとURLをコピーしました