Docker引擎安装和问题解决

Docker是什么?

Docker是一个开源的应用容器的引擎,是基于GO语言并遵从Apache2.0协议开源

Docker可以让开发者打包他们的应用以及依赖包到一个轻量级的、可移植的容器中。然后发布到任何流行的Linux机器上,也可以实现虚拟化

Docker的应用场景

  1. Web应用的自动化打包和发布
  2. 自动化测试和持续集成、发布
  3. 在服务型环境中部署和调整数据库或去其他的后台应用
  4. 从头编译或者扩展现有的Openshift或Cloud Foundry平台来搭建自己的PaaS环境

Docker的优点

  1. 快速,一致地交付您的应用程序
  2. 响应式部署和扩展
  3. 在同一硬件上运行更多工作负载

Docker架构

Docker包括三个基本概念:

镜像(Image): Docker镜像(Image),就相当于是一个root文件系统。

比如官方镜像Ubuntu:16.04就包含了完整Ubuntu16.04 最小系统的 root 文件系统。

容器(Container): 镜像(Image)和容器(Container)的关系,就想是面对对象程序实际的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。

仓库(Repository):仓库可看成是一个代码控制中心,用来保存镜像。

CentOS Docker安装

Docker支持一下的64位CentOS版本:

  • CentOS7
  • CentOS8
  • 更高版本

较旧的Docker版本为docker或docker-engine。如果已安装这些程序,请写在他们以及相关的依赖项。

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

在新主机上首次安装Docker Engine-Community之前,需要设置Docke仓库。之后,就可以从仓库安装和更新Docker。

设置仓库

安装所需的软件包。yum-untils提供了yum-config-manager,并且device mapper存储驱动程序需要device-mapper-persistent-data和lvm2。

# 安装 yum-untils
sudo yum install -y yum-untils
# 设置 yum 源为阿里云   方便下载 Docker Engine
sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
# 使用官方源地址(比较慢)
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

安装 Docker Engine-Community

安装最新版本的 Docker Engine-Community 和 containerd,或者转到下一步安装特定版本:

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

如果提示您接受 GPG 密钥,请选是。

有多个 Docker 仓库吗?

如果启用了多个 Docker 仓库,则在未在 yum install 或 yum update 命令中指定版本的情况下,进行的安装或更新将始终安装最高版本,这可能不适合您的稳定性需求。

Docker 安装完默认未启动。并且已经创建好 docker 用户组,但该用户组下没有用户。

Docker 的启动和停止

# 启动 docker
sudo systemctl start docker
# 停止 docker
sudo systemctl stop docker
# 重启 docker
sudo systemctl restart docker
# 设置开机启动
sudo systemctl enable docker
# 查看 docker 状态
sudo systemctl status docker
# 查看 docker 内容器的运行状态
sudo docker stats
# 查看 docker 概要信息
sudo docker info
# 查看 docker 帮助文档
sudo docker --help

安装和校验

[root@localhost ~]# docker -v
Docker version 19.03.12, build 48a66213fe
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version:      19.03.12
API version:    1.40
Go version:    go1.13.10
Git commit:    48a66213fe
Built:       Mon Jun 22 15:46:54 2020
OS/Arch:      linux/amd64
Experimental:    false
Server: Docker Engine - Community
Engine:
Version:      19.03.12
API version:    1.40 (minimum version 1.12)
Go version:    go1.13.10
Git commit:    48a66213fe
Built:      Mon Jun 22 15:45:28 2020
OS/Arch:     linux/amd64
Experimental:   false
containerd:
Version:      1.2.13
GitCommit:    7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version:      1.0.0-rc10
GitCommit:    dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version:      0.18.0
GitCommit:    fec3683

配置镜像加速

Docker从DockerHub拉取镜像,因为是从国外获取,所以速度比较慢。这里介绍中国科学技术大学
(LUG@USTC)的开源镜像:https://docker.mirrors.ustc.edu.cn 和网易的开源镜像:http://hub-mirror.
c.163.com

USTC 是老牌的 Linux 镜像服务提供者了,USTC 的 Docker 镜像加速器速度很快。USTC 和网易的优
势之一就是不需要注册,属于真正的公共服务。(也可以使用阿里等其他服务商的镜像加速服务)

编辑文件 daemon.json 。(这时候要确保Docker是开启状态)

vi /etc/docker/daemon.json

在文件中输入以下内容并保存。

{
"registry-mirrors": ["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"]
}

重新加载配置信息及重启 Docker 服务。

# 重新加载某个服务的配置文件
sudo systemctl daemon-reload
# 重新启动 docker
sudo systemctl restart docker

测试hello-world

通过运行helloworld镜像来验证Docker Engine是否已正确安装

[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally # 本地找不到 hello-world 镜像
latest: Pulling from library/hello-world # 拉取最新版本的 hello-world 镜像
0e03bdcc26d7: Pull complete
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
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/

安装Docker的时候遇到的问题

安装yum-untils报错

http://people.centos.org/tru/devtools-2/7/x86_64/RPMS/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
正在尝试其它镜像。
To address this issue please refer to the below wiki article 

https://wiki.centos.org/yum-errors

If above article doesn't help to resolve this issue please use https://bugs.centos.org/.



 One of the configured repositories failed (testing 2 devtools for CentOS 7),
 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=testing-devtools-2-centos-7 ...

     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 testing-devtools-2-centos-7
        or
            subscription-manager repos --disable=testing-devtools-2-centos-7

     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=testing-devtools-2-centos-7.skip_if_unavailable=true

failure: repodata/repomd.xml from testing-devtools-2-centos-7: [Errno 256] No more mirrors to try.
http://people.centos.org/tru/devtools-2/7/x86_64/RPMS/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found

解决

cd /etc/yum.repod.d

删除掉与docker相关的文件

# 在此目录下更新yum
yum -y update

猜你喜欢

转载自blog.csdn.net/weixin_45561352/article/details/110182503