LoginSignup
4
3

Docker Getting Started Tutorialの過程で遭遇した問題とその解決策

Posted at

はじめに

Docker Tutrial日本語訳して下さった記事を用いてDockerの勉強をしていた際に、それなりに躓いたので備忘録も兼ねて遭遇したエラーとその解決策を纏めます。
ご意見等ありましたらどうぞよろしくお願いいたします。

実行環境

  • OS: macOS Sonoma 14.4.1
  • CPU: Apple M3
  • Docker環境: Rancher Desktop ver.1.13.1

チュートリアルのウェブページが表示できない

症状

Docker チュートリアルのコンテナ起動後、http://localhost/にアクセスすると、404 page not found と表示されページが開けない。

原因

80番がすでに使用されていたことが原因。
以下のようにポート番号を変更してhttp://localhost:8000に接続することで対応。

$ docker run -d -p 8000:80 docker/getting-started
ポート使用状況確認法
$ lsof -i :80
COMMAND   PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ssh     11009 hoge   51u  IPv4 0x1762bdeg67433187      0t0  TCP *:http (LISTEN)

尚、コンテナも80番を使用していることになっていた。

$ docker ps
CONTAINER ID   IMAGE                        COMMAND                   CREATED          STATUS          PORTS                                                                                  NAMES
6d52ade3f072   docker/getting-started       "/docker-entrypoint.…"   31 seconds ago   Up 30 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp                                                      trusting_taussig

コンテナイメージを作成できない

症状

コンテナイメージのビルドを実行すると、yarnでのパッケージインストールでエラーが発生。

The engine "node" is incompatible with this module
エラー全文
$ docker build -t getting-started .
[+] Building 16.0s (8/8) FINISHED                                                                                                                       docker:rancher-desktop
 => [internal] load build definition from Dockerfile                                                                                                                      0.0s
 => => transferring dockerfile: 137B                                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                                         0.0s
 => => transferring context: 2B                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/node:12-alpine                                                                                                         6.6s
 => [1/4] FROM docker.io/library/node:12-alpine@sha256:d4b15b3d48f42059a15bd659be60afe21762aae9d6cbea6f124440895c27db68                                                   0.0s
 => [internal] load build context                                                                                                                                         0.0s
 => => transferring context: 11.14kB                                                                                                                                      0.0s
 => CACHED [2/4] WORKDIR /app                                                                                                                                             0.0s
 => [3/4] COPY . .                                                                                                                                                        0.1s
 => ERROR [4/4] RUN yarn install --production                                                                                                                             9.1s
------                                                                                                                                                                         
 > [4/4] RUN yarn install --production:                                                                                                                                        
0.467 yarn install v1.22.18                                                                                                                                                    
0.523 [1/4] Resolving packages...                                                                                                                                              
0.659 [2/4] Fetching packages...
9.009 error jest@29.3.1: The engine "node" is incompatible with this module. Expected version "^14.15.0 || ^16.10.0 || >=18.0.0". Got "12.22.12"
9.010 error Found incompatible module.
9.010 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
------
Dockerfile:4
--------------------
   2 |     WORKDIR /app
   3 |     COPY . .
   4 | >>> RUN yarn install --production
   5 |     CMD ["node", "src/index.js"]
--------------------
ERROR: failed to solve: process "/bin/sh -c yarn install --production" did not complete successfully: exit code: 1

原因

日本語記事を参照し、公式のDockerfileの記載を参照していなかったため、古いNode.jsを利用していたことが原因。(公式は node:18-alpineを使用していた)。公式の推奨するバージョンに従って実行することで解決。

# ↓バージョンが古い
FROM node:12-alpine 

WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

DockerHubへのイメージのpushに失敗

症状

自分のDockerHubアカウントにコンテナイメージをpushすると以下のエラーが発生。

denied: requested access to the resource is denied
エラー全文
$ docker push hoge/getting-started                
Using default tag: latest
The push refers to repository [docker.io/hoge/getting-started]
c2b1af8b43fe: Preparing 
bc38f5dff8b7: Preparing 
aad1308a826a: Preparing 
800ebf9a084d: Preparing 
b598656ed4f9: Preparing 
6724b338f31e: Waiting 
b09314aec293: Waiting 
denied: requested access to the resource is denied

原因

push前にログインしておく必要があった。以下のようにログイン後、再度pushして成功。
Passwordを入力後、Succeedまで少しラグがあるのでエンターボタンを連打しないように。

$ docker login -u 【アカウント名】
Password: 【パスワードを入力】
Login Succeeded
$ docker push hoge/getting-started

Play with Docker 上でコンテナイメージが実行できない

症状

ローカル環境では正常に実行できたコンテナイメージを、Play with Docker 上で実行しようとすると以下のエラーが発生。

WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64/v3) and no specific platform was requested
エラー全文
$ docker run -dp 3000:3000 hoge/getting-started
Unable to find image 'hoge/getting-started:latest' locally
latest: Pulling from hoge/getting-started
bca42909639: Pull complete 
ed2e267392d: Pull complete 
5cc68c01cd8: Pull complete 
84905eddcc1: Pull complete 
eafb42bd8f8: Pull complete 
52323b28df6: Pull complete 
8326d3fa2d0: Pull complete 
Digest: sha256:786c308d3e3a7e5766e3bf59848a33efc34246f5b665cea090c8855ca31c0d
Status: Downloaded newer image for hoge/getting-started:latest
WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64/v3) and no specific platform was requested
5e56e98e2cb7dd6850763d9b0df45c180661e21da99ce2f364be32463bef9d

原因

ローカル環境とPlay with Dockerでは、実行環境のプラットフォームの差異が原因。

  • ローカル環境(Mac)のプラットフォームは arm64
  • Play with Dockerのホストプラットフォームは x86_64
platformの確認法
  • Macでの確認
$ uname -m
arm64
  • Play with Dockerでの確認
$ uname -m
x86_64

プラットフォームを指定して再度pushしなおすことで解決。

$ docker build --platform linux/x86_64 -t getting-started-playground .
$ docker tag 987ed757aee0 hoge/getting-started-playground
$ docker push hoge/getting-started-playground

解決しなかった問題

Play with Dockerでコンテナの起動に成功後、チュートリアルに記載された方法でポートに接続すると ERR_EMPTY_RESPONSEが返される。これの原因は不明。

MySQLのコンテナを起動できない

症状

Docker内でMySQLデータベースサーバーコンテナを起動しようとすると以下のエラーが発生。

docker: no matching manifest for linux/arm64/v8 in the manifest list entries.
エラー全文
$ docker run -d \
    --network todo-app --network-alias mysql \
    -v todo-mysql-data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=secret \
    -e MYSQL_DATABASE=todos \
    mysql:5.7

Unable to find image 'mysql:5.7' locally
5.7: Pulling from library/mysql
docker: no matching manifest for linux/arm64/v8 in the manifest list entries.
See 'docker run --help'.

原因

日本語記事を参照し、公式のDockerfileの記載を参照していなかったため、古いmysqlイメージを利用していたことが原因。Docker Hubに公開されているmysql:5.7イメージは、マシンのアーキテクチャ(linux/arm64/v8)に対応していなかった。
公式チュートリアルの推奨するバージョンに従って実行することで解決。

$ docker run -d \
    --network todo-app --network-alias mysql \
    -v todo-mysql-data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=secret \
    -e MYSQL_DATABASE=todos \
    mysql:8.0

Docker Scanコマンドが使えない

症状

イメージのセキュリティスキャンを実行しようとすると以下のエラーが発生。

$ docker scan getting-started
docker: 'scan' is not a docker command.

原因

既にdocker scanは削除されており、現在はdocker scoutの利用が推奨されている(ドキュメント)。
Docker Desktop なしで Docker Engine を実行する場合(つまり今回のRancher Desktop)、スタンドアロン バイナリとしてあらかじめインストールする必要がある(ドキュメント)。

 $ curl -fsSL https://raw.githubusercontent.com/docker/scout-cli/main/install.sh -o install-scout.sh
 $ sh install-scout.sh

以下コマンドにてイメージの脆弱性を調査することができる。

$ docker scout cves hoge/getting-started
4
3
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
4
3