Open8

AWS EC2上のDockerコンテナ内でPython Streamlitフレームワークアプリを動かして公開する

Teru roomTeru room
回答
WS EC2 上でDockerコンテナ内で動作するNginxをインターネット上に公開する手順は以下の通りです。

1. AWS EC2 インスタンスを起動する
AWSマネジメントコンソールにログインして、EC2サービスからインスタンスを起動します。必要に応じて、セキュリティグループを設定し、インバウンドトラフィックを許可します。

2. Dockerをインストールする

EC2インスタンスにログインして、Dockerをインストールします。以下のコマンドを使用することで、最新版のDockerをインストールできます。

$ sudo yum update -y
$ sudo amazon-linux-extras install docker

3. Nginx Dockerイメージを取得する
以下のコマンドを使用して、公式のNginx Dockerイメージを取得します。

$ sudo docker pull nginx

4. Dockerコンテナを作成する
以下のコマンドを使用して、Nginxを実行するためのDockerコンテナを作成します。-pオプションでホストマシンのポートとDockerコンテナ内のポートを紐付けることで、インターネット上からアクセスできるようになります。

$ sudo docker run -d -p 80:80 nginx

5. NginxによるWebページを作成する

Dockerコンテナ内のNginxで公開するWebページを作成します。例えば、/usr/share/nginx/html/index.htmlに簡単なHTMLページを作成することができます。

6. インターネットからアクセスする
EC2インスタンスのパブリックIPアドレスを使用して、インターネット上からNginxにアクセスできます。例えば、ブラウザで以下のURLを入力します。

http://<EC2のパブリックIPアドレス>

以上が、AWS EC2 上でDockerコンテナ内で動作するNginxをインターネット上に公開する手順になります。適宜、セキュリティ対策やパフォーマンスの最適化を行い、Webサイトを運用することが重要です。
  • AWS EC2 → インスタンス → 接続 → SSHクライアント
インスタンス ID
i-022c6c6ef566b64e0 (teruroomLinux)
SSH クライアントを開きます。

プライベートキーファイルを見つけます。このインスタンスの起動に使用されるキーは TeruroomLinux.pem です。

必要に応じて、このコマンドを実行して、キーが公開されていないことを確認します。
 chmod 400 TeruroomLinux.pem

ご使用のインスタンスの パブリック IP を使用してインスタンスに接続:
 43.207.210.70

例:

 ssh -i "TeruroomLinux.pem" ec2-user@43.207.210.70
  • Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Teru roomTeru room
  • (base) macpro:Downloads sharland$
chmod 400 TeruroomLinux.pem
  • (base) macpro:Downloads sharland$
ls -l TeruroomLinux.pem
-r--------@ 1 sharland  staff  1674  4 10 14:21 TeruroomLinux.pem
  • (base) macpro:Downloads sharland$
ssh -i "TeruroomLinux.pem" ec2-user@43.207.210.70
ssh -i "protoTeruroom.pem" ec2-user@ec2-13-230-229-224.ap-northeast-1.compute.amazonaws.com
  • [ec2-user@ip-172-31-9-111 ~]$
cat /etc/*-release
実行結果
Amazon Linux release 2023 (Amazon Linux)
NAME="Amazon Linux"
VERSION="2023"
ID="amzn"
ID_LIKE="fedora"
VERSION_ID="2023"
PLATFORM_ID="platform:al2023"
PRETTY_NAME="Amazon Linux 2023"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023"
HOME_URL="https://aws.amazon.com/linux/"
BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023"
SUPPORT_END="2028-03-01"
Amazon Linux release 2023 (Amazon Linux)
  • [ec2-user@ip-172-31-44-51 ~]$ cat /etc/system-release
Amazon Linux release 2023 (Amazon Linux)
  • [ec2-user@ip-172-31-44-51 ~]$ uname -r
6.1.29-47.49.amzn2023.x86_64
  • [ec2-user@ip-172-31-9-111 ~]$
rpm -E %{rhel}
7
Teru roomTeru room
  • [ec2-user@ip-172-31-9-111 ~]$
sudo yum update -y
  • [ec2-user@ip-10-0-1-20 ~]$
sudo dnf install -y docker
実行結果
Last metadata expiration check: 0:33:44 ago on Tue Nov 14 10:15:38 2023.
Dependencies resolved.
==================================================================================================================================================
 Package                                  Architecture             Version                                    Repository                     Size
==================================================================================================================================================
Installing:
 docker                                   x86_64                   20.10.23-1.amzn2023.0.1                    amazonlinux                    42 M
Installing dependencies:
 containerd                               x86_64                   1.6.19-1.amzn2023.0.1                      amazonlinux                    31 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.1.4-1.amzn2023.0.1                       amazonlinux                   3.1 M

Transaction Summary
==================================================================================================================================================
Install  10 Packages

Total download size: 77 M
Installed size: 300 M
Downloading Packages:
(1/10): libnfnetlink-1.0.1-19.amzn2023.0.2.x86_64.rpm                                                             300 kB/s |  30 kB     00:00    
(2/10): libcgroup-3.0-1.amzn2023.0.1.x86_64.rpm                                                                   717 kB/s |  75 kB     00:00    
(3/10): libnftnl-1.2.2-2.amzn2023.0.2.x86_64.rpm                                                                  1.8 MB/s |  84 kB     00:00    
(4/10): runc-1.1.4-1.amzn2023.0.1.x86_64.rpm                                                                      5.8 MB/s | 3.1 MB     00:00    
(5/10): iptables-nft-1.8.8-3.amzn2023.0.2.x86_64.rpm                                                              3.2 MB/s | 183 kB     00:00    
(6/10): pigz-2.5-1.amzn2023.0.3.x86_64.rpm                                                                        1.0 MB/s |  83 kB     00:00    
(7/10): iptables-libs-1.8.8-3.amzn2023.0.2.x86_64.rpm                                                             5.1 MB/s | 401 kB     00:00    
(8/10): libnetfilter_conntrack-1.0.8-2.amzn2023.0.2.x86_64.rpm                                                    2.2 MB/s |  58 kB     00:00    
(9/10): docker-20.10.23-1.amzn2023.0.1.x86_64.rpm                                                                  26 MB/s |  42 MB     00:01    
(10/10): containerd-1.6.19-1.amzn2023.0.1.x86_64.rpm                                                               15 MB/s |  31 MB     00:02    
--------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                              35 MB/s |  77 MB     00:02     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                          1/1 
  Installing       : runc-1.1.4-1.amzn2023.0.1.x86_64                                                                                        1/10 
  Installing       : containerd-1.6.19-1.amzn2023.0.1.x86_64                                                                                 2/10 
  Running scriptlet: containerd-1.6.19-1.amzn2023.0.1.x86_64                                                                                 2/10 
  Installing       : pigz-2.5-1.amzn2023.0.3.x86_64                                                                                          3/10 
  Installing       : libnftnl-1.2.2-2.amzn2023.0.2.x86_64                                                                                    4/10 
  Installing       : libcgroup-3.0-1.amzn2023.0.1.x86_64                                                                                     5/10 
  Installing       : libnfnetlink-1.0.1-19.amzn2023.0.2.x86_64                                                                               6/10 
  Installing       : libnetfilter_conntrack-1.0.8-2.amzn2023.0.2.x86_64                                                                      7/10 
  Installing       : iptables-libs-1.8.8-3.amzn2023.0.2.x86_64                                                                               8/10 
  Installing       : iptables-nft-1.8.8-3.amzn2023.0.2.x86_64                                                                                9/10 
  Running scriptlet: iptables-nft-1.8.8-3.amzn2023.0.2.x86_64                                                                                9/10 
  Running scriptlet: docker-20.10.23-1.amzn2023.0.1.x86_64                                                                                  10/10 
  Installing       : docker-20.10.23-1.amzn2023.0.1.x86_64                                                                                  10/10 
  Running scriptlet: docker-20.10.23-1.amzn2023.0.1.x86_64                                                                                  10/10 
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /usr/lib/systemd/system/docker.socket.

  Verifying        : libnfnetlink-1.0.1-19.amzn2023.0.2.x86_64                                                                               1/10 
  Verifying        : docker-20.10.23-1.amzn2023.0.1.x86_64                                                                                   2/10 
  Verifying        : libcgroup-3.0-1.amzn2023.0.1.x86_64                                                                                     3/10 
  Verifying        : containerd-1.6.19-1.amzn2023.0.1.x86_64                                                                                 4/10 
  Verifying        : libnftnl-1.2.2-2.amzn2023.0.2.x86_64                                                                                    5/10 
  Verifying        : runc-1.1.4-1.amzn2023.0.1.x86_64                                                                                        6/10 
  Verifying        : iptables-nft-1.8.8-3.amzn2023.0.2.x86_64                                                                                7/10 
  Verifying        : pigz-2.5-1.amzn2023.0.3.x86_64                                                                                          8/10 
  Verifying        : iptables-libs-1.8.8-3.amzn2023.0.2.x86_64                                                                               9/10 
  Verifying        : libnetfilter_conntrack-1.0.8-2.amzn2023.0.2.x86_64                                                                     10/10 
==================================================================================================================================================
WARNING:
  A newer release of "Amazon Linux" is available.

  Available Versions:

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

      dnf upgrade --releasever=2023.0.20230614

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

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

      dnf upgrade --releasever=2023.1.20230628

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

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

      dnf upgrade --releasever=2023.1.20230629

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

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

      dnf upgrade --releasever=2023.1.20230705

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

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

      dnf upgrade --releasever=2023.1.20230719

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

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

      dnf upgrade --releasever=2023.1.20230725

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

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

      dnf upgrade --releasever=2023.1.20230809

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

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

      dnf upgrade --releasever=2023.1.20230825

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

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

      dnf upgrade --releasever=2023.1.20230906

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

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

      dnf upgrade --releasever=2023.1.20230912

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

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

      dnf upgrade --releasever=2023.2.20230920

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

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

      dnf upgrade --releasever=2023.2.20231002

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

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

      dnf upgrade --releasever=2023.2.20231011

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

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

      dnf upgrade --releasever=2023.2.20231016

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

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

      dnf upgrade --releasever=2023.2.20231018

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

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

      dnf upgrade --releasever=2023.2.20231026

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

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

      dnf upgrade --releasever=2023.2.20231030

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

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

Installed:
  containerd-1.6.19-1.amzn2023.0.1.x86_64       docker-20.10.23-1.amzn2023.0.1.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.1.4-1.amzn2023.0.1.x86_64             

Complete!
  • [ec2-user@ip-172-31-9-111 ~]$
sudo docker --version
Docker version 20.10.22, build 3a2c30b
  • [ec2-user@ip-172-31-9-111 ~]$
sudo service docker start
Redirecting to /bin/systemctl start docker.service
  • [ec2-user@ip-172-31-9-111 ~]$
sudo docker info
実行結果
Client:
 Context:    default
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.22
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 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: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 1e1ea6e986c6c86565bc33d52e34b81b3e2bc71f
 runc version: 5fd4c4d144137e991c4acebb2146ab1483a97925
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 5.10.173-154.642.amzn2.x86_64
 Operating System: Amazon Linux 2
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 952.7MiB
 Name: ip-10-0-1-20.ap-northeast-1.compute.internal
 ID: CF4Y:IFOV:YZYA:NQP4:LIWI:VVZI:5TUF:3TDC:KJSB:J76K:JBS2:PYUA
 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
  • [ec2-user@ip-172-31-9-111 ~]$
sudo usermod -aG docker ec2-user
  • [ec2-user@ip-172-31-9-111 ~]$
logout
  • (base) macpro:Downloads sharland$
ssh -i "TeruroomLinux.pem" ec2-user@43.207.210.70
Last login: Mon Apr 10 05:29:31 2023 from xxx-xx-xxx-xxx.xxxx.xxxx.ne.jp

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

https://aws.amazon.com/amazon-linux-2/```
  • [ec2-user@ip-10-0-1-20 ~]$
sudo service docker start
Redirecting to /bin/systemctl start docker.service
  • [ec2-user@ip-172-31-9-111 ~]$
docker run hello-world
実行結果
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:ffb13da98453e0f04d33a6eee5bb8e46ee50d08ebe17735fc0779d0349e889e9
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/
  • [ec2-user@ip-172-31-9-111 ~]$
docker pull nginx
実行結果
latest: Pulling from library/nginx
f1f26f570256: Pull complete 
7f7f30930c6b: Pull complete 
2836b727df80: Pull complete 
e1eeb0f1c06b: Pull complete 
86b2457cc2b0: Pull complete 
9862f2ee2e8c: Pull complete 
Digest: sha256:2ab30d6ac53580a6db8b657abf0f68d75360ff5cc1670a85acb5bd85ba1b19c0
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
  • [ec2-user@ip-172-31-9-111 ~]$
docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         latest    080ed0ed8312   8 days ago      142MB
hello-world   latest    feb5d9fea6a5   18 months ago   13.3kB
  • [ec2-user@ip-172-31-9-111 ~]$
docker run -d -p 80:80 nginx
01063529c51708c90a2bf7fecc3618f34bf73393526da9541ad488804fb8a0a4
  • [ec2-user@ip-172-31-9-111 ~]$
curl http://localhost:80
実行結果
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Teru roomTeru room

EC2インスタンスのセキュリティグループを確認して、HTTP(ポート80)へのアクセスが許可されていることを確認します。必要に応じて、セキュリティグループのインバウンドルールを追加して、HTTPアクセスを許可する必要があります。

  • AWS EC2 セキュリティグループのインバウンドルール 追加

    • タイプ:http
    • プロトコル:TCP
    • ポート範囲:80
      • ソース:カスタム0.0.0.0/0
  • [ec2-user@ip-172-31-9-111 ~]$

docker ps
実行結果
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                               NAMES
01063529c517   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   quizzical_moore
  • [ec2-user@ip-172-31-9-111 ~]$
docker ps -a
実行結果
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                     PORTS                               NAMES
01063529c517   nginx         "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes               0.0.0.0:80->80/tcp, :::80->80/tcp   quizzical_moore
d2fe03670d78   hello-world   "/hello"                 8 minutes ago   Exited (0) 8 minutes ago                                       romantic_dhawan
Teru roomTeru room
  • [ec2-user@ip-10-0-1-20 ~]$
docker stop 719f3dc6f334 5b55685e6a02
実行結果
719f3dc6f334
5b55685e6a02
  • [ec2-user@ip-10-0-1-20 ~]$
docker images
実行結果
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         latest    080ed0ed8312   12 days ago     142MB
hello-world   latest    feb5d9fea6a5   18 months ago   13.3kB
  • [ec2-user@ip-10-0-1-20 ~]$
docker rmi 080ed0ed8312 feb5d9fea6a5
実行結果
Untagged: nginx:latest
Untagged: nginx@sha256:2ab30d6ac53580a6db8b657abf0f68d75360ff5cc1670a85acb5bd85ba1b19c0
Deleted: sha256:080ed0ed8312deca92e9a769b518cdfa20f5278359bd156f3469dd8fa532db6b
Deleted: sha256:d78ffa8b3ff6145eb277087027ab2f07317ef3c8155dea0c68aba0be0dc9e357
Deleted: sha256:52c16551e27714b78f78d51673e87d8327b6878f61f5ed37bbda99b30bd6258f
Deleted: sha256:f71bcdb5f9b7b85bc15acf26db665dbd64f1e59fc13cbdfa7b7cac4fed5c3ac1
Deleted: sha256:e5804950d08e966c6a00a433c67a12cff6347e5e2108865dfde6148f8d18130e
Deleted: sha256:4da23111cc0445f1a069ac60b0575acfc86218f0783d48440f32b35bde461a41
Deleted: sha256:3af14c9a24c941c626553628cf1942dcd94d40729777f2fcfbcd3b8a3dfccdd6
Untagged: hello-world:latest
Untagged: hello-world@sha256:ffb13da98453e0f04d33a6eee5bb8e46ee50d08ebe17735fc0779d0349e889e9
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
  • [ec2-user@ip-10-0-1-20 ~]$
docker images
実行結果
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
Teru roomTeru room
  • [ec2-user@ip-172-31-9-111 ~]$
docker system
実行結果
Usage:  docker system COMMAND

Manage Docker

Commands:
  df          Show docker disk usage
  events      Get real time events from the server
  info        Display system-wide information
  prune       Remove unused data

Run 'docker system COMMAND --help' for more information on a command.
  • [ec2-user@ip-172-31-9-111 ~]$
docker system df
実行結果
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          2         2         142.1MB   0B (0%)
Containers      2         0         1.093kB   1.093kB (100%)
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B
  • [ec2-user@ip-172-31-9-111 ~]$
docker system info
  • [ec2-user@ip-172-31-9-111 ~]$
docker system prune
実行結果
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache
  • Are you sure you want to continue? [y/N] y
実行結果
Deleted Containers:
01063529c51708c90a2bf7fecc3618f34bf73393526da9541ad488804fb8a0a4
d2fe03670d7873a160ba986a4483b713ed96abab81874ba06306a26424bc1305

Total reclaimed space: 1.093kB

[ec2-user@ip-172-31-9-111 ~]$ sudo service docker stop
Redirecting to /bin/systemctl stop docker.service
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket
Teru roomTeru room

Streamlit containerを作成する

  • [ec2-user@ip-172-31-9-111 ~]$
mkdir dev;cd dev;mkdir app;cd app
  • [ec2-user@ip-172-31-9-111 app]$
pwd
実行結果
/home/ec2-user/dev/app
  • [ec2-user@ip-172-31-9-111 app]$
touch Dockerfile;vim Dockerfile
編集内容
# app/Dockerfile

FROM python:3.9-slim

WORKDIR /app

RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    software-properties-common \
    git \
    && rm -rf /var/lib/apt/lists/*

RUN git clone https://github.com/streamlit/streamlit-example.git .

RUN pip3 install -r requirements.txt

EXPOSE 8501

HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health

ENTRYPOINT ["streamlit", "run", "streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
  • [ec2-user@ip-172-31-9-111 app]$
touch requirements.txt;vim requirements.txt
編集内容
altair
pandas
streamlit
  • [ec2-user@ip-172-31-9-111 app]$
touch streamlit_app.py;vim streamlit_app.py
編集内容
from collections import namedtuple
import altair as alt
import math
import pandas as pd
import streamlit as st

"""
# Welcome to Streamlit!

Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:

If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
forums](https://discuss.streamlit.io).

In the meantime, below is an example of what you can do with just a few lines of code:
"""

with st.echo(code_location='below'):
   total_points = st.slider("Number of points in spiral", 1, 5000, 2000)
   num_turns = st.slider("Number of turns in spiral", 1, 100, 9)

   Point = namedtuple('Point', 'x y')
   data = []

   points_per_turn = total_points / num_turns

   for curr_point_num in range(total_points):
      curr_turn, i = divmod(curr_point_num, points_per_turn)
      angle = (curr_turn + 1) * 2 * math.pi * i / points_per_turn
      radius = curr_point_num / total_points
      x = radius * math.cos(angle)
      y = radius * math.sin(angle)
      data.append(Point(x, y))

   st.altair_chart(alt.Chart(pd.DataFrame(data), height=500, width=500)
      .mark_circle(color='#0068c9', opacity=0.5)
      .encode(x='x:Q', y='y:Q'))
Teru roomTeru room
  • [ec2-user@ip-172-31-9-111 app]$
docker build -t streamlit .
実行結果
Sending build context to Docker daemon   5.12kB
Step 1/8 : FROM python:3.9-slim
3.9-slim: Pulling from library/python
・・・・・
Successfully built 1deb0f58a661
  • [ec2-user@ip-172-31-9-111 app]$
docker images
実行結果
REPOSITORY   TAG        IMAGE ID       CREATED          SIZE
streamlit    latest     56f41c4ba115   17 seconds ago   984MB
python       3.9-slim   3279228d157d   2 weeks ago      124MB
  • AWS EC2 インバウンドのルールを編集
    • タイプ:カスタムTCP
    • プロトコル:TCP
    • ポート:8501
    • ソース:Anywhere 0.0.0.0/0
      • ルールを保存
  • [ec2-user@ip-172-31-9-111 app]$
docker run -p 8501:8501 streamlit
実行結果
Collecting usage statistics. To deactivate, set browser.gatherUsageStats to False.


  You can now view your Streamlit app in your browser.

  URL: http://0.0.0.0:8501
  • 別ターミナル
    • (base) macpro:~ sharland$
cd Downloads/
  • (base) macpro:Downloads sharland$
ssh -i "TeruroomLinux.pem" ec2-user@43.207.210.70
実行結果
Last login: Sun Apr  9 15:29:54 2023 from 121-84-123-193f1.hyg1.eonet.ne.jp

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

https://aws.amazon.com/amazon-linux-2/
  • [ec2-user@ip-172-31-9-111 ~]$
curl http://0.0.0.0:8501
実行結果
<!doctype html><html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"/><link rel="shortcut icon" href="./favicon.png"/><title>Streamlit</title><script>window.prerenderReady=!1</script><script src="./vendor/viz/viz-1.8.0.min.js" type="javascript/worker"></script><script src="./vendor/bokeh/bokeh-2.4.3.min.js"></script><script src="./vendor/bokeh/bokeh-widgets-2.4.3.min.js"></script><script src="./vendor/bokeh/bokeh-tables-2.4.3.min.js"></script><script src="./vendor/bokeh/bokeh-api-2.4.3.min.js"></script><script src="./vendor/bokeh/bokeh-gl-2.4.3.min.js"></script><script src="./vendor/bokeh/bokeh-mathjax-2.4.3.min.js"></script><script defer="defer" src="./static/js/main.b4c46d75.js"></script><link href="./static/css/main.f4a8738f.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>[ec2-user@
  • 任意の端末のブラウザでURLにアクセス
    • URL:http://43.207.210.70:8501/