Springboot整合Dockerfile
创建springboot项目
新建maven项目
maven配置springboot依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ohb.springboot</groupId>
<artifactId>springboot-dockerfile</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.lombok.version>1.18.24</project.lombok.version>
</properties>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.6.5</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${project.lombok.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--定义springboot的maven打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建springboot项目代码
-
springboot项目结构
-
启动类
package com.ohb.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class); } }
-
dto
package com.ohb.springboot.dto; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @AllArgsConstructor @NoArgsConstructor public class HttpResp { private int code; private String msg; private Object results; private Date date; }
-
controller
package com.ohb.springboot.controller; import com.ohb.springboot.dto.HttpResp; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController @RequestMapping("docker") public class DockerController { @GetMapping("/hello") public HttpResp hello() { return new HttpResp(2001, "success", "使用dockerfile构建springboot项目", new Date()); } }
-
资源(resource)
配置文件confg/ application-dev.yml
server: port: 8990
配置文件config/application.yml
spring: profiles: active: dev
静态资源 static/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>springboot-Dockerfile</title> </head> <body> <h1>springboot整合dockerfile文件</h1> <a href="docker/hello">访问java后台</a> <img src="https://logos-world.net/wp-content/uploads/2021/02/Docker-Logo.png"/> </body> </html>
运行项目
打包springboot项目
idea配置docker
配置docker remote api
配置docker.service 让linux开发Docker Remote API。
-
查询配置文件位置
systemctl show --property=FragmentPath docker
-
使用vim编辑此文件
[root@localhost ~]# vim /usr/lib/systemd/system/docker.service
修改语句
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375
- 重载配置文件
[root@localhost ~]# systemctl daemon-reload
-
重启docker
[root@localhost ~]# systemctl restart docker
-
测试2375端口
[root@localhost ~]# netstat -nplt|grep 2375 tcp6 0 0 :::2375 :::* LISTEN 30524/dockerd
-
linux开发2375端口
[root@localhost ~]# firewall-cmd --zone=public --list-ports 8000/tcp 445/tcp 8848/tcp 8080/tcp [root@localhost ~]# firewall-cmd --zone=public --add-port=2375/tcp --permanent success [root@localhost ~]# firewall-cmd --reload success [root@localhost ~]# firewall-cmd --zone=public --list-ports 8000/tcp 445/tcp 8848/tcp 8080/tcp 2375/tcp
-
测试配置
curl localhost:2375/info
[root@localhost ~]# curl localhost:2375/info {"ID":"JWCS:2OUE:KEOL:OW3A:ABKR:AJYV:G3PO:PUQT:6HS2:EBMP:N3KQ:PE5S","Containers":1,"ContainersRunning":0,"ContainersPaused":0,"ContainersStopped":1,"Images":2,"Driver":"overlay2","DriverStatus":[["Backing Filesystem","xfs"],["Supports d_type","true"],["Native Overlay Diff","true"],["userxattr","false"]],"Plugins":{"Volume":["local"],"Network":["bridge","host","ipvlan","macvlan","null","overlay"],"Authorization":null,"Log":["awslogs","fluentd","gcplogs","gelf","journald","json-file","local","logentries","splunk","syslog"]},"MemoryLimit":true,"SwapLimit":true,"KernelMemory":true,"KernelMemoryTCP":true,"CpuCfsPeriod":true,"CpuCfsQuota":true,"CPUShares":true,"CPUSet":true,"PidsLimit":true,"IPv4Forwarding":true,"BridgeNfIptables":true,"BridgeNfIp6tables":true,"Debug":false,"NFd":27,"OomKillDisable":true,"NGoroutines":37,"SystemTime":"2022-12-20T20:55:10.32641512+08:00","LoggingDriver":"json-file","CgroupDriver":"cgroupfs","CgroupVersion":"1","NEventsListener":0,"KernelVersion":"3.10.0-1160.76.1.el7.x86_64","OperatingSystem":"CentOS Linux 7 (Core)","OSVersion":"7","OSType":"linux","Architecture":"x86_64","IndexServerAddress":"https://index.docker.io/v1/","RegistryConfig":{"AllowNondistributableArtifactsCIDRs":[],"AllowNondistributableArtifactsHostnames":[],"InsecureRegistryCIDRs":["127.0.0.0/8"],"IndexConfigs":{"docker.io":{"Name":"docker.io","Mirrors":["https://ung2thfc.mirror.aliyuncs.com/"],"Secure":true,"Official":true}},"Mirrors":["https://ung2thfc.mirror.aliyuncs.com/"]},"NCPU":4,"MemTotal":15540805632,"GenericResources":null,"DockerRootDir":"/var/lib/docker","HttpProxy":"","HttpsProxy":"","NoProxy":"","Name":"localhost.localdomain","Labels":[],"ExperimentalBuild":false,"ServerVersion":"20.10.21","Runtimes":{"io.containerd.runc.v2":{"path":"runc"},"io.containerd.runtime.v1.linux":{"path":"runc"},"runc":{"path":"runc"}},"DefaultRuntime":"runc","Swarm":{"NodeID":"","NodeAddr":"","LocalNodeState":"inactive","ControlAvailable":false,"Error":"","RemoteManagers":null},"LiveRestoreEnabled":false,"Isolation":"","InitBinary":"docker-init","ContainerdCommit":{"ID":"a05d175400b1145e5e6a735a6710579d181e7fb0","Expected":"a05d175400b1145e5e6a735a6710579d181e7fb0"},"RuncCommit":{"ID":"v1.1.4-0-g5fd4c4d","Expected":"v1.1.4-0-g5fd4c4d"},"InitCommit":{"ID":"de40ad0","Expected":"de40ad0"},"SecurityOptions":["name=seccomp,profile=default"],"Warnings":["WARNING: API is accessible on http://0.0.0.0:2375 without encryption.\n Access to the remote API is equivalent to root access on the host. Refer\n to the 'Docker daemon attack surface' section in the documentation for\n more information: https://docs.docker.com/go/attack-surface/"]}
配置idea docker插件
-
打开settings–>docker
-
配置docker插件
在Services中会出现Docker选项。
选中Docker图标,鼠标左键双击会显示容器中的内容。
项目中创建Dockerfile
-
在项目下创建Dockerfile文件
注意: 文件名第一个字母必须大写D,位置在项目下(与src同级)
-
编辑Dockerfile文件内容
FROM java:8 ADD target/springboot-dockerfile-1.0-SNAPSHOT.jar /app.jar ENTRYPOINT ["java","-jar","/app.jar"] EXPOSE 8990
-
配置Dockerfile
Deploying 'springboot-dockerfile-demo Dockerfile: Dockerfile'… Building image… Preparing build context archive… [==================================================>]31/31 files Done Sending build context to Docker daemon… [==================================================>] 17.60MB Done Step 1/5 : FROM java:8 ---> d23bdf5b1b1b Step 2/5 : MAINTAINER ohb ---> Running in e0f0764f6095 Removing intermediate container e0f0764f6095 ---> 41b5fe818344 Step 3/5 : ADD target/springboot-dockerfile-1.0-SNAPSHOT.jar /app.jar ---> b985011c3e49 Step 4/5 : ENTRYPOINT ["java","-jar","/app.jar"] ---> Running in c566762d3fc6 Removing intermediate container c566762d3fc6 ---> f57d66188fef Step 5/5 : EXPOSE 8990 ---> Running in 3430401b323a Removing intermediate container 3430401b323a ---> 9506ade75591 Successfully built 9506ade75591 Creating container… Container Id: 9424847664f3b580ac1ce175322bf8f18d1532b394da00f943cbe2625df5ff83 Container name: 'springboot-dockerfile-demo' Starting container 'springboot-dockerfile-demo' 'springboot-dockerfile-demo Dockerfile: Dockerfile' has been deployed successfully.
-
设置 host与docker容器绑定的端口号
-
查询docker容器是否已经创建
-
测试
linux服务器测试(docker容器装在此服务器)
curl url
从外部访问容器。