APC 技術ブログ

株式会社エーピーコミュニケーションズの技術ブログです。

株式会社 エーピーコミュニケーションズの技術ブログです。

Docker on CloudShell

はじめに

こんにちは、クラウド事業部の梅本です。 遅ればせながら、CloudShell で Docker が動くようになったとのこと。

aws.amazon.com

実際に CloudShell を起動して色々試していきたいと思います。

CloudShell で Docker

早速 CloudShell を起動して動作を確認してみます。

※実行は 2024/02/09 時点

# バージョンは 24.0.5
$ docker version
Client:
 Version:           24.0.5
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        ced0996
 Built:             Tue Nov 14 00:00:00 2023
 OS/Arch:           linux/amd64
 Context:           default

Server:
 Engine:
  Version:          24.0.5
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       a61e2b4
  Built:            Tue Nov 14 00:00:00 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.11
  GitCommit:        64b8a811b07ba6288238eefc14d898ee0b5b99ba
 runc:
  Version:          1.1.11
  GitCommit:        4bccb38cc9cf198d52bebf2b3a90cd14e7af8c06
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

# docker プロセスの確認(root で稼働)
# node コマンドで実行しているコードの中で、dockerd を子プロセスとして実行している模様
# 以下の結果は docker 関連のみ表示
$ ps aux --forest
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
cloudsh+       1  0.2  1.1 619636 43272 ?        Ssl  16:06   0:00 node /var/lib/amazon/cloudshell/on-container-launch.js
cloudsh+      16  0.0  0.0   4228  2972 ?        Ss   16:06   0:00 /bin/sh -c sudo dockerd | sudo tee /var/log/docker-daemon.log
root          18  0.0  0.1  14588  7444 ?        S    16:06   0:00  \_ sudo dockerd
root          26  0.1  1.8 1537604 73796 ?       Sl   16:06   0:00  |   \_ dockerd
root          51  0.3  1.1 1288804 45540 ?       Ssl  16:06   0:00  |       \_ containerd --config /var/run/docker/containerd/containerd.toml
root          19  0.0  0.1  14588  7412 ?        S    16:06   0:00  \_ sudo tee /var/log/docker-daemon.log
root          24  0.0  0.0   4928  1348 ?        S    16:06   0:00      \_ /usr/bin/coreutils --coreutils-prog-shebang=tee /usr/bin/tee /var/log/docker-daemon.log

# docker グループにも参加
$ id
uid=1000(cloudshell-user) gid=996(cloudshell-user) groups=996(cloudshell-user),997(docker)

# 起動したばかりなので、コンテナもイメージも無し
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

環境は一通り確認できたので、python のコンテナイメージに ansible をインストールして実行してみます。 以下、Dockerfile です。

FROM python:3.12.1-slim
RUN pip install --no-cache-dir ansible-core

早速ビルドして実行してみます。

# Build
$ docker build -t myansible:0.1 .
[+] Building 23.9s (6/6) FINISHED                                                                                                                                        docker:default
 => [internal] load .dockerignore                                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                                    0.0s
 => [internal] load build definition from Dockerfile                                                                                                                               0.0s
 => => transferring dockerfile: 160B                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/library/python:3.12.1-slim                                                                                                              2.4s
 => [1/2] FROM docker.io/library/python:3.12.1-slim@sha256:a64ac5be6928c6a94f00b16e09cdf3ba3edd44452d10ffa4516a58004873573e                                                        9.9s
...
 => [2/2] RUN pip install --no-cache-dir ansible-core                                                                                                                             10.5s
 => exporting to image                                                                                                                                                             1.1s 
 => => exporting layers                                                                                                                                                            1.1s 
 => => writing image sha256:d15c060d7d0bb8b3cd914906714050795bc6282c590f7e2a55b4da80104883fa                                                                                       0.0s 
 => => naming to docker.io/library/myansible:0.1                                                                                                                                   0.0s 

$ docker images                                                                                                                                      
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE                                                                                                                              
myansible    0.1       d15c060d7d0b   5 minutes ago   177MB


# Run
$ docker run --rm myansible:0.1 ansible --version
ansible [core 2.16.3]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.12/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.12.1 (main, Feb  1 2024, 04:33:21) [GCC 12.2.0] (/usr/local/bin/python)
  jinja version = 3.1.3
  libyaml = True

# ansible コマンドは実行しても失敗することを確認
$ docker run --rm myansible:0.1 ansible -i "127.0.0.1," all -m ping
127.0.0.1 | FAILED! => {
    "msg": "Unable to execute ssh command line on a controller due to: [Errno 2] No such file or directory: b'ssh'"
}

build/run もスムーズにできました。今回 push は行いませんが、ECR への push スムーズにできそうです。

コンテナへのアクセスも確認してみます。 nginx のコンテナを起動してアクセスしてみます。

$ docker run -d -p 8080:80 nginx:1.25.3

$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                   NAMES
ea654a87ba09   nginx:1.25.3   "/docker-entrypoint._"   7 seconds ago   Up 3 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   lucid_bhaskara

$ curl -I localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.25.3
Date: Thu, 08 Feb 2024 16:41:55 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 24 Oct 2023 13:46:47 GMT
Connection: keep-alive
ETag: "6537cac7-267"
Accept-Ranges: bytes

無事にポート指定してアクセスできました。

最後に気になるディスク関連を確認します。

# 永続化されるホームディレクトリは 1GB
$ df -hT ~
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/loop0     ext4  974M  7.9M  899M   1% /home/cloudshell-user

# docker のディレクトリはホームディレクトリとは異なりエフェメラルな領域
# / ディレクトリと一緒
$ df -hT /
Filesystem     Type     Size  Used Avail Use% Mounted on
overlay        overlay   16G  5.6G  9.2G  38% /

$ df -hT /var/lib/docker
Filesystem     Type     Size  Used Avail Use% Mounted on
overlay        overlay   16G  5.6G  9.2G  38% /

# docker pull して ディスクの使用量を確認
# 確認のため大きいサイズのイメージを pull
$ docker pull python:3.9.18-bullseye

$ df -hT ~
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/loop0     ext4  974M  7.9M  899M   1% /home/cloudshell-user

$ df -hT /var/lib/docker
Filesystem     Type     Size  Used Avail Use% Mounted on
overlay        overlay   16G   11G  4.3G  72% /

/var/lib/docker がエフェメラルの領域にあるため、CloudShell への再アクセスや再起動の際にコンテナイメージや起動中のコンテナは削除されてしまうので注意しましょう。 必要以上にディスクを使用しているようにも見えますが・・・謎です。

CloudShell のスペックも高いわけではないので、利用の際は考慮しましょう。

仕様やトラブルシューティングの参考リンクを貼っておきます。

docs.aws.amazon.com

docs.aws.amazon.com

まとめ

CloudShell で docker が実行できるようになり、コンテナをよく触っている身としては非常にありがたいです。記事作成時の環境を CloudShell にすることで、「記事の内容をすぐに試したい!」ってことも楽になりますね。コンテナを使って各種ツールを起動できることから、利用用途は増えそうなので今後も利用していきたいと思います!


APCはAWSアドバンスドティアサービスパートナー認定を受けております。

AWSアドバンスドティアサービスパートナー

その中で私達クラウド事業部はAWSなどのクラウド技術を活用したSI/SESのご支援をしております。

www.ap-com.co.jp

また、AWSの運用自動化ツールも展開しております。

www.ap-com.co.jp

本記事の投稿者: 梅本
コンテナや k8s をメインにインフラ系のご支援を担当しています。
AWS は現在学び直し中! 普段は QiitaZenn に k8s を中心とした記事を投稿しております。よろしければ。