0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

EC2にDockerを立てて、Webページを表示してみる

0
Posted at

AWSが全然わからなすぎるWebエンジニアです。前職ではAWSを触る権限がある人たちが限られていたので仕事でAWSを触ることがほとんどないまま転職し、バリバリAWSを使わないといけない環境になりましたが、いまだに全然わかりません...

今回は自分のような人のために、とりあえず動かしてみて、動いたぞー!という感覚を得てなんとなく理解するための記事です。

Regionを確認する

アジアパシフィック(東京)になっていることを確認。

キーペアを作成する

まずEC2にアクセスするためのキーペアを作成します。

image.png

EC2のメニューにある、キーペアをクリック。

image.png

ー「名前」だけ変更し、キーペアを作成、をクリック。

すると、自動的にPCにキーペアファイルがダウンロードされます。

EC2を起動する

image.png

メニューから「インスタンス」を選び、「インスタンスを起動する」ボタンを押下。

image.png

ー「名前」を変更。

OSやマシンイメージ、インスタンスタイプはそのままデフォルトを選択したままにする。

image.png

「キーペア」を先ほど作成したものを選択。

image.png

「ネットワーク設定」で「インターネットからのHTTPトラフィックを許可」にチェック。
これで外部からインターネット経由でアクセスできるようにします。

ストレージ設定はいじらず、「インスタンスを起動」を押下。

これでインスタンスが「実行中」になるのを待ちます。

Cloudshellでログインする

自分のPCのTerminalなどでも良いです。

Cloudshellの場合は、Cloudshellをメニューからクリックします。

image.png

アクションメニューから、ファイルをアップロードを選択。
先ほどダウンロードしたキーペアファイルをアップロードします。

アップロードできたらファイル権限を変更しておきます。
「chmod 400 xx.pem」

image.png

sshでEC2にアクセスします。
「ssh -i xx.pem(pemファイル名) ec2-user@xx(ここはEC2のパブリックIPアドレス)」

パブリックIPv4アドレスは起動したインスタンス名をクリックしたときに見れる、詳細ページに記載してあります。

最初はフィンガープリントなどを聞かれますが、yesで進みます。

~ $ ssh -i first_ec2.pem ec2-user@35.72.179.3
The authenticity of host '35.72.179.3 (35.72.179.3)' can't be established.
ED25519 key fingerprint is SHA256:u4tHlXLGR6+FvtRCHzJ07sFO+5kMYTGNS2Zv5cpQkCg.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '35.72.179.3' (ED25519) to the list of known hosts.
   ,     #_
   ~\_  ####_        Amazon Linux 2023
  ~~  \_#####\
  ~~     \###|
  ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
   ~~       V~' '->
    ~~~         /
      ~~._.   _/
         _/ _/
       _/m/'

その後、以下のコマンドを打っていきます。

[ec2-user@ip-172-31-25-244 ~]$ sudo su -
[root@ip-172-31-25-244 ~]# yum install -y docker
Amazon Linux 2023 Kernel Livepatch repository                                                                                                                       259 kB/s |  31 kB     00:00    
Last metadata expiration check: 0:00:01 ago on Wed Apr  8 01:16:52 2026.
Dependencies resolved.
====================================================================================================================================================================================================
 Package                                               Architecture                          Version                                               Repository                                  Size
====================================================================================================================================================================================================
Installing:
 docker                                                x86_64                                25.0.14-1.amzn2023.0.2                                amazonlinux                                 46 M
Installing dependencies:
 container-selinux                                     noarch                                4:2.245.0-1.amzn2023                                  amazonlinux                                 58 k
 containerd                                            x86_64                                2.2.1-1.amzn2023.0.1                                  amazonlinux                                 24 M
 iptables-libs                                         x86_64                                1.8.8-3.amzn2023.0.2                                  amazonlinux                                401 k
 iptables-nft                                          x86_64                                1.8.8-3.amzn2023.0.2                                  amazonlinux                                183 k
 libcgroup                                             x86_64                                3.0-1.amzn2023.0.1                                    amazonlinux                                 75 k
 libnetfilter_conntrack                                x86_64                                1.0.8-2.amzn2023.0.2                                  amazonlinux                                 58 k
 libnfnetlink                                          x86_64                                1.0.1-19.amzn2023.0.2                                 amazonlinux                                 30 k
 libnftnl                                              x86_64                                1.2.2-2.amzn2023.0.2                                  amazonlinux                                 84 k
 pigz                                                  x86_64                                2.5-1.amzn2023.0.3                                    amazonlinux                                 83 k
 runc                                                  x86_64                                1.3.4-1.amzn2023.0.2                                  amazonlinux                                3.9 M

Transaction Summary
====================================================================================================================================================================================================
Install  11 Packages

Total download size: 75 M
Installed size: 282 M
Downloading Packages:
(1/11): container-selinux-2.245.0-1.amzn2023.noarch.rpm                                                                                                             1.4 MB/s |  58 kB     00:00    
(2/11): iptables-libs-1.8.8-3.amzn2023.0.2.x86_64.rpm                                                                                                                11 MB/s | 401 kB     00:00    
(3/11): iptables-nft-1.8.8-3.amzn2023.0.2.x86_64.rpm                                                                                                                4.1 MB/s | 183 kB     00:00    
(4/11): libcgroup-3.0-1.amzn2023.0.1.x86_64.rpm                                                                                                                     2.2 MB/s |  75 kB     00:00    
(5/11): libnetfilter_conntrack-1.0.8-2.amzn2023.0.2.x86_64.rpm                                                                                                      2.2 MB/s |  58 kB     00:00    
(6/11): libnfnetlink-1.0.1-19.amzn2023.0.2.x86_64.rpm                                                                                                               1.0 MB/s |  30 kB     00:00    
(7/11): libnftnl-1.2.2-2.amzn2023.0.2.x86_64.rpm                                                                                                                    3.0 MB/s |  84 kB     00:00    
(8/11): pigz-2.5-1.amzn2023.0.3.x86_64.rpm                                                                                                                          2.5 MB/s |  83 kB     00:00    
(9/11): containerd-2.2.1-1.amzn2023.0.1.x86_64.rpm                                                                                                                   64 MB/s |  24 MB     00:00    
(10/11): runc-1.3.4-1.amzn2023.0.2.x86_64.rpm                                                                                                                        29 MB/s | 3.9 MB     00:00    
(11/11): docker-25.0.14-1.amzn2023.0.2.x86_64.rpm                                                                                                                    68 MB/s |  46 MB     00:00    
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                               103 MB/s |  75 MB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                            1/1 
  Installing       : runc-1.3.4-1.amzn2023.0.2.x86_64                                                                                                                                          1/11 
  Installing       : containerd-2.2.1-1.amzn2023.0.1.x86_64                                                                                                                                    2/11 
  Running scriptlet: containerd-2.2.1-1.amzn2023.0.1.x86_64                                                                                                                                    2/11 
  Installing       : pigz-2.5-1.amzn2023.0.3.x86_64                                                                                                                                            3/11 
  Installing       : libnftnl-1.2.2-2.amzn2023.0.2.x86_64                                                                                                                                      4/11 
  Installing       : libnfnetlink-1.0.1-19.amzn2023.0.2.x86_64                                                                                                                                 5/11 
  Installing       : libnetfilter_conntrack-1.0.8-2.amzn2023.0.2.x86_64                                                                                                                        6/11 
  Installing       : iptables-libs-1.8.8-3.amzn2023.0.2.x86_64                                                                                                                                 7/11 
  Installing       : iptables-nft-1.8.8-3.amzn2023.0.2.x86_64                                                                                                                                  8/11 
  Running scriptlet: iptables-nft-1.8.8-3.amzn2023.0.2.x86_64                                                                                                                                  8/11 
  Installing       : libcgroup-3.0-1.amzn2023.0.1.x86_64                                                                                                                                       9/11 
  Running scriptlet: container-selinux-4:2.245.0-1.amzn2023.noarch                                                                                                                            10/11 
  Installing       : container-selinux-4:2.245.0-1.amzn2023.noarch                                                                                                                            10/11 
  Running scriptlet: container-selinux-4:2.245.0-1.amzn2023.noarch                                                                                                                            10/11 
  Running scriptlet: docker-25.0.14-1.amzn2023.0.2.x86_64                                                                                                                                     11/11 
  Installing       : docker-25.0.14-1.amzn2023.0.2.x86_64                                                                                                                                     11/11 
  Running scriptlet: docker-25.0.14-1.amzn2023.0.2.x86_64                                                                                                                                     11/11 
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /usr/lib/systemd/system/docker.socket.

  Running scriptlet: container-selinux-4:2.245.0-1.amzn2023.noarch                                                                                                                            11/11 
  Running scriptlet: docker-25.0.14-1.amzn2023.0.2.x86_64                                                                                                                                     11/11 
  Verifying        : container-selinux-4:2.245.0-1.amzn2023.noarch                                                                                                                             1/11 
  Verifying        : containerd-2.2.1-1.amzn2023.0.1.x86_64                                                                                                                                    2/11 
  Verifying        : docker-25.0.14-1.amzn2023.0.2.x86_64                                                                                                                                      3/11 
  Verifying        : iptables-libs-1.8.8-3.amzn2023.0.2.x86_64                                                                                                                                 4/11 
  Verifying        : iptables-nft-1.8.8-3.amzn2023.0.2.x86_64                                                                                                                                  5/11 
  Verifying        : libcgroup-3.0-1.amzn2023.0.1.x86_64                                                                                                                                       6/11 
  Verifying        : libnetfilter_conntrack-1.0.8-2.amzn2023.0.2.x86_64                                                                                                                        7/11 
  Verifying        : libnfnetlink-1.0.1-19.amzn2023.0.2.x86_64                                                                                                                                 8/11 
  Verifying        : libnftnl-1.2.2-2.amzn2023.0.2.x86_64                                                                                                                                      9/11 
  Verifying        : pigz-2.5-1.amzn2023.0.3.x86_64                                                                                                                                           10/11 
  Verifying        : runc-1.3.4-1.amzn2023.0.2.x86_64                                                                                                                                         11/11 
====================================================================================================================================================================================================
WARNING:
  A newer release of "Amazon Linux" is available.

  Available Versions:

  Version 2023.11.20260406:
    Run the following command to upgrade to 2023.11.20260406:

      dnf upgrade --releasever=2023.11.20260406

    Release notes:
     https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes-2023.11.20260406.html

====================================================================================================================================================================================================

Installed:
  container-selinux-4:2.245.0-1.amzn2023.noarch     containerd-2.2.1-1.amzn2023.0.1.x86_64     docker-25.0.14-1.amzn2023.0.2.x86_64                   iptables-libs-1.8.8-3.amzn2023.0.2.x86_64    
  iptables-nft-1.8.8-3.amzn2023.0.2.x86_64          libcgroup-3.0-1.amzn2023.0.1.x86_64        libnetfilter_conntrack-1.0.8-2.amzn2023.0.2.x86_64     libnfnetlink-1.0.1-19.amzn2023.0.2.x86_64    
  libnftnl-1.2.2-2.amzn2023.0.2.x86_64              pigz-2.5-1.amzn2023.0.3.x86_64             runc-1.3.4-1.amzn2023.0.2.x86_64                      

Complete!
[root@ip-172-31-25-244 ~]# sytemctl enable docker
-bash: sytemctl: command not found
[root@ip-172-31-25-244 ~]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@ip-172-31-25-244 ~]# systemctl start docker
[root@ip-172-31-25-244 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; preset: disabled)
     Active: active (running) since Wed 2026-04-08 01:20:25 UTC; 8s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
    Process: 27204 ExecStartPre=/bin/mkdir -p /run/docker (code=exited, status=0/SUCCESS)
    Process: 27205 ExecStartPre=/usr/libexec/docker/docker-setup-runtimes.sh (code=exited, status=0/SUCCESS)
   Main PID: 27206 (dockerd)
      Tasks: 9
     Memory: 30.1M
        CPU: 374ms
     CGroup: /system.slice/docker.service
             └─27206 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --default-ulimit nofile=32768:65536

Apr 08 01:20:24 ip-172-31-25-244.ap-northeast-1.compute.internal systemd[1]: Starting docker.service - Docker Application Container Engine...
Apr 08 01:20:25 ip-172-31-25-244.ap-northeast-1.compute.internal dockerd[27206]: time="2026-04-08T01:20:25.025774168Z" level=info msg="Starting up"
Apr 08 01:20:25 ip-172-31-25-244.ap-northeast-1.compute.internal dockerd[27206]: time="2026-04-08T01:20:25.090980611Z" level=info msg="Loading containers: start."
Apr 08 01:20:25 ip-172-31-25-244.ap-northeast-1.compute.internal dockerd[27206]: time="2026-04-08T01:20:25.692206619Z" level=info msg="Loading containers: done."
Apr 08 01:20:25 ip-172-31-25-244.ap-northeast-1.compute.internal dockerd[27206]: time="2026-04-08T01:20:25.714232734Z" level=info msg="Docker daemon" commit=d334795 containerd-snapshotter=false s>
Apr 08 01:20:25 ip-172-31-25-244.ap-northeast-1.compute.internal dockerd[27206]: time="2026-04-08T01:20:25.714414727Z" level=info msg="Daemon has completed initialization"
Apr 08 01:20:25 ip-172-31-25-244.ap-northeast-1.compute.internal dockerd[27206]: time="2026-04-08T01:20:25.757348253Z" level=info msg="API listen on /run/docker.sock"
Apr 08 01:20:25 ip-172-31-25-244.ap-northeast-1.compute.internal systemd[1]: Started docker.service - Docker Application Container Engine.

[root@ip-172-31-25-244 ~]# docker run hello-worold
Unable to find image 'hello-worold:latest' locally
docker: Error response from daemon: pull access denied for hello-worold, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
[root@ip-172-31-25-244 ~]# ^C
[root@ip-172-31-25-244 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
4f55086f7dd0: Pull complete 
Digest: sha256:452a468a4bf985040037cb6d5392410206e47db9bf5b7278d281f94d1c2d0931
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/

[root@ip-172-31-25-244 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    e2ac70e7319a   2 weeks ago   10.1kB
[root@ip-172-31-25-244 ~]# docker pull amazonlinux:2
2: Pulling from library/amazonlinux
5e0c2ef2594e: Pull complete 
Digest: sha256:dc69cc77521b5f921c5610e200bec1ef8141709ba33bf5c427ab6e85a8674ecb
Status: Downloaded newer image for amazonlinux:2
docker.io/library/amazonlinux:2
[root@ip-172-31-25-244 ~]# docker run -p 80:80 -it -name test01 amazonlinux:2 /bin/bash
unknown shorthand flag: 'n' in -name
See 'docker run --help'.
[root@ip-172-31-25-244 ~]# yum install -y httpd
Last metadata expiration check: 0:05:44 ago on Wed Apr  8 01:16:52 2026.
Dependencies resolved.
====================================================================================================================================================================================================
 Package                                            Architecture                          Version                                                  Repository                                  Size
====================================================================================================================================================================================================
Installing:
 httpd                                              x86_64                                2.4.66-1.amzn2023.0.1                                    amazonlinux                                 47 k
Installing dependencies:
 apr                                                x86_64                                1.7.5-1.amzn2023.0.4                                     amazonlinux                                129 k
 apr-util                                           x86_64                                1.6.3-1.amzn2023.0.2                                     amazonlinux                                 97 k
 apr-util-lmdb                                      x86_64                                1.6.3-1.amzn2023.0.2                                     amazonlinux                                 13 k
 generic-logos-httpd                                noarch                                18.0.0-12.amzn2023.0.3                                   amazonlinux                                 19 k
 httpd-core                                         x86_64                                2.4.66-1.amzn2023.0.1                                    amazonlinux                                1.4 M
 httpd-filesystem                                   noarch                                2.4.66-1.amzn2023.0.1                                    amazonlinux                                 13 k
 httpd-tools                                        x86_64                                2.4.66-1.amzn2023.0.1                                    amazonlinux                                 81 k
 libbrotli                                          x86_64                                1.0.9-4.amzn2023.0.2                                     amazonlinux                                315 k
 mailcap                                            noarch                                2.1.49-3.amzn2023.0.3                                    amazonlinux                                 33 k
Installing weak dependencies:
 apr-util-openssl                                   x86_64                                1.6.3-1.amzn2023.0.2                                     amazonlinux                                 15 k
 mod_http2                                          x86_64                                2.0.27-1.amzn2023.0.3                                    amazonlinux                                166 k
 mod_lua                                            x86_64                                2.4.66-1.amzn2023.0.1                                    amazonlinux                                 60 k

Transaction Summary
====================================================================================================================================================================================================
Install  13 Packages

Total download size: 2.4 M
Installed size: 6.9 M
Downloading Packages:
(1/13): apr-util-lmdb-1.6.3-1.amzn2023.0.2.x86_64.rpm                                                                                                               374 kB/s |  13 kB     00:00    
(2/13): apr-1.7.5-1.amzn2023.0.4.x86_64.rpm                                                                                                                         3.2 MB/s | 129 kB     00:00    
(3/13): apr-util-1.6.3-1.amzn2023.0.2.x86_64.rpm                                                                                                                    1.8 MB/s |  97 kB     00:00    
(4/13): apr-util-openssl-1.6.3-1.amzn2023.0.2.x86_64.rpm                                                                                                            608 kB/s |  15 kB     00:00    
(5/13): generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch.rpm                                                                                                       771 kB/s |  19 kB     00:00    
(6/13): httpd-2.4.66-1.amzn2023.0.1.x86_64.rpm                                                                                                                      1.6 MB/s |  47 kB     00:00    
(7/13): httpd-filesystem-2.4.66-1.amzn2023.0.1.noarch.rpm                                                                                                           508 kB/s |  13 kB     00:00    
(8/13): httpd-core-2.4.66-1.amzn2023.0.1.x86_64.rpm                                                                                                                  30 MB/s | 1.4 MB     00:00    
(9/13): httpd-tools-2.4.66-1.amzn2023.0.1.x86_64.rpm                                                                                                                2.5 MB/s |  81 kB     00:00    
(10/13): libbrotli-1.0.9-4.amzn2023.0.2.x86_64.rpm                                                                                                                  9.9 MB/s | 315 kB     00:00    
(11/13): mailcap-2.1.49-3.amzn2023.0.3.noarch.rpm                                                                                                                   1.3 MB/s |  33 kB     00:00    
(12/13): mod_http2-2.0.27-1.amzn2023.0.3.x86_64.rpm                                                                                                                 6.2 MB/s | 166 kB     00:00    
(13/13): mod_lua-2.4.66-1.amzn2023.0.1.x86_64.rpm                                                                                                                   2.3 MB/s |  60 kB     00:00    
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                12 MB/s | 2.4 MB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                            1/1 
  Installing       : apr-1.7.5-1.amzn2023.0.4.x86_64                                                                                                                                           1/13 
  Installing       : apr-util-lmdb-1.6.3-1.amzn2023.0.2.x86_64                                                                                                                                 2/13 
  Installing       : apr-util-openssl-1.6.3-1.amzn2023.0.2.x86_64                                                                                                                              3/13 
  Installing       : apr-util-1.6.3-1.amzn2023.0.2.x86_64                                                                                                                                      4/13 
  Installing       : mailcap-2.1.49-3.amzn2023.0.3.noarch                                                                                                                                      5/13 
  Installing       : httpd-tools-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                  6/13 
  Installing       : libbrotli-1.0.9-4.amzn2023.0.2.x86_64                                                                                                                                     7/13 
  Running scriptlet: httpd-filesystem-2.4.66-1.amzn2023.0.1.noarch                                                                                                                             8/13 
  Installing       : httpd-filesystem-2.4.66-1.amzn2023.0.1.noarch                                                                                                                             8/13 
  Installing       : httpd-core-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                   9/13 
  Installing       : mod_http2-2.0.27-1.amzn2023.0.3.x86_64                                                                                                                                   10/13 
  Installing       : mod_lua-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                     11/13 
  Installing       : generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch                                                                                                                        12/13 
  Installing       : httpd-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                       13/13 
  Running scriptlet: httpd-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                       13/13 
  Verifying        : apr-1.7.5-1.amzn2023.0.4.x86_64                                                                                                                                           1/13 
  Verifying        : apr-util-1.6.3-1.amzn2023.0.2.x86_64                                                                                                                                      2/13 
  Verifying        : apr-util-lmdb-1.6.3-1.amzn2023.0.2.x86_64                                                                                                                                 3/13 
  Verifying        : apr-util-openssl-1.6.3-1.amzn2023.0.2.x86_64                                                                                                                              4/13 
  Verifying        : generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch                                                                                                                         5/13 
  Verifying        : httpd-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                        6/13 
  Verifying        : httpd-core-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                   7/13 
  Verifying        : httpd-filesystem-2.4.66-1.amzn2023.0.1.noarch                                                                                                                             8/13 
  Verifying        : httpd-tools-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                  9/13 
  Verifying        : libbrotli-1.0.9-4.amzn2023.0.2.x86_64                                                                                                                                    10/13 
  Verifying        : httpd-tools-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                  9/13 
  Verifying        : libbrotli-1.0.9-4.amzn2023.0.2.x86_64                                                                                                                                    10/13 
  Verifying        : mailcap-2.1.49-3.amzn2023.0.3.noarch                                                                                                                                     11/13 
  Verifying        : mod_http2-2.0.27-1.amzn2023.0.3.x86_64                                                                                                                                   12/13 
  Verifying        : mod_lua-2.4.66-1.amzn2023.0.1.x86_64                                                                                                                                     13/13 
====================================================================================================================================================================================================
WARNING:
  A newer release of "Amazon Linux" is available.

  Available Versions:

  Version 2023.11.20260406:
    Run the following command to upgrade to 2023.11.20260406:

      dnf upgrade --releasever=2023.11.20260406

    Release notes:
     https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes-2023.11.20260406.html

====================================================================================================================================================================================================

Installed:
  apr-1.7.5-1.amzn2023.0.4.x86_64                       apr-util-1.6.3-1.amzn2023.0.2.x86_64      apr-util-lmdb-1.6.3-1.amzn2023.0.2.x86_64     apr-util-openssl-1.6.3-1.amzn2023.0.2.x86_64     
  generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch     httpd-2.4.66-1.amzn2023.0.1.x86_64        httpd-core-2.4.66-1.amzn2023.0.1.x86_64       httpd-filesystem-2.4.66-1.amzn2023.0.1.noarch    
  httpd-tools-2.4.66-1.amzn2023.0.1.x86_64              libbrotli-1.0.9-4.amzn2023.0.2.x86_64     mailcap-2.1.49-3.amzn2023.0.3.noarch          mod_http2-2.0.27-1.amzn2023.0.3.x86_64           
  mod_lua-2.4.66-1.amzn2023.0.1.x86_64                 

Complete!
[root@ip-172-31-25-244 ~]# /usr/sbin/httpd

そして、httpdを起動したところで、先ほど入力したEC2インスタンスのパブリックIPアドレスを自分のPCのブラウザに入れて、アクセスしてみます。

image.png

動きました!

このままだと、デフォルトのままなので、表示を変えてみます。

[root@ip-172-31-25-244 ~]# echo hello, world! > /var/www/html/index.html
[root@ip-172-31-25-244 ~]# 

image.png

できました!

[root@ip-172-31-25-244 ~]# mkdir docker-test
[root@ip-172-31-25-244 ~]# cd docker-test
[root@ip-172-31-25-244 docker-test]# touch Dockerfile
[root@ip-172-31-25-244 docker-test]# vi Dockerfile

次に、docker-testというディレクトリを作って、そこにDockerfileを作ります。

そしたら、ファイルを開いて以下を貼り付けます。

FROM amazonlinux:2
RUN yum install -y httpd
COPY ./index.html /var/www/html/index.html
EXPOSE 80
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

Cloudshellで複数行貼り付けようとすると、このような注意ポップアップが出ますが気にせず進みます。

image.png

次に、index.htmlファイルを作成して、こちらも編集します。

[root@ip-172-31-25-244 docker-test]# touch index.html
[root@ip-172-31-25-244 docker-test]# vi index.html

内容は適当で良いです。

image.png

[root@ip-172-31-25-244 docker-test]# docker run -d -p 80:80 --name training2026 training2026-test
35443e271335246f167377b63a284df0d24fb780643484f73dd598900c6ae2d8

image.png

できました!!

次は、index.htmlを更新して、再度ビルドし直してみましょう。

とりあえず、Dockerを止めます。

[root@ip-172-31-25-244 docker-test]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS                               NAMES
39274159382f   training2026-test   "/usr/sbin/httpd -D …"   37 seconds ago   Up 36 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   training2026
[root@ip-172-31-25-244 docker-test]# docker stop 39274159382f
39274159382f

そして、index.htmlを更新しておきます。

[root@ip-172-31-25-244 docker-test]# docker rm training2026
training2026
[root@ip-172-31-25-244 docker-test]# docker build -t training2026-test .
[+] Building 0.1s (8/8) FINISHED                                                                                                                                                     docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                           0.0s
 => => transferring dockerfile: 238B                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/amazonlinux:2                                                                                                                               0.0s
 => [internal] load .dockerignore                                                                                                                                                              0.0s
 => => transferring context: 2B                                                                                                                                                                0.0s
 => [1/3] FROM docker.io/library/amazonlinux:2                                                                                                                                                 0.0s
 => [internal] load build context                                                                                                                                                              0.0s
 => => transferring context: 123B                                                                                                                                                              0.0s
 => CACHED [2/3] RUN yum install -y httpd                                                                                                                                                      0.0s
 => CACHED [3/3] COPY ./index.html /var/www/html/index.html                                                                                                                                    0.0s
 => exporting to image                                                                                                                                                                         0.0s
 => => exporting layers                                                                                                                                                                        0.0s
 => => writing image sha256:3b95eee49f742954625e4e2642a4252ce72d7d452f31842123f3f8f8c64631a2                                                                                                   0.0s
 => => naming to docker.io/library/training2026-test                                                                                                                                           0.0s
[root@ip-172-31-25-244 docker-test]# docker run -d -p 80:80 --name training2026 training2026-test
b3a452634d0e387508d8dd849b4145f62be7899f7215c0fb9adbaf471e9e4e54

image.png

更新内容が表示されました!

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?