LoginSignup
5
1

More than 3 years have passed since last update.

VagrantをProxy経由で利用する

Last updated at Posted at 2019-09-03

はじめに

Proxy経由でインターネットに接続している環境で、Vagrantを利用するとBoxイメージのダウンロードすらできません。
いくつかProxy経由でVagrantを利用する記事を見かけましたが、誤った設定も多いので、自分のメモとしてまとめます。

イメージダウンロード用の設定

Proxy経由でBoxイメージをダウンロードするには、環境変数 HTTP_PROXY, HTTPS_PROXY を設定します。

> set http_proxy=(ホスト名):(ポート番号)
> set https_proxy=(ホスト名):(ポート番号)

たまに、以下のようにホスト名にプロトコルも記載している記述を見ますが、これだと正常に動作しません。

// 誤った環境変数の設定
> set http_proxy=http://(ホスト名):(ポート番号)
> set https_proxy=https://(ホスト名):(ポート番号)

この環境変数の設定により、vagrant box addコマンドでBoxイメージがダウンロードできるようになります。

vagrant up 時のProxy設定

vagrant upにより、vagrantfileの内容で仮想環境を構築していきますが、その際のProxy設定はVagrantのPlugin(vagrant-proxyconf)が必要になります。

vagrant-proxyconf のインストール

以下のコマンドでPluginをインストールします。

> vagrant plugin install vagrant-proxyconf

vagrantfile の更新

Pluginのインストールができたら、vagrantfileにProxyの記述を追記します。

vagrantfile
if Vagrant.has_plugin?("vagrant-proxyconf")
  config.proxy.http     = "http://(ホスト名):(ポート番号)"
  config.proxy.https    = "http://(ホスト名):(ポート番号)"
  config.proxy.no_proxy = "localhost,127.0.0.1"
end

config.proxy.httpsに設定している値もhttp://~となっていますが、問題無いようです。

vagrant up

ここまで設定できたら、vagrant upで仮想環境立ち上げましょう。
起動時に以下のようなメッセージが表示されると思います。

==> default: Configuring proxy environment variables...
==> default: Configuring proxy for Yum...

上記メッセージの通り、起動したゲストOSの環境変数とYum設定にProxy情報が反映されています。

/etc/yum.conf
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
proxy=http://(ホスト名):(ポート番号) # Proxy設定が追記されている
proxy_username=
proxy_password=
環境変数
http_proxy=http://(ホスト名):(ポート番号)
https_proxy=http://(ホスト名):(ポート番号)
HTTP_PROXY=http://(ホスト名):(ポート番号)
HTTPS_PROXY=http://(ホスト名):(ポート番号)

これにより、yum などもProxy経由でダウンロードが行えます。

Docker

DockerでBuildやpullする場合、以下の設定をdocker.serviceに追記する。

docker.service
[Service]
Environment="HTTP_PROXY=http://(ホスト名):(ポート番号)/" "HTTPS_PROXY=http://(ホスト名):(ポート番号)/"

docker.serviceファイルは以下のどこかにあると思います。

/etc/systemd/system/docker.service
/lib/systemd/system/docker.service
/etc/systemd/system/docker.service.d配下

※HTTPS_PROXYに設定する値は「http」で設定してください。「https」で設定してdocker buildしたら、以下のようなエラーが表示されました。

Get https://registry-1.docker.io/v2/: proxyconnect tcp: tls: first record does not look like a TLS handshake

参考

https://weblabo.oscasierra.net/windows-vargent-proxy/
https://weblabo.oscasierra.net/vagrant-proxyconf/
https://protocol.nekono.tokyo/2018/09/10/%E3%83%97%E3%83%AD%E3%82%AD%E3%82%B7%E7%92%B0%E5%A2%83%E3%81%A7vagrant%E3%82%92%E5%88%A9%E7%94%A8%E3%81%99%E3%82%8B/

5
1
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
5
1