プロキシ環境下でDockerを使うための設定方法を、apt Docker デーモン・コンテナの3箇所に分けて説明します。


apt 周りのプロキシ設定

この記事の確認環境は WSL Ubuntu20.04です。

1
2
3
4
5
6
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal

まず、WSLのProxy設定方法 の記事を参考に設定してください。 WSL でもUbuntuでも同じです。特に apt.conf の設定を忘れないでください。 ここを間違えると下記のように「そんなもんDockerリポジトリには存在せんぞ」と怒られてしまいます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$ sudo add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable"
$ sudo apt-get update
Ign:1 https://download.docker.com/linux/ubuntu focal InRelease
Err:2 https://download.docker.com/linux/ubuntu focal Release
  Could not handshake: An unexpected TLS packet was received. [IP: XX.XX.XX.XX:YY]
Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:4 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Reading package lists... Done
E: The repository 'https://download.docker.com/linux/ubuntu focal Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

DockerデーモンのProxy設定

さらに/etc/default/dockerの修正が必要です。そこにコメントアウトされた状態でProxyの設定例が書いてあります。そこを自分の環境に合わせて修正します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ cat /etc/default/docker
# Docker Upstart and SysVinit configuration file

#
# THIS FILE DOES NOT APPLY TO SYSTEMD
#
#   Please see the documentation for "systemd drop-ins":
#   https://docs.docker.com/engine/admin/systemd/
#

# Customize location of Docker binary (especially for development testing).
#DOCKERD="/usr/local/bin/dockerd"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"

# If you need Docker to use an HTTP proxy, it can also be specified here.
export http_proxy="http://username:password@proxy-host:proxy-port"
export https_proxy="http://username:password@proxy-host:proxy-port"
export ftp_proxy="http://username:password@proxy-host:proxy-port"

# This is also a handy place to tweak where Docker's temporary files go.
#export DOCKER_TMPDIR="/mnt/bigdrive/docker-tmp"

さらに、systemctl の設定も行います。

1
sudo systemctl edit docker

と実行するとエディタが開きますので下記のようにProxyを設定してください。

1
2
3
[Service]
Environment="HTTP_PROXY=http://proxy-host:proxy-port/"
Environment="HTTPS_PROXY=http://proxy-host:proxy-port/"

コンテナ起動時のプロキシ設定

コンテナの作成もしくは起動時に、環境変数がコンテナ内へ自動的に設定するように設定ファイルを作成します。 プロキシサーバを使うように Docker を設定にあるようにホスト側に ~/.docker/config.json を作ればOKです。 下記のような感じですね。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ mkdir -p ~/.docker/
$ vim ~/.docker/config.json
$ cat ~/.docker/config.json
{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://192.168.1.12:3128",
     "httpsProxy": "http://192.168.1.12:3128",
     "noProxy": "*.test.example.com,.example2.com,127.0.0.0/8"
   }
 }
}

まとめ

これでDockerイメージをプルしてHello Worldまではできるようになったはずです。