Docker基础(1)——安装Docker
更新:HHH   时间:2023-1-7


配置环境:CentOS-7.5

内存建议最少分配4g;

开启虚拟化:

阿里云开源站点下载docker-ce.repo

[root@www ~]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@www ~]# mv docker-ce.repo /etc/yum.repos.d/
[root@www ~]# yum repolist
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * epel: mirror.horizon.vn
源标识                            源名称                                                    状态
EPEL/7/x86_64                     CentOS-7 - Base - 163.com                                  9,911
base                              cdrom                                                      9,911
docker-ce-stable/x86_64           Docker CE Stable - x86_64                                     25
epel/x86_64                       Extra Packages for Enterprise Linux 7 - x86_64            12,703
extras/7/x86_64                   CentOS-7 - Extras - 163.com                                  434
updates/7/x86_64                  CentOS-7 - Updates - 163.com                               1,614
repolist: 34,598

 

查看一下其配置文件中的baseurl,是否为国内镜像资源站点,假如为外网,建议用末行模式修改过来,否则,不仅慢还需要科学上网;

[root@www ~]# cat /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-edge]
name=Docker CE Edge - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-edge-debuginfo]
name=Docker CE Edge - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-edge-source]
name=Docker CE Edge - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
 
[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

 

然后我们直接用yum安装docker-ce即可;

[root@www ~]# yum install docker-ce
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * epel: mirror.horizon.vn
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-ce.x86_64.3.18.09.0-3.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2.9,它被软件包 3:docker-ce-18.09.0-3.el7.x86_64 需要
--> 正在处理依赖关系 containerd.io,它被软件包 3:docker-ce-18.09.0-3.el7.x86_64 需要
--> 正在处理依赖关系 docker-ce-cli,它被软件包 3:docker-ce-18.09.0-3.el7.x86_64 需要
--> 正在检查事务
---> 软件包 container-selinux.noarch.2.2.68-1.el7 将被 安装
---> 软件包 containerd.io.x86_64.0.1.2.0-3.el7 将被 安装
---> 软件包 docker-ce-cli.x86_64.1.18.09.0-3.el7 将被 安装
--> 解决依赖关系完成
 
依赖关系解决
 
==================================================================================================
 Package                   架构           版本                     源                        大小
==================================================================================================
正在安装:
 docker-ce                 x86_64         3:18.09.0-3.el7          docker-ce-stable          19 M
为依赖而安装:
 container-selinux         noarch         2:2.68-1.el7             extras                    36 k
 containerd.io             x86_64         1.2.0-3.el7              docker-ce-stable          22 M
 docker-ce-cli             x86_64         1:18.09.0-3.el7          docker-ce-stable          14 M
 
事务概要
==================================================================================================
安装  1 软件包 (+3 依赖软件包)
 
总下载量:55 M
安装大小:235 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): container-selinux-2.68-1.el7.noarch.rpm                             |  36 kB  00:00:03    
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/containerd.io-1.2.0-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
containerd.io-1.2.0-3.el7.x86_64.rpm 的公钥尚未安装
(2/4): containerd.io-1.2.0-3.el7.x86_64.rpm                                |  22 MB  00:00:09    
(3/4): docker-ce-cli-18.09.0-3.el7.x86_64.rpm                              |  14 MB  00:00:04    
(4/4): docker-ce-18.09.0-3.el7.x86_64.rpm                                  |  19 MB  00:00:19    
--------------------------------------------------------------------------------------------------
总计                                                              2.8 MB/s |  55 MB  00:00:19    
从 https://mirrors.aliyun.com/docker-ce/linux/centos/gpg 检索密钥
导入 GPG key 0x621E9F35:
 用户ID     : "Docker Release (CE rpm) <docker@docker.com>"
 指纹       : 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 来自       : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
是否继续?[y/N]:y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : containerd.io-1.2.0-3.el7.x86_64                                              1/4
  正在安装    : 1:docker-ce-cli-18.09.0-3.el7.x86_64                                          2/4
  正在安装    : 2:container-selinux-2.68-1.el7.noarch                                         3/4
setsebool:  SELinux is disabled.
  正在安装    : 3:docker-ce-18.09.0-3.el7.x86_64                                              4/4
  验证中      : 1:docker-ce-cli-18.09.0-3.el7.x86_64                                          1/4
  验证中      : 3:docker-ce-18.09.0-3.el7.x86_64                                              2/4
  验证中      : containerd.io-1.2.0-3.el7.x86_64                                              3/4
  验证中      : 2:container-selinux-2.68-1.el7.noarch                                         4/4
 
已安装:
  docker-ce.x86_64 3:18.09.0-3.el7                                                               
 
作为依赖被安装:
  container-selinux.noarch 2:2.68-1.el7             containerd.io.x86_64 0:1.2.0-3.el7           
  docker-ce-cli.x86_64 1:18.09.0-3.el7            
 
完毕!

建议对docker设置enable属性,并开启docker,并查看其版本;

[root@www ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@www ~]# systemctl start  docker
[root@www ~]# docker version
Client:
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:48:22 2018
 OS/Arch:           linux/amd64
 Experimental:      false
 
Server: Docker Engine - Community
 Engine:
  Version:          18.09.0
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:            Wed Nov  7 00:19:08 2018
  OS/Arch:          linux/amd64
  Experimental:     false

 

Docker是由独立的仓库的,其内容一般都docker hub上,所以我们需要在docker hub上申请一个账号,地址是https://hub.docker.com


注意

1. 假如我们第一次申请,我们的浏览器也没有科学上网的插件,那么最后的sign up选项就是灰色无法点击的,因为我们没有办法收到邮件,所以我们需要在我们的浏览器上安装一个插件,以便我们可以科学上网,收到回执邮件;

2. 具体插件名称为:谷歌访问助手

3. 教程链接:http://www.ggfwzs.com

 

1.申请完账号后,我们可以简单的做一个nginx小镜像进行测试:

2.在tag标签里选择1.14-apline版本;

3.本地安装nginx:1.14-apline版本(默认先查找本地镜像仓库,没有的话就去docker hub上查找,然后下载到本地,默认不会有动作,所以我们需要在开启一个Xshell借口来查看信息);

[root@www ~]# docker run --name ngx nginx:1.14-alpine
Unable to find image 'nginx:1.14-alpine' locally
1.14-alpine: Pulling from library/nginx
4fe2ade4980c: Pull complete
c691664ebb08: Pull complete
a6f6a50701b6: Pull complete
5980ba3b5a39: Pull complete
Digest: sha256:75cf17cdf89cbd8da65c83050ebdab1026b98cf217442d6a1f2a8892f47967d7
Status: Downloaded newer image for nginx:1.14-alpine

4.开启另一个Xshell接口,查看运行信息;

[root@www ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
2fc0b111434b        nginx:1.14-alpine   "nginx -g 'daemon of…"   About a minute ago   Up About a minute   80/tcp              ngx
[root@www ~]#
[root@www ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.14-alpine         77bae8d00654        5 days ago          17.7MB

 

5.通过inspect查看ngx镜像的IP地址(最后几行中有,172.17.0.2);

[root@www ~]# docker inspect ngx
[
    {
        "Id": "2fc0b111434b579f6ffe080abd914cb921be070f20c55ed02ed7645e9a4d7a59",
        "Created": "2018-11-12T12:23:37.39282113Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 61533,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-11-12T12:23:45.219006468Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:77bae8d0065423e2338884d3698ef5ff9de8dec05a55dc81cf48ae9e78008b3f",
        "ResolvConfPath": "/var/lib/docker/containers/2fc0b111434b579f6ffe080abd914cb921be070f20c55ed02ed7645e9a4d7a59/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/2fc0b111434b579f6ffe080abd914cb921be070f20c55ed02ed7645e9a4d7a59/hostname",
        "HostsPath": "/var/lib/docker/containers/2fc0b111434b579f6ffe080abd914cb921be070f20c55ed02ed7645e9a4d7a59/hosts",
        "LogPath": "/var/lib/docker/containers/2fc0b111434b579f6ffe080abd914cb921be070f20c55ed02ed7645e9a4d7a59/2fc0b111434b579f6ffe080abd914cb921be070f20c55ed02ed7645e9a4d7a59-json.log",
        "Name": "/ngx",
        "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,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "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,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/asound",
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/d1b00e9ba9e8cbafe57bd530da02f7c22a5086e9e1d37ab995d697acb859c502-init/diff:/var/lib/docker/overlay2/9fbe29a9d6e34b27b9151fbb798c9f26a22bf0d2a47938759d1197da5324eb5f/diff:/var/lib/docker/overlay2/569ebba8fa873429effbfd7075a08f61f2fadcf1e09c61fb9feacf7adee21710/diff:/var/lib/docker/overlay2/cd229da595a1b8a9fdd3c40b8c7cad2f9abc0835603abfe5e5226ad4f738e0bb/diff:/var/lib/docker/overlay2/2cd347e2a7d82ceeb12f18b279dda0fa0853f13ab3149aa0787f3f7c685ba622/diff",
                "MergedDir": "/var/lib/docker/overlay2/d1b00e9ba9e8cbafe57bd530da02f7c22a5086e9e1d37ab995d697acb859c502/merged",
                "UpperDir": "/var/lib/docker/overlay2/d1b00e9ba9e8cbafe57bd530da02f7c22a5086e9e1d37ab995d697acb859c502/diff",
                "WorkDir": "/var/lib/docker/overlay2/d1b00e9ba9e8cbafe57bd530da02f7c22a5086e9e1d37ab995d697acb859c502/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "2fc0b111434b",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.14.1"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "nginx:1.14-alpine",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "ac090b600a21020aa323ccd020688bb0ef7c46df744739d5345996294e663afd",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/ac090b600a21",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "207649bd5ec943874679a88995a60c07d48b176704106ccd0c95bf226227332d",
            "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": "6714eb554a2843bd0582a3e7d63b09b175c5351c100aab08b1a051bc8187cb5c",
                    "EndpointID": "207649bd5ec943874679a88995a60c07d48b176704106ccd0c95bf226227332d",
                    "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
                }
            }
        }
    }
]

 

6.本地用curl测试ngx镜像的启动状态;

[root@www ~]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h2>Welcome to nginx!</h2>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
 
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
 
<p><em>Thank you for using nginx.</em></p>
</body>
</html>


返回云计算教程...