The docker installation method of Windows + WSL2, as well as configuring the python environment, transferring files and executing .py scripts

1 written in front

1.1 What is docker?

As we all know, the most troublesome thing about code reproduction is the running environment problem. The environment is clearly configured according to the README, but due to differences in library versions, systems, and hardware levels, some inexplicable errors occur.

The emergence of the docker platform has changed this situation. It creates a container (container), where technicians install the development environment, complete code development and package it, and users can reproduce this self-contained development environment after downloading the image (image). container, so that the program can be used smoothly, that is, the slogan "build, share and run".

1.2 What is WSL2?

WSL2 (Windows Subsystem for Linux 2) is Microsoft's own Linux subsystem, which was first released in Windows 10 in 2021, and can be installed and run on both Windows 10 and Windows 11.

Docker must run under the Linux system, so the Windows system needs to call WSL2 to implement docker.

2 installation

2.1 Check whether the Windows subsystem function is enabled

  • Search for "Turn Windows features on or off" in the lower left corner
  • Find "Windows Subsystem for Linux"
  • Make sure the box in front is ticked

2.2 Update WSL2

wsl --set-default-version 2

For more installation questions about WSL2, please see:

https://learn.microsoft.com/zh-cn/windows/wsl/install-manual#step-4—download-the-linux-kernel-update-package

2.3 Install Docker

For more installation questions about WSL2, please see:

https://docs.docker.com/desktop/install/windows-install/

3 Introduction to Docker Platform Functions

3.1 Containers, images and data volumes

Open Docker Desktop, you can see that there are three main functions on the left:

insert image description here

  • Containers: running containers
  • Images: Environment files, you can find the environment you need through Search in the upper right corner
  • Data volumes (Volumes): the hard disk mounted to the container, which is convenient for exchanging data with the Windows system

3.2 Controlling Docker from Windows Powershell

Docker Desktop allows you to easily view and delete recent activities, but the operation is not clear and clear, so it is more recommended to execute commands on the command line. The most common commands are listed below:

  • Mirror: docker pull xxx (download) / docker run xxx (install) / docker image ls (list)

  • Container: docker start xxx (start) / docker stop xxx (stop) / docker container ls (list)

  • Data volume: docker volume create xxx (create) / docker volume ls (list)

3.3 Correct posture

  1. Download the required image (environment, such as python 3)
  2. Prepare a volume (data disk)
  3. Put the code and data to be executed in the volume
  4. Create a container (container), install the environment, and mount the volume
  5. Run code in a container-reproducible environment

4 Reproduce a container with its own python environment and run the code

4.1 Prepare the python image file

Go back to Docker Desktop, search for python in the upper right corner, select the desired version, and click Pull

After completion, you will see one more python in images

insert image description here

4.2 Create a data disk

Open Windows Powershell and enter the following command

After completion, you can see the newly added data disk in Volumes of Docker Desktop

docker volume create --name disk

4.3 Transfer code file

The actual location of the data disk can be accessed via Windows file explorer:

\wsl.localhost\docker-desktop-data\data\docker\volumes\disk_data

At this point, you can create or copy a test.py in this location, the content is as follows:

print('Hello World!')

Answer: What's the matter with this strange path?

Please recall the principle of the docker platform, which runs in the Linux environment created by Windows using WSL2. In other words, the above path can be understood as:

  1. WSL2 creates the Linux system at the network location \wsl.localhost
  2. The docker platform is installed in this system, so in the docker-desktop-data folder
  3. docker created a harddisk, so it's inside \data\docker\volumes

4.4 Create container, install image file and mount data disk

Open Windows Powershell and enter the following command

docker run -it --name test -v disk:/mnt python

At this point, you can see that you have entered the python environment of the container, and the system of the container is the Linux system

Python 3.11.0 (main, Nov 15 2022, 19:58:01) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Answer: What are the parameters in run?

  • -it: Create the container in an interactive form, otherwise Powershell cannot enter the Linux subsystem
  • –name test: The name of the container starts with test
  • -v disk:/mnt: Mount the created disk hard disk to the /mnt folder of the container
  • python: install and enter the python environment

4.5 Execute the code in the data disk

At this point, we have prepared the container test brought by Docker for the Linux subsystem. The python environment is installed in the container, and the /mnt folder of the container is mounted with the disk folder of the system, which contains the transmitted code, waiting for further execution.

We first enter this container:

insert image description here
Then, like Linux, switch the working directory of the container to /mnt, and then execute the code:

cd mnt
python test.py

You're done:

insert image description here

Guess you like

Origin blog.csdn.net/zfqy2222/article/details/128099845