PHP 第一次安装使用docker

446 阅读6分钟

作为一个拿来主义的phper,之前一直都是用的 centOS自带的cockpit来使用容器技术,最近感觉这样有局限性,所以完全跑一遍命令。

一、查看docker官网的安装说明

地址:docs.docker.com/

找到文档中的docker引擎文档:Install Docker Engine | Docker Documentation

我这里用的centOS,所以找到centOS

二、安装

执行命令看看是否存在docker

docker -v

1.卸载旧版本

 sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2.安装依赖工具

 yum install -y yum-utils

3.设置镜像仓库

!!!千万不要执行官方的命令,因为是国外镜像,执行下面两个命令:

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum list docker-ce --showduplicates | sort -r

4.更新yum软件包索引

为了以后yum安装速度快,执行当前命令

有可能失败,失败了重试直至成功

失败如下:

[root@localhost ~]# yum makecache fast
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                       | 9.2 kB  00:00:00
 * base: mirrors.aliyun.com
 * centos-sclo-rh: mirrors.aliyun.com
 * epel: mirrors.bfsu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                       | 3.6 kB  00:00:00
centos-sclo-rh                                                                             | 3.0 kB  00:00:00
https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.aliyun.com; 未知的错误"
正在尝试其它镜像。


 One of the configured repositories failed (Docker CE Stable - x86_64),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=docker-ce-stable ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable docker-ce-stable
        or
            subscription-manager repos --disable=docker-ce-stable

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=docker-ce-stable.skip_if_unavailable=true

failure: repodata/repomd.xml from docker-ce-stable: [Errno 256] No more mirrors to try.
https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.aliyun.com; 未知的错误"

成功如下:

[root@localhost ~]# yum makecache fast
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                       | 9.2 kB  00:00:00
 * base: mirrors.aliyun.com
 * centos-sclo-rh: mirrors.aliyun.com
 * epel: mirrors.bfsu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                       | 3.6 kB  00:00:00
centos-sclo-rh                                                                             | 3.0 kB  00:00:00
docker-ce-stable                                                                           | 3.5 kB  00:00:00
extras                                                                                     | 2.9 kB  00:00:00
updates                                                                                    | 2.9 kB  00:00:00
元数据缓存已建立
[root@localhost ~]#

5.执行安装命令 yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

安装过程需要手动输入几次 “Y”

[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * centos-sclo-rh: mirrors.aliyun.com
 * epel: mirrors.bfsu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 containerd.io.x86_64.0.1.6.4-3.1.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 containerd.io-1.6.4-3.1.el7.x86_64 需要
---> 软件包 docker-ce.x86_64.3.20.10.15-3.el7 将被 安装
--> 正在处理依赖关系 docker-ce-rootless-extras,它被软件包 3:docker-ce-20.10.15-3.el7.x86_64 需要
---> 软件包 docker-ce-cli.x86_64.1.20.10.15-3.el7 将被 安装
--> 正在处理依赖关系 docker-scan-plugin(x86-64),它被软件包 1:docker-ce-cli-20.10.15-3.el7.x86_64 需要
---> 软件包 docker-compose-plugin.x86_64.0.2.5.0-3.el7 将被 安装
--> 正在检查事务
---> 软件包 container-selinux.noarch.2.2.119.2-1.911c772.el7_8 将被 安装
---> 软件包 docker-ce-rootless-extras.x86_64.0.20.10.15-3.el7 将被 安装
--> 正在处理依赖关系 fuse-overlayfs >= 0.7,它被软件包 docker-ce-rootless-extras-20.10.15-3.el7.x86_64 需要
--> 正在处理依赖关系 slirp4netns >= 0.4,它被软件包 docker-ce-rootless-extras-20.10.15-3.el7.x86_64 需要
---> 软件包 docker-scan-plugin.x86_64.0.0.17.0-3.el7 将被 安装
--> 正在检查事务
---> 软件包 fuse-overlayfs.x86_64.0.0.7.2-6.el7_8 将被 安装
--> 正在处理依赖关系 libfuse3.so.3(FUSE_3.2)(64bit),它被软件包 fuse-overlayfs-0.7.2-6.el7_8.x86_64 需要
--> 正在处理依赖关系 libfuse3.so.3(FUSE_3.0)(64bit),它被软件包 fuse-overlayfs-0.7.2-6.el7_8.x86_64 需要
--> 正在处理依赖关系 libfuse3.so.3()(64bit),它被软件包 fuse-overlayfs-0.7.2-6.el7_8.x86_64 需要
---> 软件包 slirp4netns.x86_64.0.0.4.3-4.el7_8 将被 安装
--> 正在检查事务
---> 软件包 fuse3-libs.x86_64.0.3.6.1-4.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==================================================================================================================
 Package                          架构          版本                                源                       大小
==================================================================================================================
正在安装:
 containerd.io                    x86_64        1.6.4-3.1.el7                       docker-ce-stable         33 M
 docker-ce                        x86_64        3:20.10.15-3.el7                    docker-ce-stable         22 M
 docker-ce-cli                    x86_64        1:20.10.15-3.el7                    docker-ce-stable         29 M
 docker-compose-plugin            x86_64        2.5.0-3.el7                         docker-ce-stable        7.0 M
为依赖而安装:
 container-selinux                noarch        2:2.119.2-1.911c772.el7_8           extras                   40 k
 docker-ce-rootless-extras        x86_64        20.10.15-3.el7                      docker-ce-stable        8.2 M
 docker-scan-plugin               x86_64        0.17.0-3.el7                        docker-ce-stable        3.7 M
 fuse-overlayfs                   x86_64        0.7.2-6.el7_8                       extras                   54 k
 fuse3-libs                       x86_64        3.6.1-4.el7                         extras                   82 k
 slirp4netns                      x86_64        0.4.3-4.el7_8                       extras                   81 k

事务概要
==================================================================================================================
安装  4 软件包 (+6 依赖软件包)

总下载量:104 M
安装大小:418 M
Is this ok [y/d/N]: y
Downloading packages:
(1/10): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm                               |  40 kB  00:00:00
(2/10): containerd.io-1.6.4-3.1.el7.x86_64 8% [==-                              ] 1.0 MB/s | 9.1 MB  00:01:33 ETA

安装成功最后显示:

已安装:
  containerd.io.x86_64 0:1.6.4-3.1.el7                  docker-ce.x86_64 3:20.10.15-3.el7
  docker-ce-cli.x86_64 1:20.10.15-3.el7                 docker-compose-plugin.x86_64 0:2.5.0-3.el7

作为依赖被安装:
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8       docker-ce-rootless-extras.x86_64 0:20.10.15-3.el7
  docker-scan-plugin.x86_64 0:0.17.0-3.el7                 fuse-overlayfs.x86_64 0:0.7.2-6.el7_8
  fuse3-libs.x86_64 0:3.6.1-4.el7                          slirp4netns.x86_64 0:0.4.3-4.el7_8

完毕!

6.启动docker

systemctl start docker

配置开机自启动docker

systemctl enable docker

7.测试是否安装成功

[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.15
 API version:       1.41
 Go version:        go1.17.9
 Git commit:        fd82621
 Built:             Thu May  5 13:16:58 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.15
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.9
  Git commit:       4433bf6
  Built:            Thu May  5 13:15:18 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

8.执行hello-world

出现“Hello from Docker!” 标识docker安装成功

[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67
Status: Downloaded newer image for hello-world:latest

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/

三、配置阿里云镜像加速器

1.在阿里云后台开通容器镜像服务的 【个人版】或者【企业版】

2.然后在镜像工具中找到对应操作系统的配置命令逐行执行

3.执行镜像加速配置命令

[root@localhost ~]# mkdir -p /etc/docker
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://no15i93v.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://no15i93v.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker