docker部署springboot项目,连接mysql容器
入手docker部署springboot项目,记录一下学习过程,过程很简单,主要是部署过程中的收获,下面跟我一起来吧
1.创建一个springboot项目,pom引入maven-docker-plugin依赖
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <!-- 镜像名称--> <imageName>${docker.image.prefix}-docker-demo:0.0.1</imageName> <!-- dockerfile地址 --> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>
完成pom如下
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tz</groupId> <artifactId>docker</artifactId> <version>0.0.1-SNAPSHOT</version> <name>docker</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <!-- 镜像前缀--> <docker.image.prefix>tz</docker.image.prefix> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- springdata jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <!-- 镜像名称--> <imageName>${docker.image.prefix}-docker-demo:0.0.1</imageName> <!-- dockerfile地址 --> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> <finalName>tz-docker-demo</finalName> </build> </project>
2.编写Dockerfile 文件,文件位置src/main/docker/Dockerfile,文件内容如下
FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp # 下面jar包的名称为springboot项目打包完成的jar包名称 ADD tz-docker-demo.jar app.jar EXPOSE 8761 ENTRYPOINT ["java","-jar","/app.jar"]
3.集成jpa,方便测试mysql连通性,pom依赖第一步已经给出,下面是yml文件内容
# server server: port: 8081 servlet: context-path: /boot # database spring: datasource: url: jdbc:mysql://tz_mysql:3306/mystudy driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root # jpa jpa: hibernate: ddl-auto: update # 建表策略 show-sql: true #显示SQL语句
yml文件解析
jdbc:mysql://tz_mysql:3306/mystudy tz_mysql: 我自己的本地mysql的docker镜像名称,此处注意,一会使用命令启动时连接容器别名要和此处一致 mystudy: 本地mysql数据库
4.编写controller
package com.tz.docker; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.List; /** * @author tz * @Classname UserController * @Description * @Date 2019-08-15 21:08 */ @RestController public class UserController { @Autowired private UserInfoRepository userInfoRepository; @RequestMapping("/getUserInfo") public Object getUserInfo() { /* 这里有一个大坑:使用getOne()的方法,可以查到数据。但是无法默认转化成JSON数据,返回接口直接报错 */ //UserInfo userInfo = userInfoRepository.getOne(1); for(int i = 0;i<10;i++){ UserBean userBean = new UserBean(); userBean.setAge(12); userBean.setBirthday(new Date()); userBean.setCreatedTime(new Date()); userBean.setJob("3333:"+i); userBean.setSex("1"); userInfoRepository.save(userBean); } List<UserBean> all = userInfoRepository.findAll(); return all; } }
5.编写dao
package com.tz.docker; import org.aspectj.weaver.ast.Var; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** * @author tz * @Classname UserInfoRepository * @Description * @Date 2019-08-15 21:06 */ @Repository public interface UserInfoRepository extends JpaRepository<UserBean, Integer> { }
6.编写bean
package com.tz.docker; import lombok.Data; import javax.persistence.*; import java.util.Date; /** * @author tz * @Classname UserBean * @Description * @Date 2019-08-15 21:02 */ @Data @Entity //注意包名 @Table(name="user_info") public class UserBean { @Id @GeneratedValue(strategy= GenerationType.AUTO) private Integer id; private String name; private Integer age; private String sex; private String job; private Date birthday; private Date createdTime; private Date updateTime; }
7.开始镜像容器部署,首先打包应用,如果不打包直接构建镜像会保找不到jar包的错误
打包idea中使用maven工具栏
然后第二步build镜像
用命令就是
mvn clean package docker:build
8.构建成功之后使用docker images查看构建成功的镜像
9.确认mysql容器运行
10.运行我们构建成功的容器
docker run --rm -p 8081:8081 --name demo --link tz_mysql:tz_mysql tz-docker-demo:0.0.1
命令解析
--rm 运行完删除容器 -p 端口映射 --name 本镜像名称 --link tz_mysql 第一个参数为mysql的docker镜像名称,tz_mysql 第二个参数为别名,此处和yml文件中连接mysql的地址保持一致 tz-docker-demo:0.0.1 这个为我们刚刚build的镜像名称
11.好了,docker部署springboot项目就是这么简单,下面看一下成功
浏览器输入 http://localhost:8081/boot/getUserInfo
显示
下面附上项目源码
docker-demo
相关推荐
王道革 2020-11-25
bwyyziq 2020-11-22
pigsmall 2020-11-19
changecan 2020-11-19
helloWorldAndYou 2020-11-16
nginxs 2020-11-14
红石丶 2020-11-13
WanKaShing 2020-11-12
yangkang 2020-11-12
滴水穿石点石成金 2020-11-12
张荣珍 2020-11-12
wuxunanjing 2020-11-11
魅惑青花瓷 2020-11-11
lihongtai 2020-11-09
yangkang 2020-11-09
worldsnow 2020-11-06
MichaelJScofield 2020-11-06
TaoTaoFu 2020-11-06