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?

Dockerインストール後のrunコマンドで「 401 Unauthorized」が出た場合の対処法

Last updated at Posted at 2025-05-04

最近PCを変えて、docker環境を整えたときに以下の事象が発生したので、
書いておこうと思います。

環境

  • OS:macOS Sonoma バージョン14.6
  • チップ:Apple M3
  • dockerバージョン:Docker version 28.0.4

発生した事象

brew install dockerを実行した後、docker run hello-worldを実行すると、以下のエラーが出力された。

Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: failed to resolve reference 
"docker.io/library/hello-world:latest": failed to authorize: 
failed to fetch oauth token: unexpected status from GET request to 
https://auth.docker.io/token?scope=repository%3Alibrary%2Fhello-world%
3Apull&service=registry.docker.io: 401 Unauthorized

結論と対処法

結論

Docker Hubへの接続設定ができていなかった。

過程

原因は、エラー内容にもある通り、401 Unauthorized
dockerはimageがローカルに存在しない場合、Docker Hubにimageを取得しに行く。
その際に、認可が通ってなくてエラーとなっている。
つまり、エラー原因はDocker Hubへの接続がログインできてなかったことにあると考えた。
また、同様の事象が別記事でも起こってることも確認したので、結論に至った。

対処法

以下の手順で、Docker Hubへの接続設定を行うことで可能

  1. Docker Hubでユーザ登録をする
  2. docker login -u <username>の実行
  3. 以下のように、ワンタイムコードとコード認証用のリンクが貼られるので、リンクをクリックする(リンク先で認証しないとdocker loginコマンドが終了しない)
    Your one-time device confirmation code is: ???????
    Press ENTER to open your browser or submit your device code here: https://login.docker.com/activate
    
  4. 以下のような画面に移動するので、ワンタイムコードを入力して認証を行う
    image.png

以上で認証完了。すると、CLI側でLogin Succeededと表示される。

最後に、最初のdocker run hello-worldを実行すると以下のようにDockerHubからのPullが完了してHello from Docker!と出力されます。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c9c5fd25a1bd: Pull complete 
Digest: ーーーーーーーーーーー
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.
    (arm64v8)
 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/

参考

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?