LoginSignup
5
5

More than 1 year has passed since last update.

Proxy環境でWSL2+Docker

Last updated at Posted at 2021-09-29

説明

  • proxy環境でもWSL2使ってみたいひと向け
  • ほぼCLIからやります
  • DockerforDesktopがうまく動かせなかったので、コマンドでやる人向け
  • 最後に作成した環境使ってnpm installの例があります
  • 備忘録的な意味合いが多いため若干説明不足ぎみです

WSL2

PowerShell
# 機能有効化
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
# wsl2用の更新プログラム
$url = https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
Invoke-WebRequest $url -OutFile wsl_update_x64.msi
# 更新プログラムを手動で実行する
Start-Process wsl_update_x64.msi -Wait
rm -force wsl_update_x64.msi
# wslのバージョンを2に指定
wsl --set-default-version 2
# ディストリビューションのDL
$url = "https://wsldownload.azureedge.net/Ubuntu_2004.2020.424.0_x64.appx"
Invoke-WebRequest $url -OutFile Ubuntu2004.zip
# 展開
Expand-Archive .\Ubuntu2004.zip .\Ubuntu2004
# Ubuntu20.04の実行
.\Ubuntu2004\ubuntu2004.exe

  • 途中でアップデートのインストーラが起動するのでインストールを完了させてください
  • 上記を実行して数分待つとusername、passwordを聞かれるので適当に入力します
  • その後に以下をコピペして実行します
  • [proxy]のところにはProxyサーバのIPとかPortをいれてください
cat <<'EOF' >> /etc/profile.d/proxy.sh
export http_proxy="http://[proxy]"
export https_proxy="http://[proxy]"
EOF

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

exit

  • もう一度ubuntuに入りなおすときはwslを実行します
  • sudo apt update && sudo apt upgradeして、エラーなく動けば完了です
  • エクスプローラーからは\\wsl$をアドレスに入れるとubuntuを参照できます

Docker

  • 下記のファイルを作成して、ubuntuで実行してください
dockerinstall.sh
# 必要なもののインストール
sudo apt install -y \
   apt-transport-https \
   ca-certificates \
   gnupg-agent \
   software-properties-common
# gpgキーの追加
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
# sudo付けないでdockerコマンド使えるようにする
sudo usermod -aG docker $USER
# install docker-compose
sudo apt install -y docker-compose

exit

DockerのProxy設定

Dockerクライアントとサーバの両方に設定していきます

  • docker client
  • 下記を実行して終了です
  • [proxy]はProxyのIP、Portをいれてください
# make .docker 
mkdir ~/.docker
# docker proxy settings
cat <<'EOF' >> ~/.docker/config.json
{ "proxies": { "default": { "httpProxy": "http://[proxy]", "httpsProxy": "http://[proxy]", "noProxy": 192.168.*,local*" } } }
EOF

  • docker server
    • wslからexitしたら外からDockerdを起動します
    • 下記のファイルをubuntu側の~/に設置してWindowsからwsl sudo sh ~/startdocker.sh
    • その後にwslでubuntu入ってdockerコマンドたたくと動きます
    • 注意 wsl sudo sh ~/startdocker.shは起動したら一回目は必ず行う必要あり
startdocker.sh
#!/bin/bash

export http_proxy=http://[proxy]
export https_proxy=http://[proxy]

#sudo cgroupfs-mount
sudo -bE dockerd

環境テスト

  • nodejs 9.3.0でnpm installします
  • プロジェクトフォルダの一階層上に下記のファイルを置いてdocker-compose up
  • エラー出ずに全部引っ張ってこれたら成功です
docker-compose.yml
version: '3'

services:
  node930:
    image: node:9.3.0
    ports:
     - "8080:8080"
    volumes:
      - ./[projectフォルダ]:/mnt/project
    working_dir: "/mnt/project"
    command: "npm install"
5
5
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
5