您的位置:首页 > 运维架构 > Docker

Docker相关知识了解学习及Cenos 7安装Docker详细教程【附docker常用命令】

2020-03-05 09:40 1241 查看

Docker相关知识了解学习及Cenos 7安装Docker详细教程【附docker常用命令】

一 ,前言
  Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
  Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
  Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。
二 ,Docker的应用场景
  Web 应用的自动化打包和发布。
  自动化测试和持续集成、发布。
  在服务型环境中部署和调整数据库或其他的后台应用。
  从头编译或者扩展现有的 OpenShift 或 Cloud Foundry 平台来搭建自己的 PaaS 环境。
三,Docker 的优点
  Docker 是一个用于开发,交付和运行应用程序的开放平台。Docker 使您能够将应用程序与基础架构分开,从而可以快速交付软件。借助 Docker,您可以与管理应用程序相同的方式来管理基础架构。通过利用 Docker 的方法来快速交付,测试和部署代码,您可以大大减少编写代码和在生产环境中运行代码之间的延迟。
1、快速,一致地交付您的应用程序
  Docker 允许开发人员使用您提供的应用程序或服务的本地容器在标准化环境中工作,从而简化了开发的生命周期。
  容器非常适合持续集成和持续交付(CI / CD)工作流程,请考虑以下示例方案:
  您的开发人员在本地编写代码,并使用 Docker 容器与同事共享他们的工作。
  他们使用 Docker 将其应用程序推送到测试环境中,并执行自动或手动测试。
  当开发人员发现错误时,他们可以在开发环境中对其进行修复,然后将其重  新部署到测试环境中,以进行测试和验证。
  测试完成后,将修补程序推送给生产环境,就像将更新的镜像推送到生产环境一样简单。
2、响应式部署和扩展
  Docker 是基于容器的平台,允许高度可移植的工作负载。Docker 容器可以在开发人员的本机上,数据中心的物理或虚拟机上,云服务上或混合环境中运行。
Docker 的可移植性和轻量级的特性,还可以使您轻松地完成动态管理的工作负担,并根据业务需求指示,实时扩展或拆除应用程序和服务。
3、在同一硬件上运行更多工作负载
  Docker 轻巧快速。它为基于虚拟机管理程序的虚拟机提供了可行、经济、高效的替代方案,因此您可以利用更多的计算能力来实现业务目标。Docker 非常适合于高密度环境以及中小型部署,而您可以用更少的资源做更多的事情。
四 Docker 架构

  • 镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
  • 容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
  • 仓库(Repository):仓库可看着一个代码控制中心,用来保存镜像
    Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。
    Docker 容器通过 Docker 镜像来创建。
    容器与镜像的关系类似于面向对象编程中的对象与类。
Docker 面向对象
容器 对象
镜像

Docker 镜像(Images): Docker 镜像是用于创建 Docker 容器的模板,比如 Ubuntu 系统。
Docker 容器(Container): 容器是独立运行的一个或一组应用,是镜像运行时的实体
Docker 客户端(Client): Docker 客户端通过命令行或者其他工具使用 Docker SDK(https://docs.docker.com/develop/sdk/)与 Docker 的守护进程通信。
Docker 主机(Host): 一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。
Docker 仓库(Registry): Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。
  Docker Hub(https://hub.docker.com) 提供了庞大的镜像集合供使用。
  一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。
  通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 <仓库名>:<标签> 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest 作为默认标签。
Docker Machine: Docker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox、 Digital Ocean、Microsoft Azure。
五 Docker 安装
本文讲解 CentOS Docker 安装;
(1)Docker 支持以下的 64 位 CentOS 版本:
   CentOS 7 更高版本…
该 centos-extras 库必须启用。默认情况下,此仓库是启用的,但是如果已禁用它,则需要重新启用它。
(2)安装
下载wget

yum install wget -y

[root@localhost ~]# yum install wget -y
Loaded plugins: fastestmirror
base                                                                                                                                         | 3.6 kB  00:00:00
extras                                                                                                                                       | 2.9 kB  00:00:00
updates                                                                                                                                      | 2.9 kB  00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.cqu.edu.cn
* extras: mirrors.cqu.edu.cn
* updates: mirrors.cqu.edu.cn
Package wget-1.14-18.el7_6.1.x86_64 already installed and latest version //显示已经存在
Nothing to do

创建目录

mkdir -p /etc/yum.repos.d

下载阿里云yum源配置
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2020-02-16 15:48:41--  http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.19.210.112, 111.19.177.248, 111.19.177.241, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.19.210.112|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’
100%[==========================================================================================================================>] 2,523       --.-K/s   in 0s
2020-02-16 15:48:41 (353 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]

更新缓存

yum clean all
yum makecache

[root@localhost ]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
base                                                                                                                                         | 3.6 kB  00:00:00
extras                                                                                                                                       | 2.9 kB  00:00:00
updates                                                                                                                                      | 2.9 kB  00:00:00
(1/10): base/7/x86_64/group_gz                                                                                                               | 165 kB  00:00:00
(2/10): extras/7/x86_64/filelists_db                                                                                                         | 210 kB  00:00:00
(3/10): extras/7/x86_64/other_db                                                                                                             | 103 kB  00:00:00
(4/10): base/7/x86_64/other_db                                                                                                               | 2.6 MB  00:00:00
(5/10): updates/7/x86_64/filelists_db                                                                                                        | 4.0 MB  00:00:00
(6/10): updates/7/x86_64/other_db                                                                                                            | 481 kB  00:00:00
base/7/x86_64/primary_db       FAILED
http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/repodata/04efe80d41ea3d94d36294f7107709d1c8f70db11e152d6ef562da344748581a-primary.sqlite.bz2: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; Unknown error"
Trying other mirror.
(7/10): updates/7/x86_64/primary_db                                                                                                          | 6.7 MB  00:00:01
(8/10): base/7/x86_64/primary_db                                                                                                             | 6.0 MB  00:00:00
base/7/x86_64/filelists_db     FAILED
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/41232548001a78473ae0f2d4b92e1ec28f7a0593e0495056515887fe2a39b416-filelists.sqlite.bz2: [Errno 12] Timeout on http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/41232548001a78473ae0f2d4b92e1ec28f7a0593e0495056515887fe2a39b416-filelists.sqlite.bz2: (28, 'Connection timed out after 30002 milliseconds')
Trying other mirror.
extras/7/x86_64/primary_db     FAILED                                          ==========================================         ] 346 kB/s |  24 MB  00:00:12 ETA
http://mirrors.aliyuncs.com/centos/7/extras/x86_64/repodata/8c048743b0740639c36ce95b6464f0d6c186db3981672c058c133d9f6cbf2485-primary.sqlite.bz2: [Errno 12] Timeout on http://mirrors.aliyuncs.com/centos/7/extras/x86_64/repodata/8c048743b0740639c36ce95b6464f0d6c186db3981672c058c133d9f6cbf2485-primary.sqlite.bz2: (28, 'Connection timed out after 30000 milliseconds')
Trying other mirror.
(9/10): base/7/x86_64/filelists_db                                                                                                           | 7.3 MB  00:00:00
(10/10): extras/7/x86_64/primary_db                                                                                                          | 159 kB  00:00:00
Determining fastest mirrors
1. base: mirrors.aliyun.com
2. extras: mirrors.aliyun.com
3. updates: mirrors.aliyun.com
Metadata Cache Created

安装前置条件 :
docker要求Linux内核要3.0以上版本,如果centos6.x 内核是2.x,需要升级linux内核
我虚拟机直接安装的是centos 7 64位 输入uname -r 查看内核
安装步骤

  1. 升级yum包
    yum update
  2. 安装工具集
    yum install -y yum-utils device-mapper-persistent-data lvm2
  3. 卸载原有旧版本
[root@localhost ]# yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \docker-logrotate \docker-engine
  1. 安装 Docker Engine-Community

    在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,您可以从仓库安装和更新 Docker。
    (1) 设置仓库:安装所需的软件包。yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

[root@localhost ]# yum install -y yum-utils \  device-mapper-persistent-data \  lvm2

如果yum 出现 Loaded plugins: fastestmirror解决办法
1.修改插件配置文件

vim /etc/yum/pluginconf.d/fastestmirror.conf

[main]
enabled=0    //由 1 改成0 ,禁用该插件
verbose=0
always_print_best_host = true
socket_timeout=3
#  Relative paths are relative to the cachedir (and so works for users as well
# as root).
hostfilepath=timedhosts.txt
maxhostfileage=10
maxthreads=15
#exclude=.gov, facebook
#include_only=.nl,.de,.uk,.ie

修改yum 配置文件

# vim /etc/yum.conf 9 plugins=0 //不使用插件

清除缓存并重新构建yum 源
# yum clean all, yum makecache

(2)使用以下命令来设置稳定的仓库。

[root@localhost /]# yum-config-manager \ --add-repo \https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@localhost /]#

更新yum缓存

yum makecache fast
yum clean all

(3)如果安装特定版本
可查看仓库
  a、列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。

[root@localhost /]# yum list docker-ce --showduplicates | sort -r
Installed Packages
docker-ce.x86_64            3:19.03.6-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.6-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.5-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.3-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.2-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.1-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.0-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.3.ce-3.el7                   docker-ce-stable
docker-ce.x86_64            18.06.2.ce-3.el7                   docker-ce-stable
docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos            @docker-ce-stable

  b、通过其完整的软件包名称安装特定版本,(安装跟CentOs匹配的版本)。建议安装18.03.0.ce-1.el7.centos

[root@localhost /]# yum install docker-ce.x86_64.0.18.03.0.ce-1.el7.centos

(4)如果安装最新版本的 Docker Engine-Community 和 containerd直接运行下列命令(不建议,本人尝试安装最新的启动报错,版本不匹配)

[root@localhost /]#  yum install docker-ce docker-ce-cli containerd.io

Docker 安装完默认未启动。并且已经创建好 docker 用户组,但该用户组下没有用户。
(五) 查看docker版本

docker -v

[root@localhost /]# docker -v
Docker version 19.03.6, build 369ce74a3c

(六) 启动docker服务

systemctl start docker

[root@localhost /]#  systemctl start docker

(七) 查看状态

systemctl status docker.service

[root@localhost /]# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2020-02-16 18:36:10 CST; 10s ago
Docs: https://docs.docker.com
Main PID: 28575 (dockerd)
Tasks: 15
Memory: 36.0M
CGroup: /system.slice/docker.service
├─28575 /usr/bin/dockerd
└─28579 docker-containerd --config /var/run/docker/containerd/containerd.toml

(八) Docker验证是否安装成功

docker run hello-world

[root@localhost /]# docker run hello-world
Unable to find image 'hello-world:latest' locally

控制台如果出现上述结果长时间没反应可能原因:docker在本地没有找到hello-world镜像,也没有从docker仓库中拉取镜像,出项这个问题的原因:是应为docker服务器再国外,我们在国内
无法正常拉取镜像,所以就需要我们为docker设置国内阿里云的镜像加速器;
修改一下文件内容

vim /etc/docker/daemon.json
添加

{
“registry-mirrors”: [“https://aeckruos.mirror.aliyuncs.com”],
“insecure-registries”: [“192.168.6.129:5000”]
}

[root@localhost /]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://aeckruos.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.6.129:5000"]
}
~
~
~

重启:

systemctl restart docker

测试:
docker run hello-world

[root@localhost /]# docker run hello-world
Unable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
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/

[root@localhost /]#

(八)开机启动配置

systemctl enable docker.service

[root@localhost /]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

至此CentOs 7 安装Docker完成!

镜像安装教程传送门:

附:常用命令
(1)镜像相关

命令 作用
docter pull NAME[:TAG] 使用pull命令从docker hub仓库下载镜像到本地
docker tag oldName newName 为本地镜像添加新的标签
docker images] 列出本地主机上已有的镜像
docker search TERM] 默认搜索docker hub官方仓库的镜像
docker rmi 镜像id或者镜像名称] 删除镜像
docker commit [OPTIONS] CONTAINER[REPOSITORY[:TAG]]] 基于已有镜像的容器创建镜像,可以创建比较复杂的镜像。-a:作者信息,-m:提交信息,-p:提交时暂停容器运行
docker import -NAME[:TAG]] 基于本地模板导入镜像
docker build -t NAME[:TAG]] dockerfile路径 基于dockerfile创建
docker save –o xxx.tar NAME[:TAG]] 存出镜像到本地文件
docker load <xxxx.tar] 载入镜像
docker inspect 镜像id] 查看镜像详细信息

(2)容器相关

命令 作用
docker create –it NAME[:TAG] 创建容器
docker start NAME[:TAG] 启动容器
docker run –it NAME[:TAG] 等于先执行docker create命令,再执行docker start,-it交互式,守护态运行,通过run后加-d实现
docker stop[-t –time[=10]]
docker restart NAME[:TAG] 启动状态重新启动
docker ps –a –q 查看处于终止的容器id信
docker ps 查看正在运行的容器
docker ps –a 查看所有容器
docker rm 容器id 删除容器,需要先停止
docker logs 容器id 查看容器日志
docker port <container-name container-id> 端口
docker inspect 容器id 查看容器信息
docker exec -u root -it containerID /bin/bash 指定用户进入容器操作
env 容器内部输入env查看容器内部环境变量
docker cp <host_path>container-id :<container_path> 复制本地文件到容器
docker cp container-id:<container_path><host_path> 复制容器文件到本地
docker network create app_net 创建自定义网络
docker network connect app_net container-name 连接网络

传送门:Docker 容器使用最详细实测教程及相关命令使用(附镜像,容器命令干货)!!

原文参考:
https://blog.csdn.net/u011477914/article/details/86591452
https://blog.csdn.net/ai147258150/article/details/100540275
https://blog.csdn.net/weixin_39477597/article/details/87715899

仅供参考学习!

  • 点赞
  • 收藏
  • 分享
  • 文章举报
iT_MaNongZYQ 发布了10 篇原创文章 · 获赞 2 · 访问量 1051 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: