5
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?

More than 1 year has passed since last update.

Mac M1でビルドしたDockerイメージがEC2で起動できなかった

Last updated at Posted at 2022-06-19

EC2でイメージからコンテナを起動しようとしたらできなかったので、自分用にメモ。

  • 何やら警告が

WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64)

  • イメージ確認
docker images

REPOSITORY                                                      TAG       IMAGE ID       CREATED        SIZE
xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/kinoko-test   tag3.0    7740c0369ffc   29 hours ago   157MB
  • コンテナ起動(失敗)
docker run --name nginx-tutorial -d -p 8080:80 xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/kinoko-test:tag3.0

WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64) and no specific platform was requested
4550cc5b7f4ba00f0fffe27a9525bce85e2c61fc5cda2f5448735d56c6c4aaa6
  • コンテナステータスがExited
docker ps -a

CONTAINER ID   IMAGE                                                                  COMMAND                  CREATED         STATUS                     PORTS     NAMES
4550cc5b7f4b   xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/kinoko-test:tag3.0   "nginx -g 'daemon of…"   2 seconds ago   Exited (1) 2 seconds ago             nginx-tutorial
  • docker logs でログを確認
docker logs 4550cc5b7f4b

standard_init_linux.go:228: exec user process caused: exec format error

このエラーは、Docerfileをビルドした M1 Macとコンテナを実行するPCのCPUアーキテクチャが異なるために発生する模様

対策

  • イメージビルド時にプラットフォームを指定
docker build --platform amd64 -t kinoko-nginx:tag3.0 .
  • ECRへプッシュ
docker push xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/kinoko-test:3.0
  • ダッシュボードはこんな感じ

image.png

EC2側操作

  • dockerインストール
sudo yum update -y
sudo yum -y install docker
  • docker起動
sudo service docker start
  • EC2ユーザーをグループに追加
sudo usermod -a -G docker ec2-user
  • IAMロール

AmazonEC2ContainerRegistryFullAccessを付与

  • セキュリティグループ

8080を許可

  • ECRログイン
aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/kinoko-test
  • イメージ取得
docker pull xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/kinoko-test:3.0

ローカルPCからアクセス

http://[EC2インスタンスIP]:8080/ で無事にアクセスできました。

image.png

5
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
5
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?