[PYTHON] Introducing Docker Desktop for Windows on WSL2

1.First of all

Summary of this article

Run Dokcer on WSL2 (Windows Subsystem for Linux).

The environment that the article assumes

--Windows10 Pro 64bit 1909 (OS build 18363,1198)

2. Introduction main part

Introducing Docker Desktop

(1) Introduce WSL2.

Please install WSL2 referring to the following.

-Introduction of WSL2 --Linux on Windows (Windows Subsystem for Linux 2)

(2) Download Docker Desktop

--Download DockerDesktop for Windows.

(3) Run the installer

(4) Set Proxy as needed

If you use it in an environment that requires a proxy, set up a proxy as needed.

-[Start] —-> [Docker Desktop] --Setting button at the top of the screen [⚙] —-> [Resources] --Turn on [Manual proxy configuration] --Enter the proxy server information.

(5) Start the getting-started container as a trial

C:\> docker run -d -p 80:80 --name docker_getting_started docker/getting-started

↑ The container name is specified by --name for easy management. If not specified, the name will be set automatically.

Unable to find image 'docker/getting-started:latest' locally
latest: Pulling from docker/getting-started
188c0c94c7c5: Pull complete
617561f33ec6: Pull complete
7d856acdaa9c: Pull complete
a0d3c6e28e6d: Pull complete
af69a9b963c8: Pull complete
0739f3815ad8: Pull complete
7c7b75d0baf8: Pull complete
Digest: sha256:b821569034e3b5fae03b40e64a866017067f3bf17effe185b782bdbf02179528
Status: Downloaded newer image for docker/getting-started:latest
968e5ed0c3110cfc0d3a1db69d9a4d3cba232fcb28ebe3f9ae76b1ca80699b14
  • Access the started container.

http://localhost:80/ 2020-12-13-007-s.png

(6) Check the docker status.

First, check the list of docker images.
C:\> docker images
REPOSITORY               TAG       IMAGE ID       CREATED      SIZE
docker/getting-started   latest    021a1b85e641   3 days ago   27.6MB
Check the list of started containers next.
C:\> docker ps -a
CONTAINER ID   IMAGE                    COMMAND                  CREATED              STATUS PORTS NAMES
968e5ed0c311   docker/getting-started   "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   docker_getting_started

(7) Check the status of WSL

C:\> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         2
  docker-desktop-data    Running         2
  docker-desktop         Running         2

A distribution for Docker has been introduced.

(8) Take a peek inside Ubuntu.

I'll go inside.

C:\> wsl -d Ubuntu-20.04

Let's operate it in Linux.

$ uname -r
4.19.128-microsoft-standard
$ cat /etc/issue
Ubuntu 20.04.1 LTS \n \l
$ docker ps -a
CONTAINER ID   IMAGE                    COMMAND                  CREATED STATUS PORTS NAMES
968e5ed0c311   docker/getting-started   "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes   0.0.0.0:80->80/tcp   docker_getting_started

The result of the docker command on Ubuntu is the same as the result of the docker command on Windows. You can see that there is a reality on Ubuntu.

Have you been able to introduce it?

3. Try using it as a trial

Try to introduce a Python image

Let's use a Python container. I will try the following two.

  1. Try running hello.py created on Winodws on the container.
  2. Try running the python command on the container.

(1) Download the image

C:\> docker pull python
Using default tag: latest
latest: Pulling from library/python
6c33745f49b4: Pull complete
c87cd3c61e27: Pull complete
05a3c799ec37: Pull complete
a61c38f966ac: Pull complete
c2dd6d195b68: Pull complete
29b9446ae7bd: Pull complete
09cf96c794f9: Pull complete
f674fd97fba7: Pull complete
ffc24df6b7b8: Pull complete
Digest: sha256:b273b08cf9fe6b07ee4c2466095e5a4ac5301ade62106b7e7817eba00a684613
Status: Downloaded newer image for python:latest
docker.io/library/python:latest

(2) Check the image

C:\> docker images
C:\> docker images
REPOSITORY               TAG       IMAGE ID       CREATED        SIZE
python                   latest    0611cf846c85   16 hours ago   885MB
docker/getting-started   latest    021a1b85e641   3 days ago     27.6MB

I was able to confirm that the python image was downloaded.

(3) Prepare Python hello.py.

Prepare a simple program that outputs hello. C:\work\docker\playgraound\python\hello.py

print('hello')
ans = 5**2
print(f'5 squared{ans}')

(4) Start the container

docker run -itd -v Win folder: folder on the container --name container name image name
C:\> docker run -itd -v C:\work\docker\playground\python:/mnt/python --name playground_python python
80de49d79dfba3d65cb944f7d8348ab2d9f090a8438d67afb81e2e91413a5f16

↑ The docker id is displayed.

(5) Check the startup status

C:\> docker ps -a
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS         NAMES
80de49d79dfb   python                   "python3"                19 seconds ago   Up 16 seconds                        playground_python
968e5ed0c311   docker/getting-started   "/docker-entrypoint.…"   3 hours ago      Up 3 hours      0.0.0.0:80->80/tcp   docker_getting_started

A new container environment was created based on the downloaded python image.

(6) Enter the container

Bash into the running container.
C:\> docker exec -it playground_python /bin/bash

(7) Check the Python version

# python --version
Python 3.9.1

(8) Check if the folder on Windows is mounted.

# ls /mnt/python
hello.py

(9) Execute the py file on Windows

# python /mnt/python/hello.py
hello
5 squared is 25

You can operate files on Windows.

(10) Try using python on the command line.

# python -c "ans=10*10; print(ans)"
100

(11) Exit the container

# exit

(12) Check the status of the container

C:\> docker ps -a
When I run it, it is backgrounded with the -d option, so it keeps running.
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                NAMES
80de49d79dfb   python                   "python3"                12 minutes ago   Up 12 minutes                        playground_python
968e5ed0c311   docker/getting-started   "/docker-entrypoint.…"   3 hours ago      Up 3 hours      0.0.0.0:80->80/tcp   docker_getting_started

4. Summary

I found that file sharing can be easily done with docker and windows. I think development productivity will increase. However, please note that a warning is written when the performance drops.

References

--Docker document Japanese localization project

  • https://docs.docker.jp/ --Docker Desktop WSL 2 backend
  • https://docs.docker.jp/docker-for-windows/wsl.html

Recommended books

--Latest Linux container technology that supports Docker --A good book that explains how Docker works through Linux functions --Nikkei BP (2015/3/25) --Kindle version for only 990 yen

Recommended Posts