当前位置:网站首页>centos7部署docker,安装mysql

centos7部署docker,安装mysql

2022-06-23 03:52:00 韧小钊

docker安装部署

创建部署用户,赋予sudo权限

[[email protected] ~]# useradd docker -d /home/docker
[[email protected] ~]# passwd docker
更改用户 docker 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[[email protected] ~]# echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
[[email protected] ~]# su - docker
[[email protected] ~]$ 

一键部署

[[email protected] ~]$ #使用官方安装脚本自动安装
[[email protected] ~]$ curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

国内daocloud一键安装命令:
curl -sSL https://get.daocloud.io/docker | sh
在这里插入图片描述

启动

[[email protected] ~]$ sudo systemctl start docker

查看状态

[[email protected] ~]$ systemctl status docker

设置开机启动

[[email protected] ~]$ # 设置开机启动
[[email protected] ~]$ sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[[email protected] ~]$ 

镜像操作

查看镜像

[[email protected] ~]$  docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

下载镜像(超时错)

[[email protected] ~]$ docker pull hello-world
Using default tag: latest
Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: TLS handshake timeout

指定镜像重新拉取

[[email protected] ~]$ sudo vi /etc/docker/daemon.json
[[email protected] ~]$ cat /etc/docker/daemon.json
{
    "registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]
}
[[email protected] ~]$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete 

Digest: sha256:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[[email protected] ~]$ 
[docker[email protected] ~]$  docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB

删除镜像

[[email protected] ~]$ docker rmi hello-world
Untagged: hello-world:latest
Untagged: [email protected]:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
[[email protected] ~]$  docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[[email protected] ~]$ 

容器操作

#### 查看全部容器
[[email protected] ~]$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

创建容器

[[email protected] ~]$ docker create -it --name=test hello-world
aaa8f6652052d88c159e53ac9f88cbfa77934c8f7b31a615ec9f18acedb88f1e
[[email protected] ~]$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS    PORTS     NAMES
aaa8f6652052   hello-world   "/hello"   8 seconds ago   Created             test

启动容器

[[email protected] ~]$ docker start aaa8f6652052
aaa8f6652052
[[email protected] ~]$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                     PORTS     NAMES
aaa8f6652052   hello-world   "/hello"   29 seconds ago   Exited (0) 3 seconds ago             test

查看日志

[[email protected] ~]$ docker logs aaa8f6652052

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. 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:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

查看容器详情

[[email protected] ~]$ docker inspect aaa8f6652052
[
    {
        "Id": "aaa8f6652052d88c159e53ac9f88cbfa77934c8f7b31a615ec9f18acedb88f1e",
        "Created": "2022-05-07T06:56:57.904666417Z",
        "Path": "/hello",
        "Args": [],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-05-07T06:57:22.999901707Z",
            "FinishedAt": "2022-05-07T06:57:23.018206468Z"
        },
        "Image": "sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412",
        "ResolvConfPath": "/var/lib/docker/containers/aaa8f6652052d88c159e53ac9f88cbfa77934c8f7b31a615ec9f18acedb88f1e/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/aaa8f6652052d88c159e53ac9f88cbfa77934c8f7b31a615ec9f18acedb88f1e/hostname",
        "HostsPath": "/var/lib/docker/containers/aaa8f6652052d88c159e53ac9f88cbfa77934c8f7b31a615ec9f18acedb88f1e/hosts",
        "LogPath": "/var/lib/docker/containers/aaa8f6652052d88c159e53ac9f88cbfa77934c8f7b31a615ec9f18acedb88f1e/aaa8f6652052d88c159e53ac9f88cbfa77934c8f7b31a615ec9f18acedb88f1e-json.log",
        "Name": "/test",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "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/66c71552979e600283024720c28fce1bbffdba0baeb948675a02c5d6ee31cc9e-init/diff:/var/lib/docker/overlay2/176b33a38f9b52db5535389d92450c23bbd3f1beb06036d93b9853ea9550a3f1/diff",
                "MergedDir": "/var/lib/docker/overlay2/66c71552979e600283024720c28fce1bbffdba0baeb948675a02c5d6ee31cc9e/merged",
                "UpperDir": "/var/lib/docker/overlay2/66c71552979e600283024720c28fce1bbffdba0baeb948675a02c5d6ee31cc9e/diff",
                "WorkDir": "/var/lib/docker/overlay2/66c71552979e600283024720c28fce1bbffdba0baeb948675a02c5d6ee31cc9e/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "aaa8f6652052",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/hello"
            ],
            "Image": "hello-world",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "46ecfbf481103b2f5bc9d5cbae762c0eb427589483e905ed1d9216e530525d39",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/46ecfbf48110",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "c0c4813d6f77ca6c8b0b6c4faddd8a2041a3e1f93c56acd5fb22533b70e96444",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]
[[email protected] ~]$ 

卸载docker

docker-ce社区版,docker-ee企业版
在这里插入图片描述

[[email protected] ~]$ sudo yum remove docker-ce -y
[[email protected] ~]$ sudo rm -rf /var/lib/docker

卸载完,还能看到版本号,哪里没卸载干净???
[[email protected] ~]$ docker -v
Docker version 20.10.15, build fd82621
[[email protected] ~]$

卸载旧版本命令

[[email protected] ~]$ sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine -y
已加载插件:fastestmirror
参数 docker 没有匹配
参数 docker-client 没有匹配
参数 docker-client-latest 没有匹配
参数 docker-common 没有匹配
参数 docker-latest 没有匹配
参数 docker-latest-logrotate 没有匹配
参数 docker-logrotate 没有匹配
参数 docker-engine 没有匹配
不删除任何软件包
[[email protected] ~]$ 

手动部署

设置仓库

安装依赖包

sudo yum install -y yum-utils device-mapper-persistent-data lvm2 

设置仓库地址,选择阿里云源地址(官方源地址比较慢)

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装Docker社区版

sudo yum install -y docker-ce docker-ce-cli containerd.io

docker安装MySQL

拉取MySQL镜像文件

MySQL镜像地址
在这里插入图片描述

[[email protected] ~]$ sudo docker pull mysql:8.0.25
8.0.25: Pulling from library/mysql
b4d181a07f80: Pull complete 
a462b60610f5: Pull complete 
578fafb77ab8: Pull complete 
524046006037: Pull complete 
d0cbe54c8855: Pull complete 
aa18e05cc46d: Pull complete 
32ca814c833f: Pull complete 
9ecc8abdb7f5: Pull complete 
ad042b682e0f: Pull complete 
71d327c6bb78: Pull complete 
165d1d10a3fa: Pull complete 
2f40c47d0626: Pull complete 
Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b
Status: Downloaded newer image for mysql:8.0.25
docker.io/library/mysql:8.0.25
[[email protected] ~]$ 

查看镜像

[[email protected] ~]$ sudo  docker images
REPOSITORYTAG   IMAGE ID   CREATED SIZE
hello-world   latestfeb5d9fea6a5   7 months ago13.3kB
mysql 8.0.255c62e459e087   10 months ago   556MB
[[email protected] ~]$ 

启动MySQL镜像

docker run -itd --name test-mysql1 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

参数说明:

  • -p 3307:3306 :映射容器服务的 3306 端口到宿主机的 3307 端口,外部主机可以直接通过宿主机ip:3307访问到MySQL 的服务
  • MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码。

机器之前装了mysql,因此端口改用3307,否则冲突!

验证

在这里插入图片描述

原网站

版权声明
本文为[韧小钊]所创,转载请带上原文链接,感谢
https://renxiaozhao.blog.csdn.net/article/details/124636140