Setting Up Docker on WSL 2

Tutorial 4 of 5

1. Introduction

In this tutorial, our goal is to set up Docker on the Windows Subsystem for Linux (WSL) 2. Docker is a platform that allows you to develop, test, and deliver applications quickly. The WSL 2 is a compatibility layer for running Linux binary executables natively on Windows 10 and Windows Server 2019.

By the end of this tutorial, you will learn how to install Docker on WSL 2, run Docker commands, and manage Docker containers and images.

The prerequisites for this tutorial are:
- Basic understanding of Docker and WSL.
- You should have WSL 2 installed on your Windows machine.

2. Step-by-Step Guide

Install Docker Desktop on Windows

  • Download Docker Desktop for Windows from Docker Hub. This will automatically include Docker engine, Docker CLI client, Docker Compose, Docker Machine, and Kitematic.

  • Once you've downloaded Docker Desktop, double-click on the downloaded .exe file to start the installation. Accept the terms of the service, authorize the installer, and proceed with the install.

  • After installing Docker Desktop, you need to enable the WSL 2 based engine. Open Docker Desktop, go to Settings > General, check the Use the WSL 2 based engine option and apply the changes.

Set Docker to Use WSL 2

  • Open your WSL 2 terminal and check the installed distributions by running wsl -l -v.

  • Set the desired distribution to use WSL 2 if it's not already set. You can do this by running wsl --set-version <distribution> 2. Replace <distribution> with the name of your distribution.

3. Code Examples

Example 1: Run Docker Version Command

To check if Docker is working correctly, run the Docker version command in your WSL 2 terminal.

$ docker --version

You should get an output similar to this:

Docker version 20.10.5, build 55c4c88

Example 2: Run a Docker Container

Here's an example of running a simple Docker container. We will use the hello-world image.

$ docker run hello-world

This command will download the hello-world Docker image (if it isn't available locally), create a new container from that image, and run the container.

The output will be a message from the Hello World application:

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

4. Summary

In this tutorial, we learned how to set up Docker on WSL 2. We installed Docker Desktop on Windows, enabled the WSL 2 based engine, checked the Docker version, and ran a Docker container.

Next, you can learn about building your own Docker images, pushing images to Docker Hub, and managing Docker networks and volumes.

Some additional resources for learning include the official Docker documentation, Docker's getting started guide, and various online courses on platforms like Coursera and Udemy.

5. Practice Exercises

Exercise 1: Pull the nginx Docker image and run a container from it.

Solution:

$ docker pull nginx
$ docker run -d -p 8080:80 nginx

The docker pull command downloads the nginx image, and the docker run command starts a new container from the nginx image. The -d option starts the container in detached mode (in the background), and -p 8080:80 maps port 8080 on the host to port 80 on the container.

Exercise 2: List all Docker containers and stop a running container.

Solution:

$ docker ps
$ docker stop <container-id>

The docker ps command lists all running Docker containers, and docker stop stops a running container. You need to replace <container-id> with the ID of the container you want to stop.

Tips for further practice: Try running different Docker images, managing multiple containers, and using Docker Compose to manage multi-container applications.