Install docker under WSL

Install docker under WSL

This article mainly describes how to install docker under the wsl subsystem (Ubuntu 20.04)

**Precondition: The computer has installed wsl subsystem, the installation tutorial refers to the portal**

Install docker

Root user ssh connects to the Linux subsystem, copy and execute the following commands

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

Wait for command execution and installation to complete

#查看docker版本号
docker --version

Switch Alibaba Cloud image source

Alibaba Cloud Mirror Source obtains the address . After logging in, select Mirror Accelerator in the left menu to see your exclusive address.

Then execute the following command

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://zhjxhme4.mirror.aliyuncs.com"]
}
EOF

Start docker

Execute the following command

service docker start
docker run hello-world

Executing docker run hello-world will report an error cgroups: cannot find cgroup mount destination: unknown.

Execute the following command to solve

sudo mkdir /sys/fs/cgroup/systemd
sudo mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd
service docker restart
docker run hello-world

If you see the following output, it means the docker container hello-world ran successfully

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

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

docker install mysql

docker pull mysql
docker run --restart=always -p 33006:3306 --name mysql \
    -v /usr/data/mysql/log:/var/log/mysql \
    -v /usr/data/mysql/data:/var/lib/mysql \
    -v /usr/data/mysql/conf:/etc/mysql/conf.d \
    -e MYSQL_ROOT_PASSWORD=123 \
    -d mysql:latest \
    --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

Explain the above command

docker pull mysql download the latest image of mysql

docker run xxx run docker container

--Restart=always automatic restart (start WSL automatically when booting, wsl automatically start docker service, docker automatically start mysql container)

-p 33006:3306 Since port 3306 of this machine is occupied by the local database, use port 33006 of this machine to map port 3306 of docker

--Name mysql specifies the name of the container

-v /usr/data/mysql/log:/var/log/mysql specifies the mount directory, the left side is the wsl system directory, and the right side is the docker container mysql directory

-e MYSQL_ROOT_PASSWORD=123 specify root password

-d mysql:latest specifies to run the mysql:latest image in the background

--Character-set-server=utf8mb4 specifies the mysql character set

--Collation-server=utf8mb4_unicode_ci specifies the character set at the MySQL server level

Connect to mysql

Connection tool :

Recommend dbeaver, it's free, and the interface is pretty good, download the portal

Configure the connection properties, download the driver, and then specify allowPublicKeyRetrieval=TRUE in the driver properties to connect


springboot program connection

Need to pay attention to the following points

  • driver-class-name=com.mysql.cj.jdbc.Driver
  • The ip address cannot be written in the url, it needs to be replaced with the domain name, which is localhost
  • Need to specify useSSL=false

Examples of url addresses are as follows:

jdbc:mysql://localhost:33006/sample?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&useInformationSchema=true

Guess you like

Origin blog.csdn.net/l229568441/article/details/106964616