WSL下安装docker
本文主要讲述如何在wsl子系统(Ubuntu 20.04)下安装docker
**前置条件:电脑已经安装有wsl子系统,安装教程参考传送门**
安装docker
root用户ssh连接到linux子系统,复制并执行如下命令
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
等待命令执行及安装完成
#查看docker版本号
docker --version
切换阿里云镜像源
阿里云镜像源获取地址,登陆后,左侧菜单选中镜像加速器可以看到你的专属地址。
然后执行如下命令
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://zhjxhme4.mirror.aliyuncs.com"]
}
EOF
启动docker
执行如下命令
service docker start
docker run hello-world
执行docker run hello-world会报错 cgroups: cannot find cgroup mount destination: unknown.
执行如下命令解决
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
如果看到如下输出,代表docker容器 hello-world运行成功
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安装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
解释一下上面命令
docker pull mysql 下载mysql最新的镜像
docker run xxx 运行docker容器
–restart=always 自动重启(开机自动启动WSL,wsl自动启动docker服务,docker自动启动mysql容器)
-p 33006:3306 由于本机3306端口被本地数据库占用,用本机的33006端口映射docker的3306端口
–name mysql 指定容器名称
-v /usr/data/mysql/log:/var/log/mysql 指定挂载目录,左侧是wsl系统目录,右侧是docker容器mysql目录
-e MYSQL_ROOT_PASSWORD=123 指定root密码
-d mysql:latest 指定后台运行mysql:latest镜像
–character-set-server=utf8mb4 指定mysql字符集
–collation-server=utf8mb4_unicode_ci 指定mysql服务器级别的字符集
连接mysql
连接工具:
推荐dbeaver,免费,界面也比较好看,下载传送门
配置连接属性,下载驱动,然后驱动属性指定 allowPublicKeyRetrieval=TRUE 即可连接
springboot程序连接
需注意以下几点
- driver-class-name=com.mysql.cj.jdbc.Driver
- url中不能写ip地址,需要替换为域名,也就是localhost
- 需指定useSSL=false
url地址示例如下:
jdbc:mysql://localhost:33006/sample?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&useInformationSchema=true