我使用的是虚拟机 mobaXterm连接虚拟机
建议虚拟机里的CentOS下载DVD版本,我之前下载minimal版本,虽说体积非常小,但是很多配置都需要自己搞,wget yum这些命令都没有。
第一步:先卸载掉系统可能存在的Docker容器
[root@localhost ~]# yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine \
docker-selinux
第二步:安装yum工具,可能出现以下错误:Could not retrieve mirrorlist
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
【解决方案:设置为阿里云镜像】
1.先备份当前数据源
[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
2.从阿里云镜像中下载新的CentOS-Base.repo到/etc/yum.repos.d/
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
3.清理yum下载的软件包和缓存
[root@localhost ~]# yum clean all
4.生成缓存
[root@localhost ~]# yum makecache
【解决完上面问题后再次执行安装yum工具命令】
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
第三步:配置Docker的yum源
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
第四步:更新yum,建立缓存
[root@localhost ~]# yum makecache fast
第五步:指执行Docker安装命令,安装完成后启动docker
[root@localhost ~]# yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
安装成功后,执行以下操作来配置启动Docker服务
1.设置Docker开机自启动
[root@localhost ~]# systemctl enable docker
2.启动Docker
[root@localhost ~]# systemctl start docker
3.执行docker ps命令,如果不报错,说明安装启动成功
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
其他命令如下:
重启docker
[root@localhost ~]# systemctl restart docker
停止Docker
[root@localhost ~]# systemctl stop docker
至此 到这一步,Docker算是安装完成,后面的可以说是扩展操作
第六步:拉取MySQL,出现以下问题:docker: Error response from daemon:Get "https://registry-1.docker.io/v2/"
[root@localhost ~]# docker run -d --name mysql -p 3306:3306 -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=590618 mysql
Unable to find image 'mysql:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.
【解决方案】配置Dockerhub镜像源
1.创建docker目录
[root@localhost ~]# mkdir -p /etc/docker
2.配置所有可用镜像源
[root@localhost ~]#
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://docker.imgdb.de",
"https://docker-0.unsee.tech",
"https://docker.hlmirror.com",
"https://cjie.eu.org"
]
}
EOF
3.重新加载守护进程,重启docker
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker\
如何查看镜像源是否已经配置成功
[root@localhost ~]# docker pull hello-world
/*若出现以下内容,则表示配置成功了*/
Using default tag: latest
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:7e1a4e2d11e2ac7a8c3f768d4166c2defeb09d2a750b010412b6ea13de1efb19
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
我的mysql版本号是8.1.0
win+R输入cmd,输入mysql -help 查看版本号
在这里要注意mysql8.0和5.0的版本是有变化的,是在密码加密规则那里,所以我这里需要做些变化
1、首先拉取mysql
docker pull mysql:8.1.0
2、查看docker里安装的镜像(记住这里的id)
docker images
3、启动mysql服务
docker run --name=mysql -it \
-p 3306:3306 \
-e TZ=Asia/Shanghai \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_ROOT_HOST=% \
-v /root/mysql/data:/var/lib/mysql \
-v /root/mysql/init:/docker-entrypoint-initdb.d \
-v /root/mysql/conf:/ets/mysql/conf.d \
-d ae2502152260
这里的ae2502152260是我的mysql的id
启动好之后,会返回一个容器 ID,长度为64位。这个容器 ID 是 Docker 为镜像分配的唯一标识符,可以用于对容器进行操作和管理。
例如:212d567b85dcff51dceb5b3d7d7636fba248647792166d1ff4e41a6e6012ad12
接下来的修改mysql的加密规则可以参考这篇文章解决:连接不上 docker中的mysql_thinkphp6 连不上 docker mysql-优快云博客