欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

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

程序员文章站 2022-05-29 09:47:28
1.Docker容器概述 区分Docker容器技术和VM虚拟机技术: evernotecid://394EFE90 9CE0 4D65 A8CD DFEC0DC8061E/appyinxiangcom/11359837/ENResource/p1872 Docker容器技术 [Container]: ......

1.docker容器概述

  • 区分docker容器技术和vm虚拟机技术:
    evernotecid://394efe90-9ce0-4d65-a8cd-dfec0dc8061e/appyinxiangcom/11359837/enresource/p1872Linux环境基于CentOS7 搭建部署Docker容器
    docker容器技术 [container]:容器虚拟化技术
    evernotecid://394efe90-9ce0-4d65-a8cd-dfec0dc8061e/appyinxiangcom/11359837/enresource/p1873Linux环境基于CentOS7 搭建部署Docker容器
    vm虚拟机技术[virtual machine]:传统虚拟化技术
    evernotecid://394efe90-9ce0-4d65-a8cd-dfec0dc8061e/appyinxiangcom/11359837/enresource/p1873Linux环境基于CentOS7 搭建部署Docker容器
    主要区别是:
    • [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/p1874Linux环境基于CentOS7 搭建部署Docker容器
    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/docker-ce/linux/centos/docker-ce.repo
[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
  • [2] 添加软件源信息:sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
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

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