1、docker环境部署

1.1 检查系统版本

docker要求CentOS 系统的内核版本高于 3.10

uname -r

1.2 更新系统

使用root 权限登录 CentOS。确保 yum 包更新到最新。

yum -y update

1.3 卸载旧版本

如果安装过旧版本,就会卸载当前版本;如果没安装过,运行上面的命令也没关系,只是提示未安装

yum remove docker docker-common docker-selinux docker-engine

1.4 安装docker并启动

#安装需要的软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
根据你的发行版下载repo文件(如果没有wget命令,则下载yum -y install wget)
wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
把软件仓库地址替换为 TUNA
sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
安装docker
yum makecache fast # 清缓存
 
yum install docker-ce # 安装docker-ce
启动并加入开机启动
systemctl start docker
systemctl enable docker
验证安装是否成功
docker version

1.5 安装Docker Compose

Docker Compose 可以轻松、高效地管理容器,他是一个用于定义和运行多容器Docker的应用程序工具。

从国内镜像 get.daocloud.io 快速下载:
curl -L https://get.daocloud.io/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose 
给执行文件添加执行权限
chmod +x /usr/local/bin/docker-compose
输入命令测试
docker-compose --version #返回版本号则代表正常
配置docker国内镜像
为了确保docker能快速加载应用程序镜像,配置镜像列表,指向国内的镜像地址。

修改 /etc/docker/daemon.json 文件并添加上 registry-mirrors 键值。
cat /etc/docker/daemon.json
{

"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]

}
保存修改后重启docker
systemctl restart docker

#2、Docker中部署mysql服务

2.1 从Docker Hub拉取官方mysql镜像

docker pull mysql

2.2验证镜像是否拉取成功

docker images

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
mysql        latest    5a4e492065c7   43 hours ago   514MB

2.2创建mysql容器

docker run -d --name mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

参数介绍:

d表示在后台运行,不随当前命令行窗口的退出而退出

--name给容器起一个别名,以后可以通过这个别名管理此容器

-p 3307:3307把宿主机的3307端口映射到Mysql容器的3306端口 

-e MySQL容器的环境配置

MYSQL_ROOT_PASSWORD=123456   指定mysql的密码,用户名默认为root,注意如果没有指定密码,会启动失败

查看容器地址IP

docker inspect 容器名称或 id
docker inspect 7a82c5300dbc
[
    {
        "Id": "7a82c5300dbc33ae8ae39db451e79deb747e8e5dec5b30ff590ce24c5114d819",
        "Created": "2021-08-19T06:14:34.764599801Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 20479,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-08-19T06:14:35.917956509Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5a4e492065c722ec8cc7413552bafc6fd5434c5ad90797e898ccc4e347e21aa5",
        "ResolvConfPath": "/var/lib/docker/containers/7a82c5300dbc33ae8ae39db451e79deb747e8e5dec5b30ff590ce24c5114d819/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/7a82c5300dbc33ae8ae39db451e79deb747e8e5dec5b30ff590ce24c5114d819/hostname",
        "HostsPath": "/var/lib/docker/containers/7a82c5300dbc33ae8ae39db451e79deb747e8e5dec5b30ff590ce24c5114d819/hosts",
        "LogPath": "/var/lib/docker/containers/7a82c5300dbc33ae8ae39db451e79deb747e8e5dec5b30ff590ce24c5114d819/7a82c5300dbc33ae8ae39db451e79deb747e8e5dec5b30ff590ce24c5114d819-json.log",
        "Name": "/mysql",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "3306/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "3307"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/1126e757d8936ddb89fef92d6544bb2ed29446227f2626c6659178345580c0bc-init/diff:/var/lib/docker/overlay2/395be7625b6b9dcdae9036540a378cc8275bf88c39660f6c3466581f688861a4/diff:/var/lib/docker/overlay2/df29aca1c81b5d8c1002e79867e77a67d2eccb04e57bea7001afd0e2ffa679a2/diff:/var/lib/docker/overlay2/80326139f50e7d9dea63cf89aecc8d58a34ef98857f3fc1ad7e2edb4365ac6f4/diff:/var/lib/docker/overlay2/fd5f659a08be42c8527aa483a662291dc748f05342d7e22f4e8901d573461606/diff:/var/lib/docker/overlay2/52f04bf229aefa6b3a6c406d1b43c2c25190bf1863ea2eef5e2e6dea3a216e71/diff:/var/lib/docker/overlay2/da359c9362bff0de010f79885062b087e115336c3bd88e865fc9064baf1b774e/diff:/var/lib/docker/overlay2/81c170914c1e106406378b05cbfe390b2489a67dd7723f331ce501f43f224d6a/diff:/var/lib/docker/overlay2/9297a5001ec3e1a2cbd2caba27c46726131138f449039825b51883bc75c157de/diff:/var/lib/docker/overlay2/ffe353ab3671e660016043ba55eb38fe2822413533c3e4b51fe2fd19fcff46be/diff:/var/lib/docker/overlay2/e9d1a965022a5a18f28d2e0a9b438dfa516f279370ee19338c0e149e96bfcffd/diff:/var/lib/docker/overlay2/a916e6b429287475ce016c144efc8490cd1e0305e983755457ce0eed1f9061b8/diff:/var/lib/docker/overlay2/969db2af0b06b62ff4656d68934d237f230602e4a745a50520dc909467057f5b/diff",
                "MergedDir": "/var/lib/docker/overlay2/1126e757d8936ddb89fef92d6544bb2ed29446227f2626c6659178345580c0bc/merged",
                "UpperDir": "/var/lib/docker/overlay2/1126e757d8936ddb89fef92d6544bb2ed29446227f2626c6659178345580c0bc/diff",
                "WorkDir": "/var/lib/docker/overlay2/1126e757d8936ddb89fef92d6544bb2ed29446227f2626c6659178345580c0bc/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "44cd95541004262b3a036611f4c1bdccfa5cb07d00546164be794eaf7b429272",
                "Source": "/var/lib/docker/volumes/44cd95541004262b3a036611f4c1bdccfa5cb07d00546164be794eaf7b429272/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "7a82c5300dbc",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "MYSQL_ROOT_PASSWORD=123456",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.12",
                "MYSQL_MAJOR=8.0",
                "MYSQL_VERSION=8.0.26-1debian10"
            ],
            "Cmd": [
                "mysqld"
            ],
            "Image": "mysql",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "933fc9266e2a91d1f03d5de4a415d2de2bbfd1cc50faceecf1b40f24049bdc4f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "3307"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "3307"
                    }
                ],
                "33060/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/933fc9266e2a",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "bcd580c4e15e6b347593a38d2740572c1353205a695bf8e8b442fe6505ea0e33",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "067e0331864030a9e0e14a8c0d8a3eccbe6033e569f99711595db2c3d8689f52",
                    "EndpointID": "bcd580c4e15e6b347593a38d2740572c1353205a695bf8e8b442fe6505ea0e33",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

2.3进入mysql容器

使用的docker exec命令,-it是参数,bash表示创建一个交互界面

docker exec -it mysql bash
root@7a82c5300dbc:/# mysql -uroot -p    #跟之前进去数据库的命令一样
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases; 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.03 sec)