mazhilin · 2019年09月20日

Linux环境基于CentOS7 搭建部署Docker容器

1.Docker容器概述

  • 区分Docker容器技术和VM虚拟机技术:

evernotecid://394EFE90-9CE0-4D65-A8CD-DFEC0DC8061E/appyinxiangcom/11359837/ENResource/p1872file
Docker容器技术 [Container]:容器虚拟化技术
evernotecid://394EFE90-9CE0-4D65-A8CD-DFEC0DC8061E/appyinxiangcom/11359837/ENResource/p1873file
VM虚拟机技术[Virtual Machine]:传统虚拟化技术
evernotecid://394EFE90-9CE0-4D65-A8CD-DFEC0DC8061E/appyinxiangcom/11359837/ENResource/p1873file
主要区别是:

* [1] 容器提供了基于进程的隔离,而虚拟机提供了资源的完全隔离。
* [2] 虚拟机可能需要一分钟来启动,而容器只需要一秒钟或更短。
* [3] 容器使用宿主操作系统的内核,而虚拟机使用独立的内核
  • 容器的好处
    1.2.0 容器
  • Docker容器发行版本:
    Docker CE(社区版):免费开源版本-Docker Engine
    Docker EE(企业版):商用收费版本-Docker Data Center
  • Docker容器安装环境:
    公有云环境:基于阿里云以及腾讯云主机等安装Docker
    私有云环境:基于自主主机环境安装Docker
  • Docker 平台的基本构成:

evernotecid://394EFE90-9CE0-4D65-A8CD-DFEC0DC8061E/appyinxiangcom/11359837/ENResource/p1874file
Docker 平台基本上由三部分组成:

  • [1] 客户端:用户使用 Docker 提供的工具(CLI 以及 API 等)来构建,上传镜像并发布命令来创建和启动容器Docker
  • [2] 主机:从 Docker registry 上下载镜像并启动容器Docker
  • [3] registry:Docker 镜像仓库,用于保存镜像,并提供镜像上传和下载后面的文章会具体分析。

2.Docker容器部署与安装

Docker容器部署

查看 Centos7 开发环境:cat /etc/centos-release

 [root@centos-amber ~]# cat /etc/centos-release
  CentOS Linux release 7.6.1810 (Core) 
 [root@centos-amber ~]# 

查看 Centos7 内核版本:uname -a 或者 cat /proc/version

[root@centos-amber ~]# uname -a
Linux centos-amber 3.10.0-957.27.2.el7.x86_64 #1 SMP Mon Jul 29 17:46:05 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
[root@centos-amber ~]# 

在增加Docker阿里云yum源之前需要执行基础组件安装以及卸载历史版本:

基础组件安装:yum -y install gcc & yum -y install gcc-c++

#gcc 环境安装
yum -y install gcc
yum -y install gcc-c++

卸载历史版本:yum -y remove docker docker-common docker-selinux docker-engine

[root@centos-amber ~]# yum -y remove docker docker-common docker-selinux docker-engine
Loaded plugins: fastestmirror
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
[root@centos-amber ~]# 

或者 删除已安装的Docker

# Uninstall installed docker
[root@centos-amber ~]# sudo yum remove docker \
>                   docker-client \
>                   docker-client-latest \
>                   docker-common \
>                   docker-latest \
>                   docker-latest-logrotate \
>                   docker-logrotate \
>                   docker-selinux \
>                   docker-engine-selinux \
>                   docker-engine
Loaded plugins: fastestmirror
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-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
[root@centos-amber ~]# 
Ps: Docker部署要求64位的系统且内核版本至少为3.10

2.0.2 部署Docker添加yum源:yum install epel-release –y && yum list

[root@centos-amber ~]# yum install epel-release –y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package epel-release-7-11.noarch already installed and latest version
Nothing to do
[root@centos-amber ~]# 
[root@centos-amber ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates
Cleaning up list of fastest mirrors
[root@centos-amber ~]# yum list
2.1 Docker容器安装

配置阿里云Docker Yum源

  • [1] Set up repository :

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

[root@centos-amber docker]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
.............
[root@centos-amber docker]# 
  • [2] Use Aliyun Docker

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/doc...

[root@centos-amber docker]# sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
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
[root@centos-amber docker]# 

更新Yum缓存:sudo yum -y makecache fast

[root@centos-amber ~]# sudo yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base                                                                                | 3.6 kB  00:00:00     
docker-ce-stable                                                                    | 3.5 kB  00:00:00     
epel                                                                                | 5.3 kB  00:00:00     
extras                                                                              | 3.4 kB  00:00:00     
updates                                                                             | 3.4 kB  00:00:00     
(1/2): docker-ce-stable/x86_64/updateinfo                                           |   55 B  00:00:00     
(2/2): docker-ce-stable/x86_64/primary_db                                           |  32 kB  00:00:00     
Metadata Cache Created
[root@centos-amber ~]# sudo yum -y makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base                                                                                | 3.6 kB  00:00:00     
docker-ce-stable                                                                    | 3.5 kB  00:00:00     
epel                                                                                | 5.3 kB  00:00:00     
extras                                                                              | 3.4 kB  00:00:00     
updates                                                                             | 3.4 kB  00:00:00     
Metadata Cache Created
[root@centos-amber ~]# 

查看对应版本:yum list docker-ce --showduplicates

[root@centos-amber docker]# yum list docker-ce --showduplicates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
docker-ce-stable                                                                                                                                                                                    | 3.5 kB  00:00:00     
(1/2): docker-ce-stable/x86_64/updateinfo                                                                                                                                                           |   55 B  00:00:00     
(2/2): docker-ce-stable/x86_64/primary_db                                                                                                                                                           |  32 kB  00:00:00     
Available Packages
docker-ce.x86_64                                                                                  17.03.0.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.2.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.06.0.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.2.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.09.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.12.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.1.ce-1.el7.centos                                                                                  docker-ce-stable
docker-ce.x86_64                                                                                  18.06.0.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.2.ce-3.el7                                                                                         docker-ce-stable
docker-ce.x86_64                                                                                  18.06.3.ce-3.el7                                                                                         docker-ce-stable
docker-ce.x86_64                                                                                  3:18.09.0-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.2-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.4-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.6-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.8-3.el7                                                                                          docker-ce-stable
docker-ce.x86_64                                                                                  3:19.03.0-3.el7                                                                                          docker-ce-stable
docker-ce.x86_64                                                                                  3:19.03.1-3.el7                                                                                          docker-ce-stable
[root@centos-amber docker]# 

2.1.3 安装Docker最新版本: sudo yum install docker-ce
docker-ce-cli 或者 yum install docker-io –y

[root@centos-amber docker]#  sudo yum install docker-ce
..............
Installed:
  docker-ce.x86_64 3:19.03.1-3.el7                                                                                           
Dependency Installed:
  audit-libs-python.x86_64 0:2.8.4-4.el7    checkpolicy.x86_64 0:2.5-8.el7            container-selinux.noarch 2:2.107-1.el7_6          containerd.io.x86_64 0:1.2.6-3.3.el7    docker-ce-cli.x86_64 1:19.03.1-3.el7   
  libcgroup.x86_64 0:0.41-20.el7            libsemanage-python.x86_64 0:2.5-14.el7    policycoreutils-python.x86_64 0:2.5-29.el7_6.1    python-IPy.noarch 0:0.75-6.el7          setools-libs.x86_64 0:3.3.8-4.el7   
Complete!
[root@centos-amber docker]# 

2.1.4 启动Docker服务:

# Start docker service
systemctl enable docker 
systemctl start docker

执行:systemctl enable docker 允许开机启动

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

执行:systemctl start docker 启动Docker服务

[root@centos-amber docker]# systemctl start docker
[root@centos-amber docker]#
基本命令:
systemctl enable docker # 允许开机启动
systemctl start docker #运行Docker守护进程
systemctl stop docker #停止Docker守护进程
systemctl restart docker #重启Docker守护进程
systemctl status docker #查看Docker的运行状态

2.1.5 检验安装Docker服务信息:
执行:docker info 查看Docker服务信息

[root@centos-amber docker]# docker info            
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.1
 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: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc version: 425e105d5a03fabd737a126ad93d62a9eeede87f
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-957.27.2.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.795GiB
 Name: centos-amber
 ID: TS7V:P2HE:MICX:JPN7:H5JU:PQX5:JWB3:4YSO:DM7X:RVBV:URME:64DZ
 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
[root@centos-amber docker]# 

执行:docker version 查看版本号

[root@centos-amber docker]# docker version
Client: Docker Engine - Community
 Version:           19.03.1
 API version:       1.40
 Go version:        go1.12.5
 Git commit:        74b1e89
 Built:             Thu Jul 25 21:21:07 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.1
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.5
  Git commit:       74b1e89
  Built:            Thu Jul 25 21:19:36 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.6
  GitCommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  Version:          1.0.0-rc8
  GitCommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
[root@centos-amber docker]# 

3. Docker配置阿里云镜像加速器

Docker 密码:Docker20190822

阿里云官网配置:

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@centos-amber docker]#vim  /etc/docker/daemon.json           
{
  "registry-mirrors": ["https://j98vrcq8.mirror.aliyuncs.com"]
}                                                                           
[root@centos-amber docker]# sudo systemctl daemon-reload 
[root@centos-amber docker]# sudo systemctl restart docker     
[root@centos-amber docker]# 
[root@centos-amber docker]# docker info                  
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.1
 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: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc version: 425e105d5a03fabd737a126ad93d62a9eeede87f
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-957.27.2.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.795GiB
 Name: centos-amber
 ID: TS7V:P2HE:MICX:JPN7:H5JU:PQX5:JWB3:4YSO:DM7X:RVBV:URME:64DZ
 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

[root@centos-amber docker]# 

4.Docker容器基本组件部署与安装

Compose组件部署与安装

基本概述:

Docker Compose是一个用来定义和运行复杂应用的Docker工具。
一个使用Docker容器的应用,通常由多个容器组成。
使用Docker Compose不再需要使用shell脚本来启动容器。 

Compose 通过一个配置文件来管理多个Docker容器,在配置文件中,所有的容器通过services来定义。然后使用docker-compose脚本来启动,停止和重启应用,和应用中的服务以及所有依赖服务的容器,非常适合组合使用多个容器进行开发的场景

安装docker-compose的方式:

  • [1] 从github上下载docker-compose二进制文件安装

下载最新版的docker-compose文件 :
sudo curl -L https://github.com/docker/com...uname -s-uname -m -o /usr/local/bin/docker-compose

[root@centos-amber ~]# sudo curl -L https://github.com/docker/compose/releases/download/1.25.0-rc2/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    640      0 --:--:-- --:--:-- --:--:--   640
  3 16.1M    3  645k    0     0   1669      0  2:48:53  0:06:35  2:42:18     0
curl: (56) TCP connection reset by peer
[root@centos-amber ~]# 

添加可执行权限: sudo chmod +x /usr/local/bin/docker-compose

[root@centos-amber ~]# sudo chmod +x /usr/local/bin/docker-compose
[root@centos-amber ~]# 

卸载docker-compose执行:sudo rm /usr/local/bin/docker-compose

[root@centos-amber ~]# sudo rm /usr/local/bin/docker-compose
[root@centos-amber ~]# 
  • [2] 使用pip install命令安装:

安装方式:sudo pip install docker-compose

sudo pip install docker-compose

卸载方式:pip uninstall docker-compose

pip uninstall docker-compose

2种方式检验安装结果:docker-compose --version

[root@centos-amber ~]# docker-compose --version             
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn't match a supported version!
  RequestsDependencyWarning)
docker-compose version 1.24.1, build 4667896
[root@centos-amber ~]# 
Machine组件部署与安装

基本概述:

Docker Machine是一个工具,可让您在虚拟主机上安装Docker Engine,并使用docker-machine命令管理主机。

安装docker-compose的方式:

curl -L https://github.com/docker/machine/releases/download/v0.16.1/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&   
chmod +x /tmp/docker-machine &&   
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

Docker 与宿主机建立网络通信:

ps1 :docker 端口映射错误

docker: Error response from daemon: driver failed programming external connectivity on endpoint mysql-master (1aac2f09e8d851518f8551308ccbe1e036454d5103bd99fba3f7b3870e3b21b4):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 3307 -j DNAT --to-destination 172.17.0.4:3306 ! -i docker0: iptables: No chain/target/match by that name.
 (exit status 1)).

解决办法:

  • [1] 执行:pkill docker
  • [2] iptables -t nat -F
  • [3] ifconfig docker0 down
  • [4] brctl delbr docker0

pkill docker && iptables -t nat -F && ifconfig docker0 down && brctl delbr docker0

ps2: linux brctl command not found

[root@centos-meteor ~]# brctl delbr docker0
-bash: brctl: command not found

宿主机安装: yum install bridge-utils

4.Docker容器部署与安装总结

docker 配置基于阿里云的Yum源:

  • [1] 安装必要的系统工具 :sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  • [3] 更新并安装 Docker-CE: sudo yum makecache fast sudo yum -y install docker-ce
 sudo yum makecache fast  sudo yum -y install docker-ce
  • [4] 开启Docker服务: systemctl enable docker systemctl start docker
systemctl enable docker  systemctl start docker

docker 部署安装以及基本命令使用:

  • [1] Docker部署要求64位的系统且内核版本至少为3.10
  • [2] Docker安装目录:var/lib/docker 和 /var/lib/docker-engine
  • [3] Docker配置目录:/etc/docker

Docker 部署portainer

docker run -ti -d --name portainer -p 9000:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock  portainer/portainer

版权声明:本文为博主原创文章,遵循相关版权协议,如若转载或者分享请附上原文出处链接和链接来源。

推荐阅读
关注数
0
文章数
9
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息