How to create Docker containers using WSL 2 on Windows
START
Introduction
Docker comes in handy when developing solutions that will then be deployed on a server. But it can be kind of tricky to set everything in place.
In this tutorial we will, after dealing with the prerequisites: - Setup WSL 2 to use Docker WSL 2 integration - Install Docker on Windows - Run our very first docker container - Customize our container with Dockerfile and docker-compose
Basic information
Difficulty
Duration
~30 min
Main Steps
Index
Prerequirements
Setup WSL 2
Run your first container
Install Docker
Prerequirements
Knowledge
Hardware
- Windows 11 64-bit: Home or Pro version 21H2 or higher, or Enterprise or Education version 21H2 or higher. or- Windows 10 64-bit: Home or Pro 2004 (build 19041) or higher, or Enterprise or Education 1909 (build 18363) or higher. - 64-bit processor with Second Level Address Translation (SLAT)- 4GB system RAM- Virtualization support must be enabled in BIOS
- Some command prompt knowledge
Prerequirements
To see if virtualization is enabled, go to :
Task Manager > Performance tab
Prerequirements
If virtualization is not enabled:
- Open an administrative console prompt.
- Run the command: bcdedit /set hypervisorlaunchtype auto
- Reboot your computer
Setup WSL 2
- Open an administrator PowerShell or command prompt
- Run the command: wsl --install
This command will: - Enable the required optional components - Download the latest Linux kernel - Set WSL 2 as your default - Install a Linux distribution for you (Ubuntu by default)
The above command only works if WSL is not installed at all, if you run wsl --install and see the WSL help text, your ready to go. If you want to see a list of available distros, try running wsl --list --online. You can run wsl --install -d <DistroName> to install a distro.
- Reboot your computer
Setup WSL 2
If you did just install WSL 2, you will need to create a user account and password for your newly installed Linux distribution.
To do so, you can open your Linux distribution in your command prompt by typing the command wsl. It will automatically ask your to create a User Name and Password for your Linux distribution.
Install Docker
Once you are finished with the prerequirements: - Download the last stable version of and execute it. - Start Docker Desktop from the Windows Start menu.
Docker
If you are running a supported system, Docker Desktop prompts you to enable WSL 2 during installation. Read the information displayed on the screen and enable WSL 2 to continue.
Install Docker
- From the Docker menu, select Settings > General.
- Select the Use WSL 2 based engine check box. It should be enabled by default.
- Click Apply & Restart.
Install Docker
If the Use WSL 2 engine was not enabled, you can now choose which one of your distributions you want to use for docker integration, or let it on the default one
Run your first container
To create your first docker application you should create a folder for your project containing: - A "Dockerfile" file (Docker file that will contain the necessary instructions to create the environment). - A "main.py" file for example (python file that will contain the code to be executed). Then add some code to your main.py file
#!/usr/bin/env python3
print("Docker is working!")
That way, you will be able to tell if Docker is working properly.
Run your first container
You will then need to edit your Dockerfile. your container must contain all the dependencies necessary to launch Python. A linux (Ubuntu) with Python installed on it should be enough. Most of the time, you can find an image that corresponds to your needs on - You will have to type ‘Python’ in the search bar and take the official image created for Python.
DockerHub website.
Run your first container
Make your Dockerfile look like this, the lines starting with '#' are comments you can skip them in the actual file.
# Use the keyword 'FROM' to import the base image with latest for the version. FROM python:latest # In order to launch our python code, you must import it into your image. # Use the keyword 'COPY' to put main.py in the / folder of the container. COPY main.py /
# You need to define the command to launch when you are going to run the image. # Use the keyword 'CMD' and execute "python ./main.py". CMD [ "python", "./main.py" ]
Run your first container
Now that everything is set, you can finally create the image of your first container. - Open a command prompt.- Go to your project folder using cd command (cd path/of/your/project). - Run the command: docker build -t python-test. The '-t' option allows you to define the name of your image. In this case it will be ’python-test’ but you can put what you want. - Run the command: docker run python-test That’s it. You should normally see “Docker is ready!” displayed in your terminal.
Congrats !
From now on, you know how to install docker and create containers for your projects. You can contact me to give me your opinion, help me improve this tutorial or for any assistance.
stephane.humblot@etu.univ-grenoble-alpes.fr
Docker tutorial
Stephane Humblot
Created on January 31, 2022
Start designing with a free template
Discover more than 1500 professional designs like these:
View
Practical Microsite
View
Essential Microsite
View
Akihabara Microsite
View
Essential CV
View
Akihabara Resume
View
Corporate CV
View
Interactive Onboarding Guide
Explore all templates
Transcript
How to create Docker containers using WSL 2 on Windows
START
Introduction
Docker comes in handy when developing solutions that will then be deployed on a server. But it can be kind of tricky to set everything in place.
In this tutorial we will, after dealing with the prerequisites: - Setup WSL 2 to use Docker WSL 2 integration - Install Docker on Windows - Run our very first docker container - Customize our container with Dockerfile and docker-compose
Basic information
Difficulty
Duration
~30 min
Main Steps
Index
Prerequirements
Setup WSL 2
Run your first container
Install Docker
Prerequirements
Knowledge
Hardware
- Windows 11 64-bit: Home or Pro version 21H2 or higher, or Enterprise or Education version 21H2 or higher. or- Windows 10 64-bit: Home or Pro 2004 (build 19041) or higher, or Enterprise or Education 1909 (build 18363) or higher. - 64-bit processor with Second Level Address Translation (SLAT)- 4GB system RAM- Virtualization support must be enabled in BIOS
- Some command prompt knowledge
Prerequirements
To see if virtualization is enabled, go to :
Task Manager > Performance tab
Prerequirements
If virtualization is not enabled:
- Open an administrative console prompt.
- Run the command: bcdedit /set hypervisorlaunchtype auto
- Reboot your computer
Setup WSL 2
- Open an administrator PowerShell or command prompt
- Run the command: wsl --install
This command will: - Enable the required optional components - Download the latest Linux kernel - Set WSL 2 as your default - Install a Linux distribution for you (Ubuntu by default)
The above command only works if WSL is not installed at all, if you run wsl --install and see the WSL help text, your ready to go. If you want to see a list of available distros, try running wsl --list --online. You can run wsl --install -d <DistroName> to install a distro.
- Reboot your computer
Setup WSL 2
If you did just install WSL 2, you will need to create a user account and password for your newly installed Linux distribution.
To do so, you can open your Linux distribution in your command prompt by typing the command wsl. It will automatically ask your to create a User Name and Password for your Linux distribution.
Install Docker
Once you are finished with the prerequirements: - Download the last stable version of and execute it. - Start Docker Desktop from the Windows Start menu.
Docker
If you are running a supported system, Docker Desktop prompts you to enable WSL 2 during installation. Read the information displayed on the screen and enable WSL 2 to continue.
Install Docker
- From the Docker menu, select Settings > General.
- Select the Use WSL 2 based engine check box. It should be enabled by default.
- Click Apply & Restart.
Install Docker
If the Use WSL 2 engine was not enabled, you can now choose which one of your distributions you want to use for docker integration, or let it on the default one
Run your first container
To create your first docker application you should create a folder for your project containing: - A "Dockerfile" file (Docker file that will contain the necessary instructions to create the environment). - A "main.py" file for example (python file that will contain the code to be executed). Then add some code to your main.py file
#!/usr/bin/env python3 print("Docker is working!")
That way, you will be able to tell if Docker is working properly.
Run your first container
You will then need to edit your Dockerfile. your container must contain all the dependencies necessary to launch Python. A linux (Ubuntu) with Python installed on it should be enough. Most of the time, you can find an image that corresponds to your needs on - You will have to type ‘Python’ in the search bar and take the official image created for Python.
DockerHub website.
Run your first container
Make your Dockerfile look like this, the lines starting with '#' are comments you can skip them in the actual file.
# Use the keyword 'FROM' to import the base image with latest for the version. FROM python:latest # In order to launch our python code, you must import it into your image. # Use the keyword 'COPY' to put main.py in the / folder of the container. COPY main.py / # You need to define the command to launch when you are going to run the image. # Use the keyword 'CMD' and execute "python ./main.py". CMD [ "python", "./main.py" ]
Run your first container
Now that everything is set, you can finally create the image of your first container. - Open a command prompt.- Go to your project folder using cd command (cd path/of/your/project). - Run the command: docker build -t python-test. The '-t' option allows you to define the name of your image. In this case it will be ’python-test’ but you can put what you want. - Run the command: docker run python-test That’s it. You should normally see “Docker is ready!” displayed in your terminal.
Congrats !
From now on, you know how to install docker and create containers for your projects. You can contact me to give me your opinion, help me improve this tutorial or for any assistance.
stephane.humblot@etu.univ-grenoble-alpes.fr