0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazon Linux 2023でDockerを導入・設定する手順

Posted at

環境

  • EC2
    • OS: Amazon Linux 2023
  • EC2インスタンスは起動済みであること

手順1:Dockerのインストール

パッケージのアップデート

$ sudo dnf -y update
Last metadata expiration check: 9:57:17 ago on Sun May  4 00:30:59 2025.
Dependencies resolved.
Nothing to do.
Complete!

Dockerのインストール

sudo dnf install docker

Dockerのバージョン確認をすることで、Dockerがインストールされたかを確認します

$ docker -v
Docker version 25.0.8, build 0bab007
$ which docker
/usr/bin/docker

手順2:Dockerの起動

sudo systemctl start docker.service

Dockerが起動しているか確認

sudo systemctl status docker.service

activate(running)と表示されるのでDockerが起動していることが確認できます

Dockerのステータス

ステータス 意味
created コンテナは作成されたが、まだ実行されていない
restarting コンテナが何らかの理由で停止し、自動的に再起動しようとしている
running コンテナが現在稼働中
removing コンテナが削除されている最中
paused コンテナは一時停止状態
exited コンテナのプロセスが終了した(正常終了または異常終了)
dead コンテナが完全に停止し、正しくクリーンアップされなかった状態

手順3:Dockerの自動起動設定

EC2を停止、再起動してもDockerが自動的に起動するようになります

自動起動設定になっているかどうか確認

$ sudo systemctl is-enabled docker.service
disabled

自動起動を有効化

sudo systemctl enable docker.service

再度、自動起動設定になっているかどうか確認

$ sudo systemctl is-enabled docker.service
enabled

以上で自動起動設定を有効にすることができました

手順4:Dockerグループにユーザー追加

ec2-userをDockerグループに追加してから、再起動を行うと、sudoなしで Dockerコマンドを使えるようになります

sudo usermod -aG docker ec2-user

手順5:Docker composeのインストール、設定

現時点での最新版をGithubのreleasesページで確認してからインストールしてください。(2025/05時点ではv2.35.1が最新)

$ sudo curl -SL https://github.com/docker/compose/releases/download/v2.35.1/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 70.2M  100 70.2M    0     0  34.5M      0  0:00:02  0:00:02 --:--:-- 66.1M

実行権限を追加

$ ls -l /usr/local/bin/docker-compose
-rw-r--r--. 1 root root 73699264 May  7 01:31 /usr/local/bin/docker-compose

$ sudo chmod +x /usr/local/bin/docker-compose

$ ls -l /usr/local/bin/docker-compose
-rwxr-xr-x. 1 root root 73699264 May  7 01:31 /usr/local/bin/docker-compose

バージョン確認

docker-compose -v
Docker Compose version v2.35.1

手順6:Dockerの動作テスト

# 現在のDocker環境の詳細情報を表示するコマンド
$ docker info
Client:
 Version:    25.0.8
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  0.12.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 25.0.8
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 05044ec0a9a75232cad458027ca83437aae3f4da
 runc version: 6c52b3fc541fb26fe8c374d5f58112a0a5dbda66
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.1.134-150.224.amzn2023.x86_64
 Operating System: Amazon Linux 2023.7.20250428
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 949.4MiB
 Name: ip-10-0-0-154.ap-northeast-1.compute.internal
 ID: a176f683-0026-492d-b80b-ead5c2ef4324
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

または

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:c41088499908a59aae84b0a49c70e86f4731e588a737f1637e73c8c09d995654
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/

無事にDockerが動くことを確認できました

おまけ:Dockerのcontainer, image, volumeを削除する方法

実行中のすべてのコンテナを一括で停止

docker stop $(docker ps -q)

container削除

docker container prune

image削除

docker image prune -a

volume削除

docker volume prune -a

まとめ

Amazon Linux 2023のEC2インスタンスにDockerとDocker Composeをインストール・設定し、正常に動作することを確認しました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?