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

1.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]# 
[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/compose/releases/download/1.25.0-rc2/docker-compose-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

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容