42
37

More than 3 years have passed since last update.

プロキシ環境下でdocker image buildしたい

Last updated at Posted at 2019-01-04

やりたいこと

  • プロキシ環境下でdocker image buildしたい
    • yumaptを利用してパッケージインストールする時に困る
  • プロキシサーバーは、サーバー名で指定したい
  • Dockerfileには、ENV http_proxyとかENV https_proxyとか書きたくない

--build-argを使う

解決方法は--build-arghttp_proxyhttps_proxyを、docker build時に指定してあげればOKです。

例えば、こんな感じ。

$ docker image build --build-arg http_proxy=http://your-proxy-host:your-proxy-port --build-arg https_proxy=http://your-proxy-host:your-proxy-port -t charon/your-container-image:latest .

さらに、プロキシサーバーを名前で指定したい場合で、かつDockerコンテナ内からプロキシサーバーの名前解決が面倒な場合。

Container networking

--dnsオプションを使用してもよいのですが、それも面倒。

dig +shortでいいんではないでしょうか?

こんな感じで。

docker-proxy-build

#!/bin/bash

docker image build --build-arg http_proxy=http://$(dig +short your-proxy-host):your-proxy-port --build-arg https_proxy=http://$(dig +short your-proxy-host):your-proxy-port "$@"

このスクリプトに、実行を付けて/usr/loca/binにでも置いておきましょう。

$ chmod a+x docker-proxy-build
$ sudo cp docker-proxy-build /usr/local/bin

使う時。

$ docker-proxy-build -t charon/your-container-image:latest .
42
37
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
42
37