返回顶部

Centos7.6系统下docker的安装

一、环境说明

 系统:CentOS7.6

 软件:Docker19.03

二、Docker的安装

2.1、在线安装

(1) 设置仓库,安装所需的软件包。

 yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

[root@docker ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@docker ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

(2) 安装依赖包 container-selinux

[root@docker ~]# wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.119.1-1.c57a6f9.el7.noarch.rpm
[root@docker ~]# yum install -y ./container-selinux-2.119.1-1.c57a6f9.el7.noarch.rpm

(3) 安装Docker Engine-Community和containerd

[root@docker ~]# yum install -y docker-ce docker-ce-cli containerd.io

(4) 检查docker是否安装成功

[root@docker ~]# systemctl start docker
[root@docker ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:45:28 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

2.2、离线安装

 使用离线yum安装的方式,需提前下载好相应的rpm包,这里以版本dockers-19.0.3.4为例。

(1) 下载docker-ce、docker-ce-cli、containerd.io安装包

 下载地址:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

相关包名:
docker-ce-19.03.4-3.el7.x86_64.rpm
docker-ce-cli-19.03.4-3.el7.x86_64.rpm
containerd.io-1.2.6-3.3.el7.x86_64.rpm

(2) 下载container-selinux安装包

 下载地址:https://pkgs.org/download/container-selinux

相关包名:container-selinux-2.107-3.el7.noarch.rpm

(3) 下载相关依赖包

 下载地址:https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/

相关包名:
audit-2.8.5-4.el7.x86_64.rpm
audit-libs-2.8.5-4.el7.x86_64.rpm
audit-libs-python-2.8.5-4.el7.x86_64.rpm
checkpolicy-2.5-8.el7.x86_64.rpm
libcgroup-0.41-21.el7.x86_64.rpm
libseccomp-2.3.1-4.el7.x86_64.rpm
libsemanage-python-2.5-14.el7.x86_64.rpm
libtool-ltdl-2.4.2-22.el7_3.x86_64.rpm
policycoreutils-2.5-34.el7.x86_64.rpm
policycoreutils-python-2.5-34.el7.x86_64.rpm
python-IPy-0.75-6.el7.noarch.rpm
setools-libs-3.3.8-4.el7.x86_64.rpm

 下载地址:http://mirror.centos.org/centos/7/extras/x86_64/Packages

相关包名:pigz-2.3.3-1.el7.centos.x86_64.rpm

(4) 安装

 将以上包放在一个文件夹里,如install_docker,然后进行安装即可。

yum install install_docker/*.rpm

(5) 启动

systemctl start docker
systemctl stop docker

(6) 安装docker-compose (非必需)

 首先访问 docker-compose 的 GitHub 版本发布页面:https://github.com/docker/compose/releases ,下载 Linux 版本的 docker-compose。

[root@localhost ~]# ll docker-compose-Linux-x86_64 
-rw-r--r-- 1 root root 12255808 Aug 13 14:14 docker-compose-Linux-x86_64

 再将文件移至/usr/local/bin/目录下,并加执行权限即可。

[root@localhost ~]# mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose 
[root@localhost ~]# docker-compose -version
docker-compose version 1.26.1, build f216ddbf

三、相关操作

3.1、镜像加速

[root@localhost ~]# vim /etc/docker/daemon.json 
{"registry-mirrors":["http://hub-mirror.c.163.com/"]}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker

3.2、镜像操作

#拉取镜像
docker pull redis:latest
#镜像导出
docker save redis:latest > redis.laster.tar
#镜像导入
docker load < redis.laster.tar
#镜像重命名
docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签)

3.3、docker私有仓库搭建

3.3.1、安装Docker私有仓库

(1) 拉取镜像并创建镜像保存目录

[root@localhost ~]# docker pull registry

[root@localhost ~]# mkdir -pv /usr/local/docker-registry/images

(2) 启动容器

[root@localhost ~]# docker run -d -p 5000:5000 -v /usr/local/docker-registry/images:/var/lib/registry --name=registry registry:latest

(3) 查询是否搭建成功

 浏览器输入:http://宿主机IP:5000/v2/_catalog,看到 {“repositories”:[]} 表示私有仓库搭建成功

(4) 修改/etc/docker/daemon.json

[root@localhost ~]# vim /etc/docker/daemon.json 
#添加以下行,此步用于让docker信任私有仓库地址
{"insecure-registries":["宿主机ip:5000"]}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker 
#启动私有仓库容器
[root@localhost ~]# docker start registry

(5) 为本地镜像添加标签,并将其归入本地仓库

#本地镜像
[root@localhost ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
wurstmeister/kafka          latest              40094a582680        3 weeks ago         435MB
zookeeper                   latest              6ad6cb039dfa        4 weeks ago         252MB
registry                    latest              2d4f4b5309b1        2 months ago        26.2MB
sheepkiller/kafka-manager   latest              4e4a8c5dabab        2 years ago         463MB
#给本地镜像添加标签
[root@localhost ~]# docker tag zookeeper:latest 192.168.145.7:5000/zookeeper:3.6.1
#将被标记的本地镜像push到仓库
[root@localhost ~]# docker push 192.168.145.7:5000/zookeeper:3.6.1
#推送成功后,查看私有仓库的镜像信息
[root@localhost ~]# curl -X GET http://192.168.145.7:5000/v2/_catalog
{"repositories":["zookeeper"]}

(6) 其它服务器拉取私有仓库镜像

#先修改此文件
[root@localhost ~]# vim /etc/docker/daemon.json 
{"insecure-registries":["192.168.145.7:5000"]}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
#再执行拉取操作
[root@localhost ~]# docker pull 192.168.145.7:5000/zookeeper:3.6.1
3.6.1: Pulling from zookeeper
Digest: sha256:600ca1b49934168ada239f934a2e2ec1e1e63c8a1ae845ea7072b2ec277caeac
Status: Downloaded newer image for 192.168.145.7:5000/zookeeper:3.6.1

 3.3.2、设置私有仓库的用户认证(可选)

(1) 创建存储用户密码的目录与文件

[root@localhost ~]# mkdir -pv /usr/local/docker-registry/auth

[root@localhost ~]# htpasswd -Bbn user 123456 > /usr/local/docker-registry/auth/htpasswd
#如果没有htpasswd命令,则需要安装相关工具包:yum install -y httpd-tools
#或使用registry容器生成密码文件,但使用registry最新的(2.7)的镜像此条命令会存在问题,此处使用的是2.6版本的
# docker run --entrypoint htpasswd registry:2.6 -Bbn user 123456 > /usr/local/soft/docker-registry/auth/htpasswd 

(2) 启动容器

[root@localhost ~]# docker run -d -p 5000:5000 --name=registry_docker \
--restart=always \
-e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
-v /usr/local/docker-registry/auth:/auth \
-v /usr/local/docker-registry/images:/var/lib/registry \
registry:2.7.1

(3) 登录

[root@localhost ~]# docker login -u user -p 123456 192.168.145.7:5000

 

posted @ 2020-09-08 09:14  hovin  阅读(1503)  评论(0编辑  收藏  举报