1. Docker与微服务-使用Maven插件构建Docker镜像
前提:自己搭建的私有Docker仓库需要开放远程API端口,要不然你无法推送镜像到私有仓库下,开放Docker远程私有仓库端口步骤:
Ubuntu系统下:
1. 配置
$ sudo vim /lib/systemd/system/docker.service
修改ExecStart一行,改为:
ExecStart=/usr/bin/docker -H fd:// -H tcp://0.0.0.0:22375
2. 重启Docker服务
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
3. 测试
curl http://localhost:22375/version
Centos系统下:
1. 配置(远程访问。主要是在[Service]这个部分,加上下面两个参数)
[root@ecs-dev-001 ~]# vim /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:22375 -H unix://var/run/docker.sock
2.Docker重新加载配置文件
[root@ecs-dev-001 ~]# systemctl daemon-reload
[root@ecs-dev-001 ~]# systemctl restart docker
[root@ecs-dev-001 ~]# ps -ef|grep docker
root 15373 1 0 17:59 ? 00:00:00 /usr/bin/dockerd -H tcp://0.0.0.0:22375 -H unix://var/run/docker.sock
root 15379 15373 0 17:59 ? 00:00:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
root 15475 15373 0 17:59 ? 00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 25000 -container-ip 172.17.0.2 -container-port 5000
root 15481 15379 0 17:59 ? 00:00:00 docker-containerd-shim d5b158711a40d31135a5227cb8cc9c220e4819d31eecbb527189ae7afa381c1d /var/run/docker/libcontainerd/d5b158711a40d31135a5227cb8cc9c220e4819d31eecbb527189ae7afa381c1d docker-runc
root 15499 15481 0 17:59 ? 00:00:00 registry serve /etc/docker/registry/config.yml
root 15540 15373 0 17:59 ? 00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.17.0.3 -container-port 8080
root 15546 15379 0 17:59 ? 00:00:00 docker-containerd-shim c68ccfee62efc84c4692095043b1151dec53a5ea0b676510c59116133de94975 /var/run/docker/libcontainerd/c68ccfee62efc84c4692095043b1151dec53a5ea0b676510c59116133de94975 docker-runc
root 15618 15207 0 17:59 pts/1 00:00:00 grep --color=auto docker
3. 测试
curl http://localhost:22375/version
注意:如果遇到错误信息 Get
https://192.168.0.11:5000/v1/_ping:
http: server gave HTTP response to HTTPS client
解决办法
在”/etc/docker/“目录下,创建”daemon.json“文件。在文件中写入:
{
"insecure-registries": [
"192.168.0.11:5000"
]
}
//多个私服写法,逗号分隔即可
{
"insecure-registries": [
"192.168.0.12:5000",
"192.168.0.11:5000"
]
}
重启DOCKER
systemctl restart docker
Windows环境配置环境变量 DOCKER_HOST : tcp://192.168.0.11:22375
2.工具
插件名称 | 官方地址 |
---|---|
docker-maven-plugin |
https://github.com/spotify/docker-maven-plugin |
docker-maven-plugin |
https://github.com/fabric8io/docker-maven-plugin |
docker-maven-plugin |
https://github.com/bibryam/docker-maven-plugin |
3.使用插件构建Docker镜像
4.简单使用
使用springbootdemo构建
4.1 pom.xml添加以下配置
<build>
<plugins>
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed-->
<imageName>zhouping/springbootdemo</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
4.2 执行命令
mvn clean package docker:build
docker images --显示镜像列表
启动镜像
docker run -p 8761:8761 microservice-discovery-eureka
5.使用Dockerfile进行构建
5.1 新建Dockerfile文件
FROM registry.cn-hangzhou.aliyuncs.com/acs/maven:3-jdk-8
MAINTAINER zhouping "zhouping19911013@163.com"
MAINTAINER version "online"
EXPOSE 8089
VOLUME /tmp
ADD springboot.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
5.2 说明
FROM registry.cn-hangzhou.aliyuncs.com/acs/maven:3-jdk-8
由于下载国外镜像很慢,添加阿里镜像地址
5.3 修改pom.xml
<build>
<plugins>
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed-->
<imageName>zhouping/springbootdemo</imageName>
<!-- 指定Dockerfile所在的路径 -->
<dockerDirectory>${project.basedir}/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
6.将Docker镜像push到DockerHub上
首先修改Maven的全局配置文件settings.xml,添加以下段落
<servers>
<server>
<id>docker-hub</id>
<username>你的DockerHub用户名</username>
<password>你的DockerHub密码</password>
<configuration>
<email>你的DockerHub邮箱</email>
</configuration>
</server>
</servers>
项目pom.xml修改为如下:注意imageName的路径要和repo的路径一致
<build>
<plugins>
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository
name ... only [a-z0-9-_.] are allowed -->
<!-- 如果要将docker镜像push到DockerHub上去的话,这边的路径要和repo路径一致 -->
<imageName>zping/test</imageName>
<!-- 指定Dockerfile所在的路径 -->
<dockerDirectory>${project.basedir}/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 以下两行是为了docker push到DockerHub使用的。 -->
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
</configuration>
</plugin>
</plugins>
</build>
执行命令
mvn clean package docker:build -DpushImage
7.将镜像push到私有仓库
修改maven全局配置文件settings.xml
<servers>
<server>
<id>docker-registry</id>
<username>你的DockerHub用户名</username>
<password>你的DockerHub密码</password>
<configuration>
<email>你的DockerHub邮箱</email>
</configuration>
</server>
</servers>
修改pom文件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 路径为:私有仓库地址/你想要的镜像路径 -->
<imageName>zhouping/springbootdemo</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 与maven配置文件settings.xml一致 -->
<serverId>docker-registry</serverId>
</configuration>
</plugin>
执行
mvn clean package docker:build -DpushImage
如果想要从私服上下载该镜像,执行:
docker login reg.itmuch.com # 然后输入账号和密码
docker pull reg.itmuch.com/test-pull-registry
8.将插件绑定在某个phase执行
只需要将插件的goal 绑定在某个phase即可,所谓的phase和goal,可以这样理解:maven命令格式是:mvn phase:goal ,例如mvn package docker:build 那么,package 和 docker 都是phase,build 则是goal
修改pom.xml文件
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
如上,我们只需添加
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
9.常见异常
连接不上2375一般在Win7上出现
Connect to localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect -> [Help 1]
解决步骤:
docker-machine env
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.100:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\Administrator\.docker\machine\machines\default
pom.xml文件中为插件添加配置
<!-- 解决Connect to localhost:2375的问题的其中一种方式,注意要跟docker-machine env相一致 -->
<dockerHost>https://192.168.99.100:2376</dockerHost>
<dockerCertPath>C:\Users\Administrator\.docker\machine\machines\default</dockerCertPath>
修改后pom.xml
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<imageName>eacdy/test</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<!-- 解决Connect to localhost:2375的问题的其中一种方式,注意要跟docker-machine env相一致 -->
<dockerHost>https://192.168.99.100:2376</dockerHost>
<dockerCertPath>C:\Users\Administrator\.docker\machine\machines\default</dockerCertPath>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 以下两行是为了docker push到DockerHub使用的。 -->
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
</configuration>
</plugin>
将maven打包后的镜像PUSH到远程私有仓库,需要配置系统变量,否则无法PUSH成功,配置完成后需要重启系统生效
DOCKER_HOST = tcp://139.159.158.113:22375
11.TIPS
1. imageName必须符合正则[a-z0-9-_.],否则将会构建失败
2. 插件默认使用localhost:2375去连接Docker,如果你的Docker端口不是2375,需要配置环境变量DOCKER_HOST=tcp://:2375
12.代码地址任选其一
https://github.com/thestar111/springbootdemo.git
13.参考文档
1.
http://developer.51cto.com/art/201404/434879.html
2.
https://linux.cn/article-6131-rss.html
14. 国内镜像仓库加速地址
1.
https://eo17c4cg.mirror.aliyuncs.com
2.
http://ab971fb4.m.daocloud.io
Docker Toolbox
docker-machine ssh default
sudo sed -i "s|EXTRA_ARGS='|EXTRA_ARGS='--registry-mirror=加速地址 |g" /var/lib/boot2docker/profile
exit
docker-machine restart default
Docker for Windows
在桌面右下角状态栏中右键 docker 图标,修改在 Docker Daemon 标签页中的 json ,把 加速地址(在加速器页面获取)加到"registry-mirrors"的数组里。点击 Apply 使设置生效
版权声明:本文为u012397222原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。