1
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Proxy環境下でWSL2+Docker環境をサクッと作る

Last updated at Posted at 2022-01-21

はじめに

DockerDesktopを購入しようと思ったが社内手続きが面倒・・・。
どうせ手間をかけるのならば、無料で使える代替環境の構築にパワーを使ってしまおうと思い
四苦八苦しながら必要な設定作業を1本のシェルスクリプトにまとめました。

※流用する方は自己責任でお願いします。

私の主な用途は、VSCodeのRemote Developmentを使いdockerコンテナを開発環境にすること。
後日、開発環境の構築/接続についての記事も書くかもしれないが
「VSCode devcontainer」等でググればたくさん情報があるので書かないかもしれない。

前提

WSL2:Ubuntu20.04LTS
⇒インストール後、ユーザー/パスワードを設定しただけの状態

使い方

起動したUbuntuに後述するスクリプトファイルを置いて実行する。
やり直しが必要な場合はUbuntuのリセットを推奨。
⇒追記で設定ファイルに書込みする処理があるので単純リランは無理です。

スクリプト

wsl-setup.sh
#!/bin/bash

cd ~

export http_proxy=http://proxy:port #ここだけ書き換えて使う
export https_proxy=${http_proxy}
export HTTP_PROXY=${http_proxy}
export HTTPS_PROXY=${http_proxy}

# git proxy setting
git config --global http.proxy ${http_proxy}
git config --global https.proxy ${https_proxy}
git config --global url."https://".insteadOf git://

# setting http proxy
cat <<EOF >> ~/.bashrc
# http proxy setting
export http_proxy=${http_proxy}
export https_proxy=${http_proxy}
export HTTP_PROXY=${http_proxy}
export HTTPS_PROXY=${http_proxy}
EOF

# setting apt proxy
sudo tee /etc/apt/apt.conf <<EOF > /dev/null
Acquire::http::proxy "${http_proxy}";
Acquire::https::proxy "${http_proxy}";
EOF

sudo apt -y update
sudo apt -y install \
  apt-transport-https \
  ca-certificates \
  curl \
  gnupg \
  lsb-release \

# gpg key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# docker install
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt -y update
sudo apt -y install docker-ce docker-ce-cli containerd.io

# docker-compose install
curl -L "https://github.com/docker/compose/releases/download/2.2.3/docker-compose-$(uname -s)-$(uname -m)" -o ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

sudo usermod -aG docker $USER

# setting docker proxy
mkdir ~/.docker
cat <<EOF > ~/.docker/config.json
{
  "proxies": {
    "default": {
      "httpProxy": "${http_proxy}",
      "httpsProxy": "${http_proxy}"
    }
  }
}
EOF

sudo tee /etc/default/docker <<EOF > /dev/null
export http_proxy="${http_proxy}"
export https_proxy="${http_proxy}"
export HTTP_PROXY="${http_proxy}"
export HTTPS_PROXY="${http_proxy}"
EOF

# docker auto start
sudo tee -a /etc/sudoers <<EOF > /dev/null
# docker start
${USER} ALL=(ALL)  NOPASSWD: /usr/sbin/service docker start
EOF

cat <<EOF >> ~/.bashrc
# docker start
if [ $(service docker status | awk '{print $4}') = "not" ]; then
  sudo service docker start > /dev/null
fi
EOF

echo "please reboot"
1
6
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
1
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?