一、docker介绍
容器是轻量级的,包含应用运行所需所有东西(代码、库、运行时环境、系统设置,以及依赖关系)的独立的包。每个容器都部署于它自己的 CPU、内存、块 I/O,以及网络资源上,所有这些都不依赖于某个内核和操作系统。这也是容器与虚拟机之间最大的不同;相比之下,虚拟机是一个运行于宿主机操作系统上的完整的操作系统平台,而容器不是。容器允许你以一种前所未有的方式扩展交付能力(不管内部还是外部的)
docker和VM虚拟机的区别在于:
- 容器提供了基于进程的隔离,而虚拟机提供了资源的完全隔离。
- 虚拟机可能需要一分钟来启动,而容器只需要一秒钟或更短。
- 容器使用宿主操作系统的内核,而虚拟机使用独立的内核
docker发行版本分为:
- Docker CE(社区版):免费开源版本-Docker Engine
- Docker EE(企业版):商用收费版本-Docker Data Center
docker安装环境:
- 公有云环境:基于阿里云以及腾讯云主机等安装Docker
- 私有云环境:基于自主主机环境安装Docker
Docker 平台基本上由三部分组成:
- 客户端:用户使用 Docker 提供的工具(CLI 以及 API 等)来构建,上传镜像并发布命令来创建和启动容器Docker
- 主机:从 Docker registry 上下载镜像并启动容器Docker
- registry:Docker 镜像仓库,用于保存镜像,并提供镜像上传和下载。
二、Docker容器安装部署
2.1 Docker容器部署
# 查看系统开发环境 [root@VM_0_10_centos docker_install]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) # 查看系统内核版本 [root@VM_0_10_centos docker_install]# uname -a Linux VM_0_10_centos 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux 或 [root@VM_0_10_centos docker_install]# cat /proc/version Linux version 3.10.0-957.21.3.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-3 6) (GCC) ) #1 SMP Tue Jun 18 16:35:19 UTC 2019
在增加Docker阿里云yum源之前需要执行基础组件安装以及卸载历史版本:
# 基础组件安装 [root@VM_0_10_centos docker_install]# yum -y install gcc gcc-c++ # 卸载历史版本 [root@VM_0_10_centos docker_install]# yum -y remove docker docker-common docker-selinux docker-engine Loaded plugins: fastestmirror, langpacks No Match for argument: docker No Match for argument: docker-common No Match for argument: docker-selinux No Match for argument: docker-engine No Packages marked for removal # 或删除已安装的docker [root@VM_0_10_centos docker_install]# yum -y remove docker \ > docker-client \ > docker-client-latest \ > docker-common \ > docker-latest \ > docker-latest-logrotate \ > docker-selinux \ > docker-engine-selinux \ > docker-engine Loaded plugins: fastestmirror, langpacks No Match for argument: docker No Match for argument: docker-client No Match for argument: docker-client-latest No Match for argument: docker-common No Match for argument: docker-latest No Match for argument: docker-latest-logrotate No Match for argument: docker-selinux No Match for argument: docker-engine-selinux No Match for argument: docker-engine No Packages marked for removal
PS:Docker部署要求64位的系统且内核版本至少为3.10
# 查看yum
[root@VM_0_10_centos docker_install]# yum -y install epel-release [root@VM_0_10_centos docker_install]# yum clean all Loaded plugins: fastestmirror, langpacks Repository epel is listed more than once in the configuration Cleaning repos: epel extras os updates Cleaning up list of fastest mirrors [root@VM_0_10_centos docker_install]# yum list
2.2 Docker安装
2.2.1 部署Docker添加yum源:
# 安装yum工具 [root@VM_0_10_centos docker_install]# yum -y install yum-utils device-mapper-persistent-data lvm2 # 配置docker阿里云yum源 [root@VM_0_10_centos docker_install]# yum-config-manager --add-repo=http://mirrors.aliyun.com/docker-ce/linux/centos/docke r-ce.repoLoaded plugins: fastestmirror, langpacks Repository epel is listed more than once in the configuration adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo # 更新yum缓存 [root@VM_0_10_centos docker_install]# yum -y makecache fast # 查看对应版本 [root@VM_0_10_centos docker_install]# yum list docker-ce --showduplicates
2.2.2 安装docker最新版本:
# 安装yum install docker-ce docker-ce-cli 或者 yum install docker-io –y [root@VM_0_10_centos docker_install]# yum -y install docker-ce docker-ce-cli
2.2.3 启动docker服务
[root@VM_0_10_centos docker_install]# systemctl start docker [root@VM_0_10_centos docker_install]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
2.2.4 检验安装docker服务信息
[root@VM_0_10_centos docker_install]# docker -v Docker version 19.03.5, build 633a0ea [root@VM_0_10_centos docker_install]# docker version Client: Docker Engine - Community Version: 19.03.5 API version: 1.40 Go version: go1.12.12 Git commit: 633a0ea Built: Wed Nov 13 07:25:41 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.5 API version: 1.40 (minimum version 1.12) Go version: go1.12.12 Git commit: 633a0ea Built: Wed Nov 13 07:24:18 2019 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.2.10 GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339 runc: Version: 1.0.0-rc8+dev GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 docker-init: Version: 0.18.0 GitCommit: fec3683 [root@VM_0_10_centos docker_install]# docker info Client: Debug Mode: false Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 19.03.5 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-957.21.3.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 1.795GiB Name: VM_0_10_centos ID: ZYOK:IVEB:436X:2EJF:HMYJ:A6RA:XXNW:4WY7:4YL7:MIR7:GO33:XMLX Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled
2.3 Docker配置阿里云镜像加速器
阿里云官网配置:
# 默认etc下已经有docker目录,这里不用重复创建 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://j98vrcq8.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
编辑配置文件:vim /etc/docker/daemon.json
[root@VM_0_10_centos docker]# vi daemon.json { "registry-mirrors": ["https://j98vrcq8.mirror.aliyuncs.com"] }
[root@VM_0_10_centos docker]# systemctl daemon-reload
[root@VM_0_10_centos docker]# systemctl restart docker
[root@VM_0_10_centos docker]# docker info Client: Debug Mode: false Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 19.03.5 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-957.21.3.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 1.795GiB Name: VM_0_10_centos ID: ZYOK:IVEB:436X:2EJF:HMYJ:A6RA:XXNW:4WY7:4YL7:MIR7:GO33:XMLX Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: https://j98vrcq8.mirror.aliyuncs.com/ Live Restore Enabled: false WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled
2.4 Docker容器基本组件部署与安装
2.4.1 Compose组件部署与安装:
Docker Compose是一个用来定义和运行复杂应用的Docker工具。一个Docker容器的应用,通常由多个容器组成。Compose 通过一个配置文件来管理多个Docker容器,在配置文件中,所有的容器通过services来定义。然后使用docker-compose脚本来启动,停止和重启应用,和应用中的服务以及所有依赖服务的容器,非常适合组合使用多个容器进行开发的场景
安装docker-compose的方式:
方式一:从github上下载docker-compose二进制文件安装下载最新版的docker-compose文件
# curl -L参数是表示跳转到新网址下载;-o输出文件路径;-i:显示http response的头信息;-I只显示头部信息 [root@VM_0_10_centos docker]# curl -L https://github.com/docker/compose/releases/download/1.25.1-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 617 0 617 0 0 958 0 --:--:-- --:--:-- --:--:-- 958 100 16.2M 100 16.2M 0 0 105k 0 0:02:37 0:02:37 --:--:-- 127k # 添加可执行权限 [root@VM_0_10_centos docker]# chmod +x /usr/local/bin/docker-compose # 卸载(删除该文件即可) [root@VM_0_10_centos docker]# rm /usr/local/bin/docker-compose
方式二:使用pip install命令安装
# 使用pip安装需要先安装pip命令 [root@VM_0_10_centos docker]# yum -y install python-pip # 安装 [root@VM_0_10_centos docker]# pip install docker-compose # 卸载 [root@VM_0_10_centos docker]# pip uninstall docker-compose
检验docker-compose安装结果:
[root@VM_0_10_centos docker]# docker-compose version docker-compose version 1.25.1-rc1, build d92e9bee docker-py version: 4.1.0 CPython version: 3.7.4 OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
2.4.2 Machine组件部署与安装
Docker Machine是一个工具,可让您在虚拟主机上安装Docker Engine,并使用docker-machine命令管理主机。
通过github下载安装machine组件:
[root@VM_0_10_centos docker]# curl -L https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-`uname -s `-`uname -m` > /usr/local/bin/docker-machine [root@VM_0_10_centos docker]# chmod +x /usr/local/bin/docker-machine
参考博客:
https://www.cnblogs.com/mazhilin/p/11553510.html