Running your first Docker container in WSL 2

Madhuresh Gupta
Level Up Coding
Published in
4 min readJul 12, 2020

--

This is part 2 of 2 where I will guide you through on how to create and build your first Docker container inside WSL 2. If you have not yet installed Docker and WSL 2 go through my previous blog here.

Earlier we learnt how to install WSL 2 and docker but it’s important to have the basics of how to build an application in docker

Now look there are 2 ways to create a Docker container:

  1. We create our project and then “Dockerize” the application by adding a Dockerfile.
  2. In the second way, we first take a standard Docker container and then proceed with creating the application inside the container itself.

For today’s tutorial, I will run you through the steps to create an application directly inside a container. So now let’s say I want to create a Python application, I will need a container which already has Python installed.

If you face any problem in understanding the below steps, you can go to the repository for this blog here.

We are going to run a simple application today just so that you get familiar with how Docker works and then you can proceed with any big application.

The purpose of this tutorial is to create a Python program that displays a sentence in the console. This program will have to be launched through a Dockerfile.

You will see, it’s not very complicated once you understand the process.

First we have to create a project folder, which contains 2 files primarily:

  1. main.py
  2. Dockerfile
Screenshot of two files created

Note: The “Dockerfile” is without any extension and it’s important that the name of the file is exactly same as mentioned for Docker engine to pick up the dependencies for your image to be built.

I have a written the most simple statement in main.py

print("Hello world from Docker!")

This we will be printing on the console but not using the python installed in the operating system but using the python installed inside a Docker container.

Our goal here is to launch Python code.

To do this, our Docker must contain all the dependencies necessary to launch Python.

The first step to take when you create a Docker file is to access the DockerHub website. This site contains many pre-designed images to save your time (for example: all images for linux or code languages).

Now we need to set up the Docker container for which the instructions are written in the Dockerfile.

So we take the latest python docker image from the Docker hub (the online marketplace where all official docker images are hosted). To pull the latest image from Docker hub, we need to declare the following in the Dockerfile.

FROM python:latest
COPY main.py /
CMD [ "python", "./main.py" ]

So let me explain the above 3 lines of code written in the Dockerfile-

Here we start the Dockerfile using a base image. The keyword is “FROM” and then followed by image name which you want to pull from Docker Hub. In our example, we want import the python image. So we write ‘python’ for the image name and ‘latest’ is the tag of the version.

Now we need to copy our python file into the image we just pulled. We use the keyword ‘COPY’ to do that, followed by the name of file we want to copy. The second parameter ‘/’ is the path where to put the file on the image. This ‘/’ indicates that we will copy our file to the root directory of the image (Docker container).

Lastly, we define the command to launch when we are going to run the image. The keyword which we use is ‘CMD’ to do that. The command will execute “python ./main.py” from the image.

In summary we define the steps to be followed by the Docker container.

Final Steps:

Once your code is ready and the Dockerfile is written, all you have to do is create your image to contain your application.

docker build -t python-test .

The ’-t’ option allows you to define the name of your image. In our case we have chosen “python-test” but you can put what you want.

Now the image is created, and the code is ready to be launched. We will use the docker run command.

 docker run python-test

You need to put the name of your image after ‘docker run’ command.

There you go, that’s it! You should normally see “Hello world from Docker!” displayed in your terminal. 😁

With this we come to the end of the 2 part series tutorial for you to get started with Docker containers inside WSL 2. Hope you might have now a basic idea on how we work with Docker containers.

--

--

Follows Microsoft, Tesla Motors, SpaceX, Google, Elon Musk. Aviation Admirer!