search
LoginSignup
5

More than 1 year has passed since last update.

posted at

updated at

EC2(Amazon Linux 2)にdockerとdocker-composeをインストールしてみる

EC2にdockerとdocker-composeをインストールしてみる。

EC2起動

パブリックサブネットで適当にインスタンス起動。
SSHログインができるようSGはインバウンドのSSHを空けておく。

EC2ログイン

SSHクライアント(Teraterm等)でもInstant Conectでもやり方は何でもよいのでインスタンスにSSH接続する。ユーザーはec2-user。

Dockerインストール

$ sudo yum -y install docker
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                     | 3.7 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 0:19.03.6ce-4.amzn2 will be installed
--> Processing Dependency: runc >= 1.0.0 for package: docker-19.03.6ce-4.amzn2.x86_64
--> Processing Dependency: containerd >= 1.3.2 for package: docker-19.03.6ce-4.amzn2.x86_64
--> Processing Dependency: pigz for package: docker-19.03.6ce-4.amzn2.x86_64
--> Processing Dependency: libcgroup for package: docker-19.03.6ce-4.amzn2.x86_64
--> Running transaction check
---> Package containerd.x86_64 0:1.3.2-1.amzn2 will be installed
---> Package libcgroup.x86_64 0:0.41-21.amzn2 will be installed
---> Package pigz.x86_64 0:2.3.4-1.amzn2.0.1 will be installed
---> Package runc.x86_64 0:1.0.0-0.1.20200204.gitdc9208a.amzn2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================
 Package             Arch            Version                                         Repository                  Size
======================================================================================================================
Installing:
 docker              x86_64          19.03.6ce-4.amzn2                               amzn2extra-docker           37 M
Installing for dependencies:
 containerd          x86_64          1.3.2-1.amzn2                                   amzn2extra-docker           24 M
 libcgroup           x86_64          0.41-21.amzn2                                   amzn2-core                  66 k
 pigz                x86_64          2.3.4-1.amzn2.0.1                               amzn2-core                  81 k
 runc                x86_64          1.0.0-0.1.20200204.gitdc9208a.amzn2             amzn2extra-docker          2.6 M

Transaction Summary
======================================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 65 M
Installed size: 271 M
Downloading packages:
(1/5): libcgroup-0.41-21.amzn2.x86_64.rpm                                                      |  66 kB  00:00:00     
(2/5): pigz-2.3.4-1.amzn2.0.1.x86_64.rpm                                                       |  81 kB  00:00:00     
(3/5): containerd-1.3.2-1.amzn2.x86_64.rpm                                                     |  24 MB  00:00:00     
(4/5): docker-19.03.6ce-4.amzn2.x86_64.rpm                                                     |  37 MB  00:00:00     
(5/5): runc-1.0.0-0.1.20200204.gitdc9208a.amzn2.x86_64.rpm                                     | 2.6 MB  00:00:00     
----------------------------------------------------------------------------------------------------------------------
Total                                                                                  67 MB/s |  65 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : runc-1.0.0-0.1.20200204.gitdc9208a.amzn2.x86_64                                                    1/5 
  Installing : containerd-1.3.2-1.amzn2.x86_64                                                                    2/5 
  Installing : libcgroup-0.41-21.amzn2.x86_64                                                                     3/5 
  Installing : pigz-2.3.4-1.amzn2.0.1.x86_64                                                                      4/5 
  Installing : docker-19.03.6ce-4.amzn2.x86_64                                                                    5/5 
  Verifying  : docker-19.03.6ce-4.amzn2.x86_64                                                                    1/5 
  Verifying  : containerd-1.3.2-1.amzn2.x86_64                                                                    2/5 
  Verifying  : pigz-2.3.4-1.amzn2.0.1.x86_64                                                                      3/5 
  Verifying  : runc-1.0.0-0.1.20200204.gitdc9208a.amzn2.x86_64                                                    4/5 
  Verifying  : libcgroup-0.41-21.amzn2.x86_64                                                                     5/5 

Installed:
  docker.x86_64 0:19.03.6ce-4.amzn2                                                                                   

Dependency Installed:
  containerd.x86_64 0:1.3.2-1.amzn2                 libcgroup.x86_64 0:0.41-21.amzn2 pigz.x86_64 0:2.3.4-1.amzn2.0.1
  runc.x86_64 0:1.0.0-0.1.20200204.gitdc9208a.amzn2

Complete!
$

起動

$ sudo systemctl start docker.service
$ 
$ sudo systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-06-17 15:53:56 UTC; 16s ago
     Docs: https://docs.docker.com
  Process: 4765 ExecStartPre=/usr/libexec/docker/docker-setup-runtimes.sh (code=exited, status=0/SUCCESS)
  Process: 4755 ExecStartPre=/bin/mkdir -p /run/docker (code=exited, status=0/SUCCESS)
 Main PID: 4772 (dockerd)
    Tasks: 8
   Memory: 35.8M
   CGroup: /system.slice/docker.service
           └─4772 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --default-ulimit nofile=1...

Jun 17 15:53:55 ip-172-31-14-246.ap-northeast-1.compute.internal dockerd[4772]: time="2020-06-17T15:53:55.8582692...c
Jun 17 15:53:55 ip-172-31-14-246.ap-northeast-1.compute.internal dockerd[4772]: time="2020-06-17T15:53:55.8582884...c
Jun 17 15:53:55 ip-172-31-14-246.ap-northeast-1.compute.internal dockerd[4772]: time="2020-06-17T15:53:55.8583027...c
Jun 17 15:53:55 ip-172-31-14-246.ap-northeast-1.compute.internal dockerd[4772]: time="2020-06-17T15:53:55.8813748..."
Jun 17 15:53:56 ip-172-31-14-246.ap-northeast-1.compute.internal dockerd[4772]: time="2020-06-17T15:53:56.1582849..."
Jun 17 15:53:56 ip-172-31-14-246.ap-northeast-1.compute.internal dockerd[4772]: time="2020-06-17T15:53:56.2082072..."
Jun 17 15:53:56 ip-172-31-14-246.ap-northeast-1.compute.internal dockerd[4772]: time="2020-06-17T15:53:56.2355901...e
Jun 17 15:53:56 ip-172-31-14-246.ap-northeast-1.compute.internal dockerd[4772]: time="2020-06-17T15:53:56.2360941..."
Jun 17 15:53:56 ip-172-31-14-246.ap-northeast-1.compute.internal systemd[1]: Started Docker Application Container....
Jun 17 15:53:56 ip-172-31-14-246.ap-northeast-1.compute.internal dockerd[4772]: time="2020-06-17T15:53:56.2675511..."
Hint: Some lines were ellipsized, use -l to show in full.
$ 

自動起動設定

$ sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.serv
ice.
$
$ sudo systemctl list-unit-files | grep docker.service
docker.service          enabled 
$ 

ec2-userのセカンダリグループにdockerを追加

root権限以外のユーザーでdockerコマンドを実行するには
毎回sudoするか、dockerグループに所属するかのいずれかが必要。
ここではグループ追加をしておく。

$ sudo usermod -a -G docker ec2-user
$
$ id
uid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),4(adm),10(wheel),190(systemd-journal)
$ 

グループ追加がカレントシェルに反映されていない。
一旦ログアウトして接続し直してから再びidコマンド。

$ id
uid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),4(adm),10(wheel),190(systemd-journal),993(docker)
$ 

グループ追加が反映された。

dockerコマンドが実行できることの確認

$ docker --version
Docker version 19.03.6-ce, build 369ce74
$ 

docker-composeインストール

docker-composeは複数のコンテナのビルドや起動/停止をまとめて行うことが出来るツール。
使い方は ①dockerfile作成 ②docker-compose.yml作成 ③docker-compose実行 といった流れ。
まずはdocker公式に書かれているコマンドでインストール。
https://docs.docker.com/compose/install/

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compos
e-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   638  100   638    0     0   2006      0 --:--:-- --:--:-- --:--:--  2006
100 11.6M  100 11.6M    0     0  3325k      0  0:00:03  0:00:03 --:--:-- 4044k
$ 
$ sudo chmod +x /usr/local/bin/docker-compose
$ 

docker-composeコマンドが実行できることの確認

$ dockser-compose --version
docker-compose version 1.26.0, build d4451659
$ 

 
 

以上です。おつかれさまでした。

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
What you can do with signing up
5