Docker Java API 开发

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

因为工作原因需要使用Java调用装有docker环境的机器完成打镜像 上传镜像库的操作,进过调查,发现了两个比较常用的Java API工具,分别是 docker-java 和 spotify-docker-client,Github地址分别如下:

docker-java:https://github.com/docker-java/docker-java

spotify-docker-client:https://github.com/spotify/docker-client

两者做个简单的对比:

  Star/Fork(2018/12/19) 文档丰富度 API功能覆盖度 使用复杂度
docker-java 1188/638 相对完善 基本功能 简单
spotify-docker-client 1101/450 相对完善 功能实现较多 相对复杂

 

1.升级docker版本(可选)

#删除旧的版本的docker
yum remove docker  docker-common docker-selinux docker-engine
yum erase docker docker-common docker-client docker-compose

#设置docker yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

#update
yum update

#要先安装docker-ce-selinux-17.03.2.ce,否则安装docker-ce会报错
yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm 

#以查看所有仓库中所有docker版本,并选择特定版本安装
yum list docker-ce --showduplicates | sort -r

#由于repo中默认只开启stable仓库,故这里安装的是最新稳18.03.0.ce-1.el7.centos
yum install docker-ce
#安装指定的版本 例如: yum install docker-ce-17.12.0.ce-1.el7.centos
sudo yum install 

#启动
systemctl start docker

#查看版本
docker version

 

2. 开启docker远程访问

默认情况下,Docker守护进程Unix socket(/var/run/docker.sock)来进行本地进程通信,而不会监听任何端口,因此只能在本地使用docker客户端或者使用Docker API进行操作。如果想在其他主机上操作Docker主机,就需要让Docker守护进程打开一个HTTP Socket,这样才能实现远程通信。

方法一:

vi /lib/systemd/system/docker.service

找到Execstart=/usr/bin/dockerd后加上-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock  退出并且保存

方法二:

Centos 7.X docker-ce:

修改/etc/sysconfig/docker文件,在最后增加一行DOCKER_OPTS

# docker-latest daemon can be used by starting the docker-latest unitfile.  
# To use docker-latest client, uncomment below lines  
#DOCKERBINARY=/usr/bin/docker-latest  
#DOCKERDBINARY=/usr/bin/dockerd-latest  
#DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest  
#DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest  
DOCKER_OPTS="-H unix:///var/run/docker.sock -H 0.0.0.0:2375"

方法一二都需要使配置生效:

#docker.service changed on disk. Run 'systemctl daemon-reload' to reload units.
systemctl daemon-reload  
systemctl restart docker.service  

Centos 6.x docker 1.X.X

$ sudo vi /etc/sysconfig/docker
  other_args="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock"
$ sudo service docker restart

# centos docker的其它操作方式
$ sudo service docker start
$ sudo service docker stop

$ /bin/systemctl start  docker.service

 

 

3.引入spotify-docker-client依赖(以标准maven工程为例)

 

 

 

错误分析:

1.systemctl start docker 报错,内容:

Dec 19 19:13:09 VM_0_4_centos dockerd[13361]: time="2018-12-19T19:13:09.334930508+08:00" level=warning msg="[!] DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHA
Dec 19 19:13:09 VM_0_4_centos dockerd[13361]: time="2018-12-19T19:13:09.339318789+08:00" level=info msg="libcontainerd: new containerd process, pid: 13364"
Dec 19 19:13:10 VM_0_4_centos dockerd[13361]: time="2018-12-19T19:13:10.343022632+08:00" level=error msg="[graphdriver] prior storage driver overlay2 failed: driver not supported"
Dec 19 19:13:10 VM_0_4_centos dockerd[13361]: Error starting daemon: error initializing graphdriver: driver not supported
Dec 19 19:13:10 VM_0_4_centos systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
Dec 19 19:13:10 VM_0_4_centos systemd[1]: Failed to start Docker Application Container Engine.
-- Subject: Unit docker.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit docker.service has failed.
-- 
-- The result is failed.

原因:/var/lib/docker 目录有旧的container 文件,版本变更后需要删除

解决办法:

mv /var/lib/docker /var/lib/docker.old  
service docker start
rm -rf /var/lib/docker.old

 

转载于:https://my.oschina.net/mrpei123/blog/2990496

你可能感兴趣的:(Docker Java API 开发)