LoginSignup
0
0

More than 3 years have passed since last update.

WindowsServer2019 Proxy環境でWSL+Dockerを使う

Posted at

目次

動作環境

OS Name: Microsoft Windows Server 2019 Standard
OS Version: 10.0.17763 N/A Build 17763

Domain参加していてProxy環境です。

きっかけ

WindowsServer2019にはWindowsContainerという機能がありますが、Dockerhubにはpullできるものが少なく勝手が悪い。
Hyper-VでLinaxの仮想を立てても良いですが、ドメイン参加や手続きが面倒です。
そんな中でWSLという機能を見つけたのでそれを使ってやってみようと思いました。
※以前はWSLではDockerは動かなかったようですが、今は動くようです。

WSLでUbuntu18.04を使えるようにする

Ubuntu18.04でsudo apt updateができるところまでやります。
参考 https://docs.microsoft.com/ja-jp/windows/wsl/install-on-server

※WSLだとUbuntu20.04はDockerがうまく動かなかったので、Ubuntu18.04のディストリビューションを選択
参考 https://qiita.com/AumyF/items/eaf42dd3345a2285ff9e
※GUIでポチポチやる場合は以下が参考になります。
https://pswork.jp/wsl/windows-2019-wsl-install/

PowerShell
# 機能有効化
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
# ディストリビューションのDL
$url = "https://wslstorestorage.blob.core.windows.net/wslblob/Ubuntu_1804.2019.522.0_x64.appx"
Invoke-WebRequest $url -OutFile Ubuntu1804.zip
# 展開
Expand-Archive .\Ubuntu1804.zip .\Ubuntu1804
# Ubuntu18.04の実行
.\Ubuntu1804\ubuntu1804.exe

上記を実行して4,5分待つとusername、passwordを聞かれるので適当に入力します。
次にUbuntu側でProxyの設定を行って、sudo apt updateします。

以下のshファイルを作成して、上記で作成したユーザのホームディレクトリに配置します。
エクスプローラーから配置できます。(Ubuntuからexitしておかないとエラーになる場合がある)
参考 https://qiita.com/quzq/items/1096c638c0d86795be13

proxysetting.sh
#!/bin/bash

# setting bash proxy
cat <<'EOF' >> /etc/profile.d/proxy.sh
export http_proxy="http://[proxy_ip]:[proxy_port]"
export https_proxy="http://[proxy_ip]:[proxy_port]"
EOF

# setting apt proxy
cat <<'EOF' >> /etc/apt/apt.conf
Acquire::http::proxy "http://[proxy_ip]:[proxy_port]";
Acquire::https::proxy "http://[proxy_ip]:[proxy_port]";
EOF

作成したファイルを実行して、一度exitします。

Bash
cd ~/
sudo sh proxysetting.sh
exit

もう一度ubuntuに入りなおすときはwslまたはubuntu1804.exeを実行します。
参考 https://qiita.com/mizutoki79/items/f6ba08b7f73133102c91

PowerShell
wsl 

sudo apt updateして、エラーなく動けば完了です。

Bash
sudo apt update && sudo apt upgrade

Ubuntu18.04でDockerを実行できるようにする

以下のshファイルを作成して、上記で作成したユーザのホームディレクトリに配置します。

※Dockerは最新のバージョンだとエラーがでて動きませんでした。
※以下のスクリプトでは18.06.1でインストールしています。

installdockerce.sh
#!/bin/bash

# 必要なもののインストール
sudo apt install -y \
   apt-transport-https \
   ca-certificates \
   gnupg-agent \
   software-properties-common
# gpgキーの追加 sudoでcurlは-E付けないとProxyでとまる
sudo -E curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# リポジトリの追加
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
# version指定してインストール
sudo apt install docker-ce=18.06.1~ce~3-0~ubuntu
# sudo付けないでdockerコマンド使えるようにする
sudo usermod -aG docker $USER
  • 環境変数を引き継いでdockerdをバックグラウンドで起動させるshファイルの作成
    ※サービスのところにProxyのコンフィグファイルを追加するやり方だとproxy設定が反映されませんでした。
    参考 https://qiita.com/dkoide/items/ca1f4549dc426eaf3735
startdockerd.sh
#!/bin/bash

# proxy用の環境変数
export http_proxy=http://[proxy_ip]:[proxy_port]
export https_proxy=http://[proxy_ip]:[proxy_port]
# これないとdockerdの起動でエラー
sudo cgroupfs-mount
# バックグラウンドで上記の環境変数引き継いで実行
sudo -bE dockerd

Ubuntuでinstalldockerce.shを実行してDockerをインストールします。

Bash
cd ~/
sudo sh installdockerce.sh
# ~~~
# 出力をみて、エラーなくインストールできていることを確認
# ~~~
exit

wslコマンドでstartdockerd.shを実行し、Ubuntuに入ります。

PowerShell
wsl sudo sh startdockerd.sh
wsl

dockerコマンドを実行して、エラーなどがなければ完了です。

Bash
docker info
# これでエラーの表示がでず、設定したproxyの環境変数が表示されていればok
docker run hello-world
# 実行して下記がでれば完了
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/
0
0
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
0
0