LoginSignup
5

More than 1 year has passed since last update.

AWS EC2 docker + wordpress インストール

Last updated at Posted at 2021-03-14

AWSにEC2作る

AMIの選択

image.png

インスタンスタイプの選択

image.png

インスタンスの詳細の設定

自動割り当てパブリックIPを「有効」にした以外はデフォルト設定。

image.png

ストレージの追加

image.png

タグの追加

タグはなにも設定していない。

image.png

セキュリティグループの設定

セキュリティグループは事前に作成してあったものを利用した。

  • 22番ポート: SSH接続
  • 55555番ポート: WordPress接続

image.png

インスタンス作成の確認

内容に問題なければ「起動」。

image.png

キーペアの確認

事前に作ったあったキーペアを使った。

image.png

ec2でdockerとwordpressをインストール

sshでログイン


       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
[ec2-user@ip-x-x-x-x ~]$

yumをアップデート

[ec2-user@ip-x-x-x-x ~]$ sudo yum update -y
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                                                                           | 3.7 kB  00:00:00
No packages marked for update

docker準備

dockerをインストール

[ec2-user@ip-x-x-x-x ~]$ sudo amazon-linux-extras install docker -y
Installing docker
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker
10 metadata files removed
4 sqlite files removed
0 metadata files removed
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                                                                           | 3.7 kB  00:00:00
amzn2extra-docker                                                                                                                                    | 3.0 kB  00:00:00
(1/5): amzn2-core/2/x86_64/group_gz                                                                                                                  | 2.5 kB  00:00:00
(2/5): amzn2extra-docker/2/x86_64/updateinfo                                                                                                         |   76 B  00:00:00
(3/5): amzn2extra-docker/2/x86_64/primary_db                                                                                                         |  75 kB  00:00:00
(4/5): amzn2-core/2/x86_64/updateinfo                                                                                                                | 350 kB  00:00:00
(5/5): amzn2-core/2/x86_64/primary_db                                                                                                                |  50 MB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 0:19.03.13ce-1.amzn2 will be installed
--> Processing Dependency: runc >= 1.0.0 for package: docker-19.03.13ce-1.amzn2.x86_64
--> Processing Dependency: containerd >= 1.3.2 for package: docker-19.03.13ce-1.amzn2.x86_64
--> Processing Dependency: pigz for package: docker-19.03.13ce-1.amzn2.x86_64
--> Processing Dependency: libcgroup for package: docker-19.03.13ce-1.amzn2.x86_64
--> Running transaction check
---> Package containerd.x86_64 0:1.4.1-2.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.20200826.gitff819c7.amzn2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                           Arch                          Version                                                     Repository                                Size
============================================================================================================================================================================
Installing:
 docker                            x86_64                        19.03.13ce-1.amzn2                                          amzn2extra-docker                         37 M
Installing for dependencies:
 containerd                        x86_64                        1.4.1-2.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.20200826.gitff819c7.amzn2                         amzn2extra-docker                        3.7 M

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

Total download size: 65 M
Installed size: 270 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

(省略)

dockerサービス起動

[ec2-user@ip-x-x-x-x ~]$ sudo systemctl start docker

dockerサービス状態確認

[ec2-user@ip-x-x-x-x ~]$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-03-14 05:59:36 UTC; 25s ago
     Docs: https://docs.docker.com
  Process: 3722 ExecStartPre=/usr/libexec/docker/docker-setup-runtimes.sh (code=exited, status=0/SUCCESS)
  Process: 3709 ExecStartPre=/bin/mkdir -p /run/docker (code=exited, status=0/SUCCESS)
 Main PID: 3731 (dockerd)
    Tasks: 8
   Memory: 36.6M
   CGroup: /system.slice/docker.service
           mq3731 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --default-ulimit nofile=1024:4096

Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal dockerd[3731]: time="2021-03-14T05:59:36.354394783Z" level=info msg="scheme \"unix\" not registered, f...le=grpc
Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal dockerd[3731]: time="2021-03-14T05:59:36.354629298Z" level=info msg="ccResolverWrapper: sending update...le=grpc
Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal dockerd[3731]: time="2021-03-14T05:59:36.354858217Z" level=info msg="ClientConn switching balancer to ...le=grpc
Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal dockerd[3731]: time="2021-03-14T05:59:36.394039324Z" level=info msg="Loading containers: start."
Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal dockerd[3731]: time="2021-03-14T05:59:36.858998545Z" level=info msg="Default bridge (docker0) is assig...ddress"
Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal dockerd[3731]: time="2021-03-14T05:59:36.915341056Z" level=info msg="Loading containers: done."
Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal dockerd[3731]: time="2021-03-14T05:59:36.942822332Z" level=info msg="Docker daemon" commit=4484c46 gra...3.13-ce
Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal dockerd[3731]: time="2021-03-14T05:59:36.943274164Z" level=info msg="Daemon has completed initialization"
Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal systemd[1]: Started Docker Application Container Engine.
Mar 14 05:59:36 ip-172-31-20-108.us-east-2.compute.internal dockerd[3731]: time="2021-03-14T05:59:36.967245548Z" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.

dockerをec2-userで実行可能にする

[ec2-user@ip-x-x-x-x ~]$ sudo usermod -a -G docker ec2-user
[ec2-user@ip-x-x-x-x ~]$ exit

※ここで一度ログアウトして再度ログインすることで権限が反映される

docker-compose

docker-composeインストール

[ec2-user@ip-x-x-x-x ~]$ sudo curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(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   633  100   633    0     0   9447      0 --:--:-- --:--:-- --:--:--  9447
100 11.6M  100 11.6M    0     0  22.2M      0 --:--:-- --:--:-- --:--:-- 44.5M

curlのURLは↓ここでdocker-composeのバージョンを確認して適宜書き換える。(2021/03/14時点は1.28.5)
https://docs.docker.com/compose/install/#install-compose-on-linux-systems

docker-compose実行権限設定

[ec2-user@ip-x-x-x-x ~]$ sudo chmod +x /usr/local/bin/docker-compose

バージョン確認

[ec2-user@ip-x-x-x-x ~]$ docker-compose --version
docker-compose version 1.28.5, build c4eb3a1f

wordpress

docker-compose.ymlファイルを用意

[ec2-user@ip-x-x-x-x ~]$ pwd
/home/ec2-user
[ec2-user@ip-x-x-x-x ~]$ mkdir wordpress
[ec2-user@ip-x-x-x-x ~]$ vi ~/wordpress/docker-compose.yml
docker-compose.yml
version: '3.1'

services:

  wordpress:
    container_name: wp-trial
    image: wordpress
    restart: always
    ports:
      - 55555:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html

  db:
    container_name: wp-db
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress:
  db:

wordpress用のdocker-compose.ymlファイルは↓を参考に作成。
https://hub.docker.com/_/wordpress

wordpressをビルドして起動

docker-compose.ymlが入っているディレクトリに移動してdocker-compose upする。

[ec2-user@ip-x-x-x-x ~]$ cd ~/wordpress/
[ec2-user@ip-x-x-x-x wordpress]$ pwd
/home/ec2-user/wordpress
[ec2-user@ip-x-x-x-x wordpress]$ ls -la
total 4
drwxrwxr-x 2 ec2-user ec2-user  32 Mar 14 06:12 .
drwx------ 5 ec2-user ec2-user 142 Mar 14 06:12 ..
-rw-rw-r-- 1 ec2-user ec2-user 638 Mar 14 06:12 docker-compose.yml
[ec2-user@ip-x-x-x-x wordpress]$ docker-compose up --build -d
Creating network "wordpress_default" with the default driver
Creating volume "wordpress_wordpress" with default driver
Creating volume "wordpress_db" with default driver
Pulling wordpress (wordpress:)...
latest: Pulling from library/wordpress
6f28985ad184: Pull complete
db883aae18bc: Pull complete
ffae70ea03a9: Pull complete
1e8027612378: Pull complete
3ec32e53dce5: Pull complete
3bb74037bf77: Pull complete
feda0fbd85b1: Pull complete
b2244185b327: Pull complete
8852ae668073: Pull complete
985e21deb66e: Pull complete
f262da4e7afa: Pull complete
157f3d683e13: Pull complete
990684a56233: Pull complete
c80999e49328: Pull complete
02585da80b89: Pull complete
d68ab7635a0a: Pull complete
5a577fb48682: Pull complete
d27e8a2c96b8: Pull complete
f94fa08d2764: Pull complete
6db2ebd4cef9: Pull complete
9c4c1399bbb1: Pull complete
Digest: sha256:92fb18e472ce46f289b475457289075c20387374d79bc41724689aa88112eab1
Status: Downloaded newer image for wordpress:latest
Pulling db (mysql:5.7)...
5.7: Pulling from library/mysql
6f28985ad184: Already exists
e7cd18945cf6: Pull complete
ee91068b9313: Pull complete
b4efa1a4f93b: Pull complete
f220edfa5893: Pull complete
74a27d3460f8: Pull complete
2e11e23b7542: Pull complete
39ac93d44c47: Pull complete
dfd9db50d4ea: Pull complete
4e97f54f11a3: Pull complete
ebfb95795c5f: Pull complete
Digest: sha256:5f649e87093a5b6b863f5c5277b2d2aa797b04d68657494e0f28ffabfa25e781
Status: Downloaded newer image for mysql:5.7
Creating wp-trial  ... done
Creating wp-db ... done
[ec2-user@ip-172-31-20-108 wordpress]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                   NAMES
d0296aeb5318        mysql:5.7           "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp, 33060/tcp     wp-db
c01446d87997        wordpress           "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:55555->80/tcp   wp-trial

Wordpressにアクセスしてみる

image.png


参考

AWS公式 Dockerインストール手順
AWS EC2インスタンスにdockerとdocker-composeをインストールして簡単なWEBサービスを立ち上げる方法

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
5