VSCode DevContainers with a corporate proxy

Bjego
4 min readDec 10, 2021

Visual Studio Codes DevContainers are great to share a development environment in your team. Unfortunally they won’t work out of the box behind a corporate proxy. In my case our corporate proxy requires a personal login and also injects an inhouse ssl certificate.

How to start?

Well first of all install Docker Desktop with WSL2 on your Windows PC. https://docs.docker.com/desktop/windows/install/

The problem with the certificates

For the next steps you need the ca.pem certificate from your administrators. Usually they install it on all pcs in the corporate network. So just ask them and store your pem file on a known location on your pc like C:\ca\ca.pem. We need this later.

How to configure Docker Desktop?

Open the settings of Docker Desktop (right click the toolbar icon). Go to Ressources > Proxies and enter both http and https proxy in the format: http://USER:PASSWORD@myproxy.com:8888

Docker Proxy settings

How to configure my WSL2 backend?

In my case I am using Ubuntu, if you are using another distro. The installation might differ a bit. You should crawl the web for “install ca cert in DISTRIBUTION”.

Copy the certificate into your distro.

sudo cp /mnt/c/ca/ca.pem /usr/local/share/ca-certificates/ca.crt

this copies ca.pem from your windows to /usr/local/share/ca-certificates/ca.crt

Then update the certificate store

sudo update-ca-certificates

Create a new .bat file as launcher

Right click on your desktop and create a new text file and store it as devcontainer.bat

Edit the contents of that file to be:

SET HTTP_PROXY=http://User:Password@proxy:Port

SET HTTPS_PROXY=http://User:Password@proxy:Port

SET http_proxy=http://User:Password@proxy:Port

SET https_proxy=http://User:Password@proxy:Port

start code

This sets the HTTP_PROXY and HTTPS_PROXY variables only for the launched VSCODE. So this doesn’t pollute your environment variables for other applications.

Why are you setting HTTP_PROXY just in a launcher and not as an environment variable?

Well in my environment, I’m developing mostly dotnet core applications on windows 10, setting these environment variables to “http://USER:PASSWORD@Proxy:Port” caused several issues while debugging code on windows. Therefore I chose to set HTTP_PROXY only for VSCode when it should run a devcontainer.

If you don’t have an issue with setting your HTTPS_PROXY, HTTP_PROXY, https_proxy and http_proxy environment variables on windows. You don’t need the launcher at all then.

And now the devcontainer configuration files

File structure

In your project create a folder .devcontainer

Copy your ca.pem to a subfolder “certs” in .devcontainer

Create a Dockerfile which describes your container and contains the certifiate information

File contents

Dockerfile

Usually I use predefined containers from Microsoft: https://github.com/Microsoft/vscode-dev-containers and inject my certificate to the image.

Create the devcontainer.json to configure the environment

devcontainer.json

“dockerFile”: “Dockerfile”, — This tells the devcontainer to use our Dockerfile

“build.args” : “HTTPS_PROXY”: “${localEnv:HTTP_PROXY}”,

“build.args” :“HTTP_PROXY”: “${localEnv:HTTP_PROXY}”

These build args inject the proxy into the build process of the container

extensions — pre install some vscode extensions

“containerEnv.HTTPS_PROXY”:“${localEnv:HTTP_PROXY}”,

“containerEnv.HTTP_PROXY”: “${localEnv:HTTP_PROXY}”,

These settings set the proxy environment variables in your running container instance.

“remoteEnv.HTTPS_PROXY”: “${localEnv:HTTP_PROXY}”,

“remoteEnv.HTTP_PROXY”: “${localEnv:HTTP_PROXY}”,

These settings are telling your hosting VSCode to use these environment variables outside of the container.

Way more configuration is possible check the docs: https://code.visualstudio.com/docs/remote/devcontainerjson-reference

Next time you open your project VS Code asks you, if you want to run the devcontainer.

Thanke you & have fun!

Update from the 14th December 2021 — I replaced the ProxyUsername and ProxyPassword environment variables with the well known HTTP_PROXY and HTTPS_PROXY environment variables, as the download from VS Codes Remote Server in the devcontainer stopped working for me.

Update from the 15th December 2021 — I added NO_PROXY environment variable, as I started to get issues with my local installed kubectl on windows 10.

Update from the 15th December 2021 — Moved away from setting global HTTP_PROXY and HTTPS_PROXY env vars for my hosting machine and added remoteEnv values.

Update from the 17th December 2021 — Moved to native WSL2 commands in VSCode as this leads to a clean windows environment and a typical configured linux environment with the well known HTTP(S)_PROXY environment variables.

Update from the 17th December 2021 — Well due to caching and other unpredictable things — the solution with a launcher seems to be the most stable solution for me.

Update from the 29th December 2021 — Proxy settings in VSCode aren’t needed as they are pulled from the environment variables in the batch file

--

--