LoginSignup
19
12

More than 5 years have passed since last update.

Docker のプロキシ設定

Last updated at Posted at 2018-06-04

社内などのプロキシ環境内でDocker build を行うための設定についてです。情報が混在していて正しく動作させるのに苦労したので、ここで共有します。

環境

名称      バージョン、パッケージ名称
Linux Ubuntu 16.04.1 LTS
デーモン管理 systemd
Docker    Docker CE 18.03.1-ce, build 9ee9f40

必要な設定

先に結論を言ってしまうと、以下の2つの設定の記述を行った後にdocker の再起動が必要となります。

DNS設定

/etc/docker/daemon.json に、/etc/resolve.conf と同様に以下のように記述します。

/etc/docker/daemon.json
{
    "dns": ["nameserver-ip-address"],
    "dns-search": ["search-url"]
}

プロキシ設定

~/.config/config.json に以下のように記述します。

~/.config/config.json
{
    "proxies":
    {
        "default":
        {
            "httpProxy": "http://<ip-address>:<port>/",
            "httpsProxy": "http://<ip-address>:<port>/",
            "noProxy": ""
        }
    }
}

再起動

$ sudo systemctl restart docker

動作確認方法

プロキシ設定が正しくないと、Docker build において、Dockerfile に記述されたapt-get 等が失敗します。以下のDockerfile で、HTTP とHTTPS プロキシの両方について確認しました。

Dockerfile

Dockerfile
FROM ubuntu:16.04

# http proxy の設定が正しくないと、これが成功しない
RUN apt-get update

# add-apt-repository のインストールに必要
RUN apt-get install -y software-properties-common

# https_proxy の設定が正しくないと、これが成功しない
RUN add-apt-repository ppa:team-gcc-arm-embedded/ppa

ビルドコマンドとログ

$ docker build --no-cache -t name .
Sending build context to Docker daemon  67.58kB
Step 1/2 : FROM ubuntu:16.04
 ---> 0b1edfbffd27
Step 2/2 : RUN apt-get update
 ---> Running in eb52a908e50a

# HTTP プロキシ設定が正しくないと、ここでしばらく待ち状態になった挙句に

Err:1 http://archive.ubuntu.com/ubuntu xenial InRelease
  Temporary failure resolving 'archive.ubuntu.com'

# アドレス解決に失敗する

Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for ca-certificates (20170717~16.04.1) ...
Updating certificates in /etc/ssl/certs...
148 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
Processing triggers for dbus (1.10.6-1ubuntu3.3) ...
Removing intermediate container a71810f0b742
 ---> 5d7f92217c65

# HTTPS プロキシ設定が正しくないと、add-apt-repository に失敗する

Step 4/5 : RUN add-apt-repository ppa:team-gcc-arm-embedded/ppa
 ---> Running in 857e176c6eda

補足

前述の設定を行わなくとも、ビルドコマンドへの引数でプロキシサーバの指定が可能です。ただ、DNS の設定をしていない場合は、ここでプロキシサーバのIP アドレスを直接指定する必要があります。

$ docker build -t tag --build-arg http_proxy=http://<ip-address>:<port>/ --build-arg https_proxy=https://<ip-address>:<port>/ .

参考情報

19
12
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
19
12