docker介绍及安装部署

一、docker简介

1、Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
2、Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
3、容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
4、Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。
5、Docker是通过内核虚拟化技术(namespaces及cgroups)来提供容器的资源隔离与资源限制。由于Docker通过操作系统层的虚拟化实现隔离(对操作系统的内核有要求),所以Docker容器在运行时,不需要类似虚拟机(VM)额外的操作系统开销,从而比kvm虚拟机更轻量。也可以把docker理解为一种简单的打包技术

docker目标

docker的主要目标是"Build,Ship and Run any App,Angwhere",构建,运输,处处运行
构建:制作docker镜像,打包容器的所有系统目录文件
运输:下载docker镜像
运行:基于docker镜像提供的rootfs,启动容器
总结:只要能运行docker容器,那么docker镜像中已经安装好的软件也可以运行,所以说docker是一种件的打包技术。

应用场景

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

docker优势

  docker介绍及安装部署
1:解决了操作系统和软件运行环境的依赖例如:nginx和git需要安装的openssl版本不同,在同一台设备上安装会造成软件冲突
2:对于开发人员来说,再也不用担心不会部署开发环境
3:开发环境,测试环境和生产环境高度一致。
4:让用户体验产品新特性的又一种思路。
5:容器不需要进行硬件虚拟以及运行完整操作系统等额外开销,Docker 对系统资源的利用率更高。无论是应用执行速度、内存损耗或者文件存储速度,都要比传统虚拟机技术更高效。因此,相比虚拟机技术,一个相同配置的主机,往往可以运行更多数量的应用。
6:传统的虚拟机技术启动应用服务往往需要数分钟,而 Docker 容器应用,由于直接运行于宿主内核,无需启动完整的操作系统,因此可以做到秒级、甚至毫秒级的启动时间。大大的节约了开发、测试、部署的时间。
7:由于 Docker 确保了执行环境的一致性,使得应用的迁移更加容易。Docker 可以在很多平台上运行,无论是物理机、虚拟机、公有云、私有云,甚至是笔记本,其运行结果是一致的。因此用户可以很轻易的将在一个平台上运行的应用,迁移到另一个平台上,而不用担心运行环境的变化导致应用无法正常运行的情况。
8:Docker 使用的分层存储以及镜像的技术,使得应用重复部分的复用更为容易,也使得应用的维护更新更加简单,基于基础镜像进一步扩展镜像也变得非常简单。此外,Docker 团队同各个开源项目团队一起维护了一大批高质量的 官方镜像,既可以直接在生产环境使用,又可以作为基础进一步定制,大大的降低了应用服务的镜像制作成本。
docker介绍及安装部署

Docker与虚拟机的区别

docker介绍及安装部署

二、docker的架构

1、docker三个基本概念:

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

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。

Docker 容器通过 Docker 镜像来创建。

2、docker的镜像分层

一个完整的Docker镜像可以支撑一个Docker容器的运行,在Docker容器运行过程中主要提供文件系统数据支撑。

Docker镜像作为docker中最基本的概念,有以下几个特性:

    镜像分层,每个镜像都由一个或多个镜像层组成;
    可通过在某个镜像加上一定的镜像层得到新镜像(此过程可通过编写dockerfile或基于容器Commit实现);
    每个镜像层拥有唯一镜像ID;
    镜像在存储和使用时共享相同的镜像层(根据ID),所以在pull镜像时,已有的镜像层会自动跳过下载;
    每个镜像层都是只读,即使启动成容器,也无法对其真正的修改,修改只会作用于最上层的容器层;

docker介绍及安装部署

Docker容器,可以理解为一个或多个运行进程,而这些运行进程将占有相应的内存,相应的CPU计算资源,相应的虚拟网络设备以及相应的文件系统资源。而Docker容器所占用的文件系统资源,则通过Docker镜像的镜像层文件来提供。

3、镜像与容器的联系

当启动一个新的容器时,Docker会加载只读镜像,并在其之上添加一个读写层,即容器层。

docker 容器=镜像+可读层

docker介绍及安装部署

 

4、镜像存储核心技术:联合文件系统

镜像的高效存储:引入联合文件系统,将镜像多层文件联合挂载到容器文件系统。

docker介绍及安装部署

 

5、镜像存储核心技术:写时复制(COW)

引入写时复制(copy-on-write),需要修改文件操作时,会先从镜像里把要写的文件复制到自己的文件系统(容器的读写层)中进行修改。源镜像层中的文件不会发生变化。

docker介绍及安装部署

 

三、docker的安装部署

centos系统

1、安装环境

[root@inode3 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) 
[root@inode3 ~]# uname -r 3.10.0-862.el7.x86_64

2、centos7使用yum安装(推荐的方式)

docker介绍及安装部署
#下载docker-ce的yum源
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
#把docker-ce源的地址修改为清华源的地址
sed -i 's#download.docker.com#mirrors.tuna.tsinghua.edu.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo
#更新docker-ce.repo
yum makecache fast
#安装docker-ce
yum install -y docker-ce
docker介绍及安装部署

下载安装指定版本

docker介绍及安装部署
#查看docker-ce的版本
[root@inode3 ~]# yum list docker-ce.x86_64 --showduplicates | sort -r  
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
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            17.12.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.3.ce-1.el7                   docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable 
Available Packages

......
docker介绍及安装部署

安装指定版本

yum -y install docker-ce-17.03.2.ce

安装报错(虚拟机中可能会遇到,如果没有报错请忽略)

Error: Package: docker-ce-17.03.2.ce-1.el7.centos.x86_64 (docker-ce-stable)           Requires: docker-ce-selinux >= 17.03.2.ce-1.el7.centos           
Available: docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch (docker-ce-stable)               docker-ce-selinux = 17.03.0.ce-1.el7.centos           
Available: docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch (docker-ce-stable)               docker-ce-selinux = 17.03.1.ce-1.el7.centos           
Available: docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch (docker-ce-stable)               docker-ce-selinux = 17.03.2.ce-1.el7.centos           
Available: docker-ce-selinux-17.03.3.ce-1.el7.noarch (docker-ce-stable)                   docker-ce-selinux = 17.03.3.ce-1.el7

报错原因: docker-ce-selinux 版本过低

解决办法:https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/Packages/网站下载对应版本的docker-ce-selinux,安装即可

yum -y install https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.3.ce-1.el7.noarch.rpm

重新安装docker-ce-17.03.2.ce成功

这次安装文档版本 docker-18.03.1-ce

(1)下载安装

mkdir /data
cd /data/
wget -c https://download.docker.com/linux/static/stable/x86_64/docker-18.03.1-ce.tgz
tar -xvf docker-18.03.1-ce.tgz 

(2)配置启动脚本

vim /usr/lib/systemd/system/docker.service

docker介绍及安装部署
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target
docker介绍及安装部署

(3)配置生效环境变量,方便使用docker命令

cat > /etc/profile.d/docker.sh <<EOF
export PATH=/data/docker:$PATH
EOF
source /etc/profile.d/docker.sh

(4)配置docker命令补齐脚本

wget -O /usr/share/bash-completion/completions/docker https://raw.githubusercontent.com/alonghub/Docker/master/Resource/docker 

4、Ubuntu 14.04 16.04 (使用apt-get进行安装)

(1)安装最新版本

# step 1: 安装必要的一些系统工具

sudo apt-get updatesudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

# step 2: 安装GPG证书

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

# Step 3: 写入软件源信息

sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

# Step 4: 更新并安装 Docker-CE

sudo apt-get -y updatesudo apt-get -y install docker-ce 

(2)安装指定版本的Docker-CE:

docker介绍及安装部署
# Step 1: 查找Docker-CE的版本:
# apt-cache madison docker-ce
#   docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
#   docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages

# Step 2: 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce-0~ubuntu-xenial)
# sudo apt-get -y install docker-ce=[VERSION]
docker介绍及安装部署

5、启动docker

 
[root@inode1 ~]# systemctl start docker
[root@inode1 ~]# ps -ef |grep docker
root      46215      1  2 20:26 ?        00:00:00 /usr/bin/dockerdroot      46218  46215  0 20:26 ?        00:00:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runcroot      46349  37852  0 20:26 pts/0    00:00:00 grep --color=auto docker

四、docker命令无法tab键补全

#1、安装bash-complete
yum install -y bash-completion
#2、刷新文件
source /usr/share/bash-completion/completions/docker
source /usr/share/bash-completion/bash_completion
上一篇:html的爱恨情仇—部署HTMl5结婚微信电子请柬(含源码)


下一篇:从零开始算法之路 ---- 997. 找到小镇的法官