ちょっと話題の記事

[待望] CloudShell で Docker が利用可能になりました

待望の CloudShell で Docker が使えるようになりました。今回は簡単に全体像まとめて、コンテナイメージをビルドしてみました。
2024.01.06

こんにちは! AWS 事業本部コンサルティング部のたかくに(@takakuni_) です。

タイトルの通りで CloudShell で docker コマンドが使えるようになってました。

今まではどうだったか

今まで、 CloudShell では Docker がサポートされておらず、コンテナイメージのビルド等が CloudShell 上で行えませんでした。

過去の Stack Overflow を確認するに、インストールはできるが、デーモンの起動ができなかったようです。

[cloudshell-user@ip-10-0-73-203 ~]$ docker images
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[cloudshell-user@ip-10-0-73-203 ~]$ sudo systemctl start docker
Failed to get D-Bus connection: Operation not permitted

https://stackoverflow.com/questions/68925494/aws-cloudshell-unable-to-start-docker-service

今回のアップデート

今回のアップデートで CloudShell では Docker がプリインストールされ、利用可能になりました!

Document history を確認するに 2023年12月27日 に アップデートがあった模様です。

Docker containers supported with AWS CloudShell in certain Regions Support for Docker containers with AWS CloudShell has been added in certain Regions. December 27, 2023

https://docs.aws.amazon.com/cloudshell/latest/userguide/history.html

当初、 CloudShell が AL2023 に移行したタイミングかと思っていましたが、移行後にサポートしたようです。

2023年12月19日のAWS CloudShell compute environment: specifications and software

CloudShell が Amazon Linux 2023 へ移行したアップデートブログ

やってみる

軽くですが CloudShell 上で Docker イメージのビルドをしてみます。 Docker の version は執筆時点で 24.0.5 でした。

[cloudshell-user@ip-10-134-5-100 ~]$ docker --version
Docker version 24.0.5, build ced0996
[cloudshell-user@ip-10-134-5-100 ~]$

次のコマンドで nginx コンテナイメージの作成、およびローカルでの起動を試してみます。

# ディレクトリの作成
mkdir nginx
cd nginx

# Dockerfile の作成
cat <<EOF > Dockerfile
FROM public.ecr.aws/amazonlinux/amazonlinux:2

RUN amazon-linux-extras enable epel && \
    yum install -y epel-release && \
    yum install -y nginx

COPY index.html /usr/share/nginx/html/index.html

CMD ["nginx", "-g", "daemon off; error_log /dev/stdout info;"]
EOF

# index.html の作成
cat <<EOF > index.html
<!DOCTYPE html>
<html>

<head>
    <title>CloudShell supports Docker Yeeeeeeeah!</title>
</head>

<body>
    <h1>CloudShell supports Docker Yeeeeeeeah!</h1>
    <p>We are very happy that Docker can be used in CloudShell environment!</p>
</body>

</html>
EOF

# Docker イメージの作成
docker image build -t nginx-demo .
docker image ls -a
# Docker コンテナの作成
docker container run -d -p 8080:80 --rm --name nginx-demo nginx-demo
docker container ls -a

無事 Docker コンテナが起動していることがわかります。

実行結果

[cloudshell-user@ip-10-132-65-234 ~]$ # ディレクトリの作成
[cloudshell-user@ip-10-132-65-234 ~]$ mkdir nginx
[cloudshell-user@ip-10-132-65-234 ~]$ cd nginx
[cloudshell-user@ip-10-132-65-234 nginx]$ 
[cloudshell-user@ip-10-132-65-234 nginx]$ # Dockerfile の作成
[cloudshell-user@ip-10-132-65-234 nginx]$ cat <<EOF > Dockerfile
> FROM public.ecr.aws/amazonlinux/amazonlinux:2
> 
> RUN amazon-linux-extras enable epel && \
>     yum install -y epel-release && \
>     yum install -y nginx
> 
> COPY index.html /usr/share/nginx/html/index.html
> 
> CMD ["nginx", "-g", "daemon off; error_log /dev/stdout info;"]
> EOF
[cloudshell-user@ip-10-132-65-234 nginx]$ 
[cloudshell-user@ip-10-132-65-234 nginx]$ # index.html の作成
[cloudshell-user@ip-10-132-65-234 nginx]$ cat <<EOF > index.html
> <!DOCTYPE html>
> <html>
> 
> <head>
>     <title>CloudShell supports Docker Yeeeeeeeah!</title>
> </head>
> 
> <body>
>     <h1>CloudShell supports Docker Yeeeeeeeah!</h1>
>     <p>We are very happy that Docker can be used in CloudShell environment!</p>
> </body>
> 
> </html>
> EOF
[cloudshell-user@ip-10-132-65-234 nginx]$ 
[cloudshell-user@ip-10-132-65-234 nginx]$ # Docker イメージの作成
[cloudshell-user@ip-10-132-65-234 nginx]$ docker image build -t nginx-demo .
[+] Building 56.6s (8/8) FINISHED                                                                                                                                                                                                                                                                              docker:default
 => [internal] load .dockerignore                                                                                                                                                                                                                                                                                        0.1s
 => => transferring context: 2B                                                                                                                                                                                                                                                                                          0.0s
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                                                                     0.0s
 => => transferring dockerfile: 354B                                                                                                                                                                                                                                                                                     0.0s
 => [internal] load metadata for public.ecr.aws/amazonlinux/amazonlinux:2                                                                                                                                                                                                                                                1.6s
 => [1/3] FROM public.ecr.aws/amazonlinux/amazonlinux:2@sha256:b65335719948a99e6f7f6ffa07eca7746f14930b2004af04f946baba535fadaf                                                                                                                                                                                          8.8s
 => => resolve public.ecr.aws/amazonlinux/amazonlinux:2@sha256:b65335719948a99e6f7f6ffa07eca7746f14930b2004af04f946baba535fadaf                                                                                                                                                                                          0.0s
 => => sha256:7cfddac867404bd106654872adf67b678121b49c583c631b58c2b1c78f1f6d06 1.48kB / 1.48kB                                                                                                                                                                                                                           0.0s
 => => sha256:99e9b04a7dc206f4a47a0937a2039102066a5273ce57a11fa6894d90fe3957bc 62.65MB / 62.65MB                                                                                                                                                                                                                         2.3s
 => => sha256:b65335719948a99e6f7f6ffa07eca7746f14930b2004af04f946baba535fadaf 770B / 770B                                                                                                                                                                                                                               0.0s
 => => sha256:252605f86da6db726e2da2eef8675304c9211f9a08388d366d74fb7418e65394 529B / 529B                                                                                                                                                                                                                               0.0s
 => => extracting sha256:99e9b04a7dc206f4a47a0937a2039102066a5273ce57a11fa6894d90fe3957bc                                                                                                                                                                                                                                6.4s
 => [internal] load build context                                                                                                                                                                                                                                                                                        0.0s
 => => transferring context: 348B                                                                                                                                                                                                                                                                                        0.0s
 => [2/3] RUN amazon-linux-extras enable epel &&     yum install -y epel-release &&     yum install -y nginx                                                                                                                                                                                                            34.7s
 => [3/3] COPY index.html /usr/share/nginx/html/index.html                                                                                                                                                                                                                                                               5.2s 
 => exporting to image                                                                                                                                                                                                                                                                                                   6.2s 
 => => exporting layers                                                                                                                                                                                                                                                                                                  6.2s 
 => => writing image sha256:69169412e9cdcf7cff8ef740314e188132436b108f08646412313e4e7c15cd10                                                                                                                                                                                                                             0.0s 
 => => naming to docker.io/library/nginx-demo                                                                                                                                                                                                                                                                            0.0s 
[cloudshell-user@ip-10-132-65-234 nginx]$ docker image ls -a                                                                                                                                                                                                                                                                  
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx-demo   latest    69169412e9cd   6 seconds ago   633MB
[cloudshell-user@ip-10-132-65-234 nginx]$ # Docker コンテナの作成
[cloudshell-user@ip-10-132-65-234 nginx]$ docker container run -d -p 8080:80 --rm --name nginx-demo nginx-demo
27a4a4bcd5236bc6e106058153a834e25f88c79ed7b6bfb484292057796a58b6
[cloudshell-user@ip-10-132-65-234 nginx]$ docker container ls -a
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS         PORTS                                   NAMES
27a4a4bcd523   nginx-demo   "nginx -g 'daemon of…"   30 seconds ago   Up 2 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx-demo
[cloudshell-user@ip-10-132-65-234 nginx]$

curl でローカルホストにリクエスト送ってみました。きちんとコンテナ上で nginx が起動していますね。

[cloudshell-user@ip-10-132-65-234 nginx]$ curl localhost:8080
<!DOCTYPE html>
<html>

<head>
    <title>CloudShell supports Docker Yeeeeeeeah!</title>
</head>

<body>
    <h1>CloudShell supports Docker Yeeeeeeeah!</h1>
    <p>We are very happy that Docker can be used in CloudShell environment!</p>
</body>

</html>

注意事項

利用可能なリージョンの制限と、データ量の大きいイメージを扱う場合は問題があるそうです

Docker is an open platform for developing, shipping and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. It enables you to build Dockerfiles inside AWS CloudShell, and build Docker assets with CDK. For information on which Regions are supported with Docker, see Docker Regions. You should be aware that Docker has limited space in the environment. If you have large individual images, or too many pre-existing Docker images, it can cause issues. For more information on Docker, see the Docker Documentation guide.

Pre-installed software

また、 CloudShell 環境のスペック拡張は現状サポートされていないため、以下を意識する必要があります。

  • 1 vCPU (仮想 CPU)
  • 2 GiB メモリ
  • 1 GB の永続ストレージ

https://docs.aws.amazon.com/cloudshell/latest/userguide/vm-specs.html#vm-configuration

ちなみにですが、未サポートである大阪リージョンでは、 docker はプリインストールされているが、デーモンが動いていない挙動でした。

[cloudshell-user@ip-10-6-21-208 ~]$ docker --version
Docker version 24.0.5, build ced0996
[cloudshell-user@ip-10-6-21-208 ~]$ docker image ls
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[cloudshell-user@ip-10-6-21-208 ~]$ sudo systemctl start docker
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
[cloudshell-user@ip-10-6-21-208 ~]$

Supported Regions for Docker

まとめ

以上、簡単ではありますが 「CloudShell で Docker が使えるようになってました」でした。

docker が使えることで、 DockerImageAsset コンストラクタ を使った IaC でイメージのビルド、プッシュする応用的な使い方を CloudShell でサポートしたのもCDK ユーザーにとって嬉しいのではないでしょうか。 個人的には SAM Local が使えるようになったのが嬉しいポイントです。

使い道いろいろありそうで、今後の「CloudShell で 〇〇 使ってみた」ブログに期待です。

AWS 事業本部コンサルティング部のたかくに(@takakuni_)でした!