配置:VMware虚拟机、Ubuntu24.04.1
首先打开vm启动虚拟机
下载docker
Ubuntu启动之后,按CTRL+ALT+T 打开终端
1.更新软件包索引并安装依赖
sudo apt-get update sudo apt-get install \ ca-certificates \ curl \ gnupg \ lsb-release
2.添加docker官方的GPG密钥
sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
3.设置docker源
这里解释一下,设置源的作用是为了方便我们后期拉取镜像
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
注释:上面的路径就是我们源的路径,也可以设置自己想用的源
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
4.更新APT缓存
sudo apt-get update
5.安装docker
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
6.检查是否成功
docker -v查看版本,如果有类似于Docker version 24.0.7, build afdd53b的输出就说明docker安装好了
sudo systemctl status docker检查docker服务状态,它会显示很多数据,如果有active(runing)就说明正在运行
sudo docker pull hello-world拉取镜像测试,出现以下提示说明成功了
然后我们继续
docker images 查看我们拉取到的镜像
我们这里拉取的是hello-world,所以就直接运行hello-world
使用:docker run 对应的IMAGE ID
运行成功会显示👇
xhq@user:~$ 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:
The Docker client contacted the Docker daemon.
The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
- The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
- 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:
For more examples and ideas, visit:
7.设置docker开机自启
sudo systemctl enable docker
验证方法:
sudo systemctl is-enabled docker # 应显示 "enabled"
提示输入密码(Ubuntu开机密码)之后会显示查询结果,出现enable说明成功
8. 重启docker
在某些操作进行更改之后如果查询发现没有改变,可以尝试重启一次docker
保持容器运行的重启 sudo systemctl reload docker # 部分配置支持热重载 强制重启,会停止容器运行 sudo systemctl restart docker
9.docker拉取MySQL
我们这里拉取的是8.0版本的mysql
首先使用docker的pull命令
docker pull mysql:8.0
然后查看是否拉取成功
docker images
如果出现了mysql就说明成功了
我们就运行mysql
docker run --name mysql8\ -e MYSQL_ROOT_PASSWORD=yourpassword\ 注释:上行设置的是超级用户root的密码 如果想设置一个普通用户,就需要-e MYSQL_USER=myuser -e MYSQL_PASSWORD=mypassword命令 -p 3306:3306\ -v mysql_data:/var/lib/mysql\ --restart unless-stopped\ -d mysql:8.0 注释:这段指令只需要第一次运行的时候需要,后续在想运行MySQL的时候就直接使用
docker start mysql8运行
docker stop mysql8停止
当MySQL运行的时候我们使用命令
docker exec -it 74fe79651dfe /bin/bash
成功之后会显示
然后再输入 mysql -u用户 -p密码进入
比如 mysql -uroot -p123456
进入之后就可以正常的操作MySQL了
xshell下载以及连接Ubuntu虚拟机
xshell下载地址:家庭/学校免费 - NetSarang Website
下载安装完成后打开xshell,在此之前需要确保虚拟机和主机之间是可以互相ping通的
详情见:vm虚拟机 Ubuntu ping失败情况解决方法-CSDN博客
打开xshell之后会自动弹出一个
选择会话弹窗的左上角的新建
名称自己填,主机为Ubuntu的ip地址(使用ip a命令查看ip) 然后点击右侧的连接,会弹出下图对话框

点击接受并保存

输入用户名(命令:whoami)
然后输入密码即可连接,这样我们就可以在xshell上运行Ubuntu了
