Docker服务中进程间通信通过/var/run/docker.sock实现,默认服务不提供监听端口,因此使用docker remote api 需要手动绑定端口。

在centos7.2下,可以进行这样的操作:
直接在ExecStart=/usr/bin/dockerd后面增加需要的启动参数即可。我这里增加了DockerHub镜像加速地址和开启了tcp连接(--registry-mirror=https://xxoo.mirror.acs.aliyun.com -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock)
vim /etc/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

systemctl daemon-reload
systemctl restart docker

实际操作:

[root@localhost frinder]# vi /usr/lib/systemd/system/docker.service
在:ExecStart=/usr/bin/dockerd-current 后面追加:-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

[root@localhost frinder]# cat /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target
Wants=docker-storage-setup.service
Requires=docker-cleanup.timer

[Service]
Type=notify
NotifyAccess=all
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
Environment=DOCKER_HTTP_HOST_COMPAT=1
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock \
--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 \
$OPTIONS \
$DOCKER_STORAGE_OPTIONS \
$DOCKER_NETWORK_OPTIONS \
$ADD_REGISTRY \
$BLOCK_REGISTRY \
$INSECURE_REGISTRY
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
Restart=on-abnormal
MountFlags=slave

[Install]
WantedBy=multi-user.target
[root@localhost frinder]# systemctl daemon-reload
[root@localhost frinder]# systemctl restart docker.service

客户端(idea)连接:

docker服务端设置好端口后,客户端配置 API URL为:服务器host+port,如,服务端ip:192.168.98.128,端口:2376,则配置为:http://192.168.98.128:2376

项目配置:

pom.xml中添加支持:
<docker.image.prefix>spring.io</docker.image.prefix>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.11</version>
<configuration>
<dockerHost>http://192.168.98.128:2376</dockerHost>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>

在项目 main目录下添加目录 docker,添加文件 Dockerfile,内容如下:
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD docker.app-1.0-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

设置 Artifacts:

注意点:
Output directory 设置,设置为 docker,即 Dockerfile同目录
jar包名称一定要与 Dockerfile中的一致,不然会报找不到文件的错误

推送到 docker:
clean package docker:build -DpushImage

push过程中会报错,忽略之,docker中是可以启动成功的!错误信息如下:

......
[WARNING] Failed to push spring.io/docker.app, retrying in 10 seconds (5/5).
[INFO] Pushing spring.io/docker.app
The push refers to a repository [spring.io/docker.app]
b5e9a3e6075a: Preparing
08ba4e16adba: Preparing
162f935b1198: Preparing
dcf909146faa: Preparing
23b9c7b43573: Preparing
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:46 min
[INFO] Finished at: 2017-04-11T16:37:15+08:00
[INFO] Final Memory: 31M/75M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.4.11:build (default-cli) on project docker.app: Exception caught: Error: Status 405 trying to push repository docker.app: "" -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Process finished with exit code 1

检查镜像,发现 spring.io/docker.app 已经创建,启动 spring.boot项目 docker run -p 8080:8080 -t spring.io/docker.app 可以正常启动!

[root@localhost frinder]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
spring.io/docker.app latest 93ab985f7835 18 minutes ago 195.3 MB
docker.io/centos latest a8493f5f50ff 4 days ago 192.5 MB
docker.io/frolvlad/alpine-oraclejdk8 slim 00d8610f052e 5 weeks ago 166.6 MB
[root@localhost frinder]# docker run -p 8080:8080 -t spring.io/docker.app

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)

2017-04-11 08:43:59.100 INFO 6 --- [ main] com.s.b.d.App : Starting App v1.0-SNAPSHOT on 07a7a077770d with PID 6 (/app.jar started by root in /)
2017-04-11 08:43:59.109 INFO 6 --- [ main] com.s.b.d.App : No active profile set, falling back to default profiles: default
2017-04-11 08:43:59.283 INFO 6 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b9af9a9: startup date [Tue Apr 11 08:43:59 GMT 2017]; root of context hierarchy
2017-04-11 08:44:01.873 INFO 6 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-04-11 08:44:01.905 INFO 6 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-04-11 08:44:01.908 INFO 6 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-04-11 08:44:02.097 INFO 6 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-04-11 08:44:02.098 INFO 6 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2826 ms
2017-04-11 08:44:02.322 INFO 6 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-04-11 08:44:02.334 INFO 6 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-04-11 08:44:02.336 INFO 6 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-04-11 08:44:02.336 INFO 6 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-04-11 08:44:02.336 INFO 6 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-04-11 08:44:02.822 INFO 6 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b9af9a9: startup date [Tue Apr 11 08:43:59 GMT 2017]; root of context hierarchy
2017-04-11 08:44:03.006 INFO 6 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-04-11 08:44:03.008 INFO 6 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-04-11 08:44:03.066 INFO 6 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-11 08:44:03.066 INFO 6 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-11 08:44:03.150 INFO 6 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-11 08:44:03.191 INFO 6 --- [ main] oConfiguration$WelcomePageHandlerMapping : Adding welcome page: class path resource [templates/index.html]
2017-04-11 08:44:03.411 INFO 6 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-04-11 08:44:03.506 INFO 6 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-04-11 08:44:03.518 INFO 6 --- [ main] com.s.b.d.App : Started App in 5.108 seconds (JVM running for 5.93)

已经可以访问了!

spring boot docker 初尝试的更多相关文章

  1. 自定义spring boot starter 初尝试

    自定义简单spring boot starter 步骤 从几篇博客中了解了如何自定义starter,大概分为以下几个步骤: 1 引入相关依赖: 2 生成属性配置类: 3 生成核心服务类: 4 生成自动 ...

  2. Spring Boot Docker 实战

    Spring Boot Docker 开发环境 开发工具: Intellij IDEA 2018.2.6 springboot: 2.0.6.RELEASE jdk: 1.8.0_192 maven: ...

  3. Spring boot缓存初体验

    spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...

  4. Spring Boot + Docker + K8S 简单示例

    前言 最近看了看k8s,感觉用这个管理docker确实比自己写一坨脚本进步太多了,简直不是一个次原的东西. 看着k8s的官方文档随手写了个小Demo,一个基于k8s的spring boot服务. 代码 ...

  5. Spring Boot + Redis 初体验

    本文测试环境: Spring Boot 2.1.4.RELEASE + Redis 5.0.4 + CentOS 7 让程序先 run 起来 安装及配置 Redis 参考: How To Instal ...

  6. Spring Boot Docker

    1.  IDEA中配置Docker Docker默认只接受本地客户端的请求,为了能够远程访问它,首先要开放Docker的监听端口,运行外部应用可以访问 修改 /lib/systemd/system/d ...

  7. idea spring boot docker 多项目 maven 编译

    1,重复的model [INFO] Scanning for projects... [ERROR] [ERROR] Some problems were encountered while proc ...

  8. Spring Boot + docker +mongo

    启动mongo镜像 docker run --name mongo-container -d -P mongo 连接到容器内 docker exec -it eb sh 输入:mongo 输入:sho ...

  9. java Spring boot Docker打包

    https://blog.csdn.net/Stephanie_1/article/details/88831993

随机推荐

  1. 判断是否有TrueType字体

    function IsTrueTypeAvailable : bool;var {$IFDEF WIN32}  rs : TRasterizerStatus; {$ELSE}  rs : TRaste ...

  2. 进程间通信-Queue

    进程间通信-Queue Process之间有时需要通信,操作系统提供了很多机制来实现进程间的通信. 1. Queue的使用 可以使用multiprocessing模块的Queue实现多进程之间的数据传 ...

  3. hive 显示分区

    显示某一张表的分区值 show partitions table_name;

  4. Linux初学时的一些常用命令(3)

    管道 |  重要的一个概念,其作用是将一个命令的输出用作另一个命令的输入 例如:  在ifconfig的结果里查找 192.168字符串  ifconfig | grep 192.168 查找和jav ...

  5. HTML5 画图--文字

    1:html <div style="margin:0 auto;width:794px;height:1123px"> <canvas id="myC ...

  6. Zookeeper—学习笔记(一)

    1.Zookeeper基本功能 (增 删 改 查:注册,监听) 两点: 1.放数据(少量). 2.监听节点.  注意: Zookeeper中的数据不同于数据库中的数据,没有表,没有记录,没有字段: Z ...

  7. 元素的定位tag_name,link_text,class_name

    tag_name 就是根据HTML的标签的名称来定位的: 案例:打开我要自学网,会有用户名和密码的输入框 例如:拿51zxw.net为例 from time import sleep #加载浏览器驱动 ...

  8. 如何使用eclipse搭建maven环境以及常见的错误

    这篇博客适合零基础学习maven,搭建maven以及运行项目,常见的错误,我会在结尾写出说明白,看是否和大家的错误一样,或者文章的括号注释部分也会写出一些注意点. 第一步:就是下载maven,以及配置 ...

  9. C++ auto

    auto用来声明自动变量.它是存储类型标识符,表明变量(自动)具有本地范围.块范围的变量声明(如for循环体内的变量声明)默认为auto存储类型. 好处:auto变量在离开作用域是会变程序自动释放,不 ...

  10. 非换行空白:non-breaking space

    一 维基百科(英文版)词条 In word processing and digital typesetting, a non-breaking space (" ") (also ...