天天看點

docker環境部署及在docker中部署mysql服務詳細1、docker環境部署

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,注意如果沒有指定密碼,會啟動失敗           
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 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)