ろきメモ【ROKI MEMO】- ろきsanの備忘録 -

ろきさんの備忘録。プログラミング学習記録や開発記録、および学んだ知識等のアウトプットとシェアを目的に書いています。たまに普通のことも書きます。

【プロキシにハマった】UbuntuにDockerをインストールして、プロキシ設定して動作確認する方法

スポンサーリンク

Linux(Ubuntu)にDockerをインストールした際、プロキシでハマったので、インストール方法と確認方法、そしてプロキシ設定方法をあわせて備忘録として残しておこう。

というわけで今日の目次

スポンサーリンク



1. インストールのための準備

Ubuntuへのインストールはこちらから
docs.docker.com

基本的には、ここの「Install using the repository」の部分の通りに行う。
まずは、準備の部分から。なので、その中の「SET UP THE REPOSITORY」の部分。

1. apt-getのアップグレード(Update the apt package index)
とりあえず使うツールはアップグレードしとかないとね。最新がインストールできないときがよくあるから。

$ sudo apt-get update


2. aptがHTTPS経由でリポジトリを使えるように各パッケージをインストール(Install packages to allow apt to use a repository over HTTPS)
必要なパッケージを一括で取得する。

$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

※サイトにある「 \ 」は使わず全部1行で表示しています。


3. dockerの公式GPGキーを追加(Add Docker's official GPG key)
GPGキーってのがあるんだね。

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -


4.フィンガープリントを確認

$ sudo apt-key fingerprint 0EBFCD88

9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 があればよし。


5. リポジトリの設定
安定版のリポジトリを設定する。

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

※サイトにある「 \ 」は使わず全部1行で表示しています。


ここまでが準備。
次に実際にインストールしていく。

2. Dockerのインストール

実際にインストールしていく。
上記のURLの「INSTALL DOCKER ENGINE」を見ながら行っていく。

1. apt-getのアップグレード(Update the apt package index)
さっきやったけど、書いてあるからとりあえず実行(アップグレード済みって言われる)

$ sudo apt-get update


2. Dockerインストール
最新版をインストール

$ sudo apt-get install docker-ce docker-ce-cli containerd.io

これでインストール完了。

バージョン確認してみる。

$ docker -v
Docker version 19.03.7, build 7141c199a2

よし。インストールできてるようだ。

スポンサーリンク



3. プロキシの設定

さて、インストールも完了したので、(上記URLにも書いてあるように)動作確認でhello-worldイメージを実行してみる。

$ sudo docker run hello-world
[sudo] user のパスワード: 
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on 127.0.1.1:53: no such host.
See 'docker run --help'.

あれ?エラーになった。

コレたぶんプロキシのせい。
ということでプロキシの設定をする。

参考にするのが、また公式のここのサイト
docs.docker.com

ここの「HTTP/HTTPS proxy」の部分。

やることは
1. ディレクトリ作成
2. ファイル作成
3. そのファイルにプロキシ情報書き込む
4. Docker再起動

では1つずつやっていく
1. ディレクトリ作成
/etc/systemd/system/docker.service.d というディレクトリを新規作成

$ sudo mkdir -p /etc/systemd/system/docker.service.d

※mkdir : ディレクトリ作成コマンド
※-p : 親ディレクトリなかったら作成するオプション
※sudo : 作成場所がルートディレクトリなので必要


2. ファイル作成
作成したディレクトリに、http-proxy.conf というファイルを新規作成

$ sudo touch /etc/systemd/system/docker.service.d/http-proxy.conf

※touchは空ファイル作成


3. そのファイルにプロキシ情報書き込む
ファイルの中身は、「HTTP_PROXY」なのか「HTTPS_PROXY」なのか、その両方なのかで書く内容が変わる。
両方だったら以下のように記述する。

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80"
Environment="HTTPS_PROXY=https://proxy.example.com:443"

※プロキシ情報はサンプル


4. Docker再起動
daemonのリロードとdockerの再起動して完了

daemonをリロード

$ sudo systemctl daemon-reload

docker再起動

$ sudo systemctl restart docker

プロキシ設定が反映されているか確認

$ sudo systemctl show --property=Environment docker

Environment=HTTP_PROXY=http://proxy.example.com:80 HTTPS_PROXY=https://proxy.example.com:443

表示内容がファイルに記述した内容と同じであればOK

スポンサーリンク



4. 改めてサンプルのhello-worldイメージを実行

改めて元のインストールサイト(https://docs.docker.com/engine/install/ubuntu/)に戻り、hello-worldイメージでDockerの動作確認を行う。

$ sudo docker run hello-world
[sudo] user のパスワード: 
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
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イメージの確認

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB


ちなみに、取得したhello-worldイメージは、Docker Hubのこちら(https://hub.docker.com/_/hello-world


よし、今日も無事備忘録完了。
Dockerについては基本的なコマンドなどもまとめたいな。


以上。


Dockerのおすすめ本ではこちらの本がいろんなところで紹介されている。