iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐳

Installing Docker on WSL2 and Using Dev Containers Behind a Proxy

に公開
2024/07/13

In an environment where a proxy is required to access the internet, I attempted to install Docker on WSL2 and use it as a DevContainer. However, I encountered many pitfalls, so I am documenting them in this article.

Note that with Docker Desktop, it seems to handle Windows proxy settings automatically, so I could use it without any particular issues.
This memo is intended for those who cannot use Docker Desktop for some reason and are also unsure about proxy settings in Rancher Desktop.

Installing Docker on WSL2

I tested this on Ubuntu, which is installed by default on WSL2.

To install Docker, simply follow the official Ubuntu instructions.

Perform the steps described in "Install using the convenience script."

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Without any configuration, the apt-get update part of sudo sh get-docker.sh will fail with an error.

+ sh -c apt-get update -qq >/dev/null
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease  Cannot initiate the connection to archive.ubuntu.com:80 (2620:2d:4002:1::103). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2620:2d:4000:1::103). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2620:2d:4000:1::101). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2620:2d:4002:1::101). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2620:2d:4000:1::102). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2620:2d:4002:1::102). - connect (101: Network is unreachable) Could not connect to archive.ubuntu.com:80 (185.125.190.81), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.91.82), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.91.83), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.82), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.91.81), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.83), connection timed out
...

Since this is for connecting to the internet via apt-get, create and configure /etc/apt/apt.conf so that apt-get goes through the proxy.
As I can avoid this by setting http_proxy and https_proxy in /etc/environment (mentioned later), there was no need to set it in /etc/apt/apt.conf.

Acquire::http::Proxy "http://proxy.example.com:3128";
Acquire::https::Proxy "http://proxy.example.com:3128";

The apt-get part will now work fine, but a subsequent curl command will error out again.

+ sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" -o /etc/apt/keyrings/docker.asc
curl: (28) Failed to connect to download.docker.com port 443 after 278686 ms: Connection timed out

The account created when installing Ubuntu inherits proxy settings from Windows (set in environment variables), but these are not set for other users.
Therefore, when running with sudo, the proxy settings are missing, resulting in this error.

To avoid this, add proxy-related environment variables to /etc/environment so they are set for all users.

/etc/environment
HTTP_PROXY="http://proxy.example.com:3128"
HTTPS_PROXY="http://proxy.example.com:3128"
NO_PROXY="localhost,127.0.0.1,::1"
http_proxy="http://proxy.example.com:3128"
https_proxy="http://proxy.example.com:3128"
no_proxy="localhost,127.0.0.1,::1"

Regarding environment variables, since they are case-sensitive and different tools/libraries refer to either the uppercase or lowercase version, we set both.

Running Containers with Docker

Running containers with Docker also requires proxy settings when communicating externally, such as when pulling images.

If the proxy is not configured, running sudo docker run hello-world or similar commands will result in an error like the one below:

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

Create /etc/systemd/system/docker.service.d/http-proxy.conf and configure the proxy that Docker will use.
Since the directory itself may not exist, create it with mkdir -p /etc/systemd/system/docker.service.d before creating the file.

/etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:3128"
Environment="HTTPS_PROXY=http://proxy.example.com:3128"
Environment="NO_PROXY=localhost,127.0.0.1,::1"
Environment="http_proxy=http://proxy.example.com:3128"
Environment="https_proxy=http://proxy.example.com:3128"
Environment="no_proxy=localhost,127.0.0.1,::1"

After configuration, restart the Docker service.

sudo systemctl daemon-reload
sudo systemctl restart docker

Adding Docker Execution Permissions to the User Running VSCode

If you start a VSCode DevContainer with a user who does not have the necessary permissions, an error dialog stating "Current user does not have permission to run 'docker'." will appear.

Add the user to the docker group as follows:

sudo usermod -aG docker <username>

Proxy Settings for Dockerfile Builds

Proxy settings are also required when installing anything within a Dockerfile.

In docker-compose.yml, set the values of environment variables as args for the build process, then reference them and set them as environment variables within the Dockerfile.

docker-compose.yml
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        HTTP_PROXY: ${HTTP_PROXY}
        HTTPS_PROXY: ${HTTPS_PROXY}
        NO_PROXY: ${NO_PROXY}
Dockerfile
ARG HTTP_PROXY
ENV HTTP_PROXY=${HTTP_PROXY}
ENV http_proxy=${HTTP_PROXY}

ARG HTTPS_PROXY
ENV HTTPS_PROXY=${HTTPS_PROXY}
ENV https_proxy=${HTTPS_PROXY}

ARG NO_PROXY
ENV NO_PROXY=${NO_PROXY}
ENV no_proxy=${NO_PROXY}

Proxy Settings within the DevContainer

It seems that proxy settings are not automatically inherited by the running container.

Therefore, issues arise when executing processes that connect to the internet (such as npm install) on a container started with DevContainer.

Configure .devcontainer\devcontainer.json to inherit the proxy settings.

.devcontainer\devcontainer.json
  "containerEnv": {
    "HTTP_PROXY": "${localEnv:HTTP_PROXY}",
    "HTTPS_PROXY": "${localEnv:HTTPS_PROXY}",
    "NO_PROXY": "${localEnv:NO_PROXY}",
    "http_proxy": "${localEnv:HTTP_PROXY}",
    "https_proxy": "${localEnv:HTTPS_PROXY}",
    "no_proxy": "${localEnv:NO_PROXY}"
  },

With these settings, everything should now be operational.

Discussion