一、安装 Docker
CentOS 7 安装 Docker:https://docs.docker.com/engine/install/centos/
1.1、卸载掉旧版本的 Docker
yum remove -y docker docker-client docker-client-latest docker-common
docker-latest docker-latest-logrotate docker-logrotate
docker-selinux docker-engine-selinux docker-engine
1.2、执行以下安装命令去安装依赖包
[root@centos7 ~] yum install -y yum-utils device-mapper-persistent-data lvm2
[root@centos7 ~] sudo yum-config-manager
–add-repo
https://download.docker.com/linux/centos/docker-ce.repo
[root@centos7 ~] yum -y install docker-ce docker-ce-cli containerd.io
[root@centos7 ~]# docker ps --查看docker
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? --docker没有启动
1.3、启动 Docker
[root@centos7 ~]#systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@centos7 ~]#systemctl start docker
[root@centos7 ~]#systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2021-01-16 18:47:43 EST; 6s ago
Docs: https://docs.docker.com
Main PID: 2435 (dockerd)
Memory: 45.1M
CGroup: /system.slice/docker.service
└─2435 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Jan 16 18:47:43 centos7 dockerd[2435]: time=“2021-01-16T18:47:43.051980834-05:00” level=info msg="ccResolverWrapper: …e=grpc
Jan 16 18:47:43 centos7 dockerd[2435]: time=“2021-01-16T18:47:43.051989827-05:00” level=info msg="ClientConn switchin…e=grpc
Jan 16 18:47:43 centos7 dockerd[2435]: time=“2021-01-16T18:47:43.077331225-05:00” level=info msg=“Loading containers: start.”
Jan 16 18:47:43 centos7 dockerd[2435]: time=“2021-01-16T18:47:43.382655173-05:00” level=info msg=“Default bridge (doc…dress”
Jan 16 18:47:43 centos7 dockerd[2435]: time=“2021-01-16T18:47:43.449861379-05:00” level=info msg=“Loading containers: done.”
Jan 16 18:47:43 centos7 dockerd[2435]: time=“2021-01-16T18:47:43.463137686-05:00” level=warning msg="Not using native…erlay2
Jan 16 18:47:43 centos7 dockerd[2435]: time=“2021-01-16T18:47:43.463311349-05:00” level=info msg=“Docker daemon” comm…0.10.2
Jan 16 18:47:43 centos7 dockerd[2435]: time=“2021-01-16T18:47:43.463380194-05:00” level=info msg=“Daemon has complete…ation”
Jan 16 18:47:43 centos7 systemd[1]: Started Docker Application Container Engine.
Jan 16 18:47:43 centos7 dockerd[2435]: time=“2021-01-16T18:47:43.493313812-05:00” level=info msg=“API listen on /var/…sock”
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos7 ~]# docker ps --查看容器
[root@centos7 ~]# docker version --查看版本
[root@centos7 ~]# docker info --查看版本
1.4.docker 创建容器时指定容器ip
Docker创建容器时默认采用bridge网络,自行分配ip,不允许自己指定。
在实际部署中,我们需要指定容器ip,不允许其自行分配ip,尤其是搭建集群时,固定ip是必须的。
我们可以创建自己的bridge网络 : mynet,创建容器的时候指定网络为mynet并指定ip即可
[root@docker ~]# docker network ls – 查看网络模式
[root@docker ~]# docker network create --driver bridge --subnet=172.18.12.0/16 --gateway=172.18.1.1 mynet – 创建一个新的bridge网络
[root@docker ~]# docker network inspect mynet --查看网络信息
二、Mysql安装
[root@centos7 ~]# docker search mysql
[root@centos7 ~]# docker pull mysql:5.7.30
[root@centos7 ~]# docker pull mysql:8.0.20
#宿主机执行以下命令
mkdir -p /usr/local/mysql5730/
mkdir -p /usr/local/mysql8020/
【5.7.30版本】
docker run -d --name mysql5730 -h mysql5730
-p 233:22 -p 3607:3306
–network=mynet --ip 172.18.12.1
-v /sys/fs/cgroup:/sys/fs/cgroup
-v /usr/local/mysql5730/conf:/etc/mysql/conf.d
-e MYSQL_ROOT_PASSWORD=root -e TZ=Asia/Shanghai
mysql:5.7.30
[root@centos7 ~]# docker update --restart=always mysql5730 --开机自启动
[root@centos7 ~]# docker exec -it mysql5730 bash
【8.0.20版本】
docker run -d --name mysql8020 -h mysql8020
-p 234:22 -p 3608:3306
–network=mynet --ip 172.18.12.2
-v /sys/fs/cgroup:/sys/fs/cgroup
-v /usr/local/mysql8020/conf:/etc/mysql/conf.d
-e MYSQL_ROOT_PASSWORD=root -e TZ=Asia/Shanghai
mysql:8.0.20
[root@centos7 ~]# docker update --restart=always mysql8020 --开机自启动
[root@centos7 ~]# docker exec -it mysql8020 bash
注:以上密码均为root