WSL2 container development with Moby

ยท 532 words ยท 3 minute read
view gist on GitHub

WSL2 container development with Moby ๐Ÿ”—

Building, pulling, pushing, and running containers is something many developers do often without even thinking. Most of my development over the past couple of years has been exclusively in a Linux environment, specifically WSL2.

Even prior to the recent licensing changes to Docker Desktop, I found myself increasingly as an engineer whose workflow didn’t line up with my tools. I never used the GUI features. I never built Windows containers. I used kind or k3d instead of the Docker Kubernetes functionality. I never mounted the Windows filesystem into my containers. And I certainly didn’t enjoy frequent downtime caused by updates for those features that I wasn’t using. I wanted the container experience in my dev environment to match what I got on a server - just the runtime & tools.

That said, I still like shiny new (or not-so-new but I never see anyone use them even though they rock!) bits like build secrets and heredocs in Dockerfiles. The good news is that all of that BuildKit goodness is present in Moby!

And it’s not particularly shouted from the rooftops, but Microsoft publishes and maintains a Moby distribution that’s used in both GitHub Codespaces and Azure IoT Edge. Being included in 2 shipping products gives me more than enough confidence for me to use it in my dev environment and saves me from having to build it from source. Perfect!

Usage ๐Ÿ”—

# Configure the repository
curl -sSL https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft-prod.list
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc
sudo apt-get update

# Install moby
sudo apt-get install moby-cli moby-buildx moby-compose moby-engine

# Let your account run docker w/o root
sudo usermod -aG docker $USER

# Add the System V init script
sudo curl -L https://raw.githubusercontent.com/moby/moby/master/contrib/init/sysvinit-debian/docker -o /etc/init.d/docker
sudo chmod +x /etc/init.d/docker

# Start the service
sudo service docker start

# Yay, containers!
docker run --rm -it hello-world

I assume the moby-engine, moby-cli, etc. packages are also present for Debian, RHEL, and so on, but will leave that as an exercise for the reader

Notes ๐Ÿ”—

  • The VS Code Remote Containers extension will pick it up automatically. Dev containers work great!
  • You’ll need to run service docker start in your terminal to start the daemon (or otherwise include it in your .bashrc)
    • Expect dockerd to start in about 1 second vs the ~2 minutes or so that Docker Desktop usually takes
  • You’ll have an instance of Docker per-WSL distribution, for better or worse. I like it, but just know that they’re distinct
  • This is for Linux only - sorry Windows container users!
  • Your version string will be a little funky
~$ docker version
Client:
 Cloud integration: 1.0.17
 Version:           20.10.8+azure
 API version:       1.41
 Go version:        go1.16.7
 Git commit:        3967b7d28e15a020e4ee344283128ead633b3e0c
 Built:             Thu Jul 29 13:55:47 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server:
 Engine:
  Version:          20.10.8+azure
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.7
  Git commit:       75249d88bc107a122b503f6a50e89c994331867c
  Built:            Fri Jul 30 01:30:57 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9+azure
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        4144b63817ebcc5b358fc2c8ef95f7cddd709aa7
 docker-init:
  Version:          0.19.0
  GitCommit: