环境是:win10+wsl2+ubuntu18.04 下安装 Dcoker
如果已经安装了宝塔面板,直接安装就可以,挺好用的。
还有一种是:Win10 Build + WSL2 + Docker Desktop,别说了,都是泪,还不如宝塔一键安装来得舒服!!!
下面来看命令行安装方式:
安装过程
更换软件源为国内的,这里我选阿里
$
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
$
sudo sed -i
's/security.ubuntu/mirrors.aliyun/g' /etc/apt/sources.list
$
sudo sed -i
's/archive.ubuntu/mirrors.aliyun/g' /etc/apt/sources.list
$
sudo apt-get update
$
sudo apt-get upgrade
安装依赖包:
$
sudo apt
install apt-transport-https ca-certificates software-properties-common
curl
添加 GPG 密钥,并添加 Dcoker-ce 安装源
$
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg
| sudo apt-key add -
$
sudo add-apt-repository
"deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable"
$
sudo apt-get update
安装 Docker-ce
$
sudo apt-get install docker-ce
测试运行,成功
$
sudo docker run 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
...
...
添加当前用户到 docker 用户组,可以不用 sudo 来运行 docker 命令
$
sudo groupadd docker
$
sudo usermod -aG docker
$USER
$ docker run hello-world
下面使用 Dcoker 来装个 mysql 试试效果:
先添加一下镜像源,没有这个文件就创建
$
sudo vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://mirror.ccs.tencentyun.com",
"http://hub-mirror.c.163.com"]
}
重启 docker sudo service docker restart
下载安装 mysql
$ docker run --name mysql -e MYSQL_ROOT_PASSWORD
=root -d -p 33000:3306 --restart
=always mysql
Unable to
find image
'mysql:latest' locally
latest: Pulling from library/mysql
8559a31e96f4: Pull complete
d51ce1c2e575: Pull complete
........
........
-d 是后台运行,守护进程,同 --detach-name Assign a name to the container,自定义容器名称-p 指定端口,前面是映射端口,后面是容器运行的端口,同 --publish-e 设置环境变量,同 --env--restart 设置容器开机运行
docker 的其他一些命令
docker ps 查看正在运行的容器docker ps -a 查看所有容器docker images 或 docker image ls 查看所有镜像docker rm CONTAINER [CONTAINER...] 删除容器docker rmi IMAGE [IMAGE...] 删除镜像
使用 Navicat 连接 Mysql
先看看如何进入 mysql 容器中运行命令:$ docker exec -it mysql bash ,This will create a new Bash session in the container mysql$ mysql -uroot -proot 登录mysql查看一下 root 等用户信息:select user,host,plugin from mysql.user; 如果发现 root 后面是 caching_sha2_password,那么执行下面两条命令
mysql
> ALTER user
'root'@
'%'IDENTIFIED WITH mysql_native_password BY
'root';
mysql
> FLUSH PRIVILEGES
;
Navicat 连接,成功! 如果是在服务器创建容器,要进行远程连接,需要放行映射的端口才行!
参考:https://class.imooc.com/lesson/1293#mid=30329 参考:https://www.runoob.com/docker/ubuntu-docker-install.html