16、CentOS 简单搭建Docker私有仓库服务
安装服务
# yum install docker -y
# yum install epel-release -y
# yum install python-pip -y
# pip install docker-compose
查看安装完成的Docker和Compose的版本
# docker version
# pip freeze | grep compose
启动docker服务,并设置为开机自启动
# systemctl enable docker 开机启动
# systemctl start docker 启动Docker服务
【注意】在启动的时候我遇到了一个问题而导致启动失败,下面将针对问题做下简单描述,如果电脑前的你也遇到了同样的问题,非常荣幸地顺便为您解决一个烦恼
问题描述:
[[email protected] lib]# systemctl start docker
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
[[email protected] lib]# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2018-06-22 15:22:45 CST; 10s ago
Docs: http://docs.docker.com
Process: 6374 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES (code=exited, status=1/FAILURE)
Main PID: 6374 (code=exited, status=1/FAILURE)
Jun 22 15:22:42 registry.sefon.com systemd[1]: Starting Docker Application Container Engine...
Jun 22 15:22:42 registry.sefon.com dockerd-current[6374]: time="2018-06-22T15:22:42.987932115+08:00" level=info msg="libcontainerd: new containerd process, pid: 6381"
Jun 22 15:22:45 registry.sefon.com dockerd-current[6374]: Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kernel. Either boot into a newer kernel or disabl...nabled=false) #关键报错信息
Jun 22 15:22:45 registry.sefon.com systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
Jun 22 15:22:45 registry.sefon.com systemd[1]: Failed to start Docker Application Container Engine.
Jun 22 15:22:45 registry.sefon.com systemd[1]: Unit docker.service entered failed state.
Jun 22 15:22:45 registry.sefon.com systemd[1]: docker.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
原因分析:
根据报错信息“Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kernel. Either boot into a newer kernel or disabl...nabled=false)”的提示,这台机器的linux的内核中的SELinux不支持 overlay2 graph driver 。
解决方法就在docker配置文件里面里禁用selinux,--selinux-enabled=false
解决方法:
# vi /etc/sysconfig/docker
打开并编辑配置文件,请看红色加粗字体
# /etc/sysconfig/docker
# Modify these options if you want to change the way the docker daemon runs
#OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false --registry-mirror=https://fzhifedh.mirror.aliyuncs.com --insecure-registry=registry.sese.com' #修改这里的"--selinux-enabled",改成"--selinux-enabled=false"
if [ -z "${DOCKER_CERT_PATH}" ]; then
DOCKER_CERT_PATH=/etc/docker
fi
...... #配置文件后面的内容省略
保存
启动Docker服务
# systemctl start docker 启动Docker
# systemctl status docker 查看服务状态
--------------------------------------------------------------------------------------------------------------------------------------
继续 Docker私有仓库搭建工作
下载仓库镜像
# docker pull registry
# docker images 查看镜像是否下载成功
运行仓库镜像
# docker run -itd -v /data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry docker.io/registry:latest
参数描述:
-itd:在容器中打开一个伪终端进行交互操作,并在后台运行;
-v:把宿主机的/data/registry目录绑定 到 容器/var/lib/registry目录(这个目录是registry容器中存放镜像文件的目录),来实现数据的持久化;
-p:映射端口;访问宿主机的5000端口就访问到registry容器的服务了;
--restart=always:这是重启的策略,假如这个容器异常退出会自动重启容器;
--name registry:创建容器命名为registry,你可以随便命名;
docker.io/registry:latest:这个是刚才pull下来的镜像;
查看私有仓库中所有镜像
# curl http://192.168.186.130:5000/v2/_catalog
{"repositories":[]}
修改镜像源
# vi /etc/docker/daemon.json
加入下面一段代码
{
"registry-mirrors": [ "https://registry.docker-cn.com"],
"insecure-registries": [ "192.168.186.130:5000"]
}
【说明】
192.168.186.130 为我本地测试虚拟机IP地址,需要改动为真实的服务器地址
insecure-registries 解决的是HTTPS访问拦截,如果不在这里添加,上传镜像至私有仓库的时候就会报后面的异常信息Get https://172.18.18.90:5000/v2/: http: server gave HTTP response to HTTPS client
重启Docker服务
# systemctl restart docker
以busybox镜像为例,下拉后上传至私有仓库,实际操作如下
# docker pull busybox
# docker images 查看镜像是否下载完成
为镜像打标签
# docker tag busybox:latest 192.168.186.130:5000/busybox:v1
上传镜像至私有仓库
# docker push 192.168.186.130:5000/busybox:v1
再次查看私有仓库中所有镜像
# curl http://192.168.186.130:5000/v2/_catalog
比之前多了一个busybox镜像,说明我们上传成功
列出该镜像所有标签Tag列表
# curl http://192.168.186.130:5000/v2/busybox/tags/list
在这里我们可以看到之前为镜像打过的一个标签
--------------------------------------------------------------------------------------------------------------------------------------
关于docker的一点命令,记一下,自己记性不好,没办法
1、删除所有镜像
# docker rmi -f $(docker images -aq) 切记正在运行的无法删除
2、maven项目打包docker
# mvn clean package docker:build
3、运行镜像
# docker run -p 端口号:端口号 -d 镜像名称
4、显示正在运行的日志
# docker logs -f -t --tail 100 容器ID
5、列出docker容器
# docker ps [option]
-
-a :显示所有的容器,包括未运行的。
-
-f :根据条件过滤显示的内容。
-
--format :指定返回值的模板文件。
-
-l :显示最近创建的容器。
-
-n :列出最近创建的n个容器。
-
--no-trunc :不截断输出。
-
-q :静默模式,只显示容器编号。
-
-s :显示总的文件大小。
至此,打完收工!!!