文章目录
- 一、Compose入门案例
- 1. 依赖
- 2. 实体类
- 3. mapper接口
- 4. 启动类
- 5. yml配置
- 6. 测试案例
- 7. 打包
- 二、制作 DockerFile和docker-compose.yml
- 2.1. 制作 DockerFile
- 2.2. docker-compose.yml
- 三、打包部署
- 3.1. 资料上传
- 3.2. 启动docker-compose
- 3.3. 创建表
- 3.4. 接口测试
- 3.5. 数据验证
- 3.6. Compose常用命令
一、Compose入门案例
流程:
- 需要定义一个docker-compose.yml文件----工程
- 需要在docker-compose文件配置依赖服务
- docker-compose up 执行该文件
1. 依赖
创建springboot项目
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>
2. 实体类
package com.gblfy.entity;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;/**** @Author gblfy* @Date 2022-03-20 8:18**/
@Data
@ToString
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
@TableName("docker_compose_entity")
public class DockerComposeEntity {private Integer id;private String name;
}
3. mapper接口
package com.gblfy.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gblfy.entity.DockerComposeEntity;/**** @Author gblfy* @Date 2022-03-20 8:17**/
public interface DockerComposeMapper extends BaseMapper<DockerComposeEntity> {
}
4. 启动类
package com.gblfy;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.gblfy.mapper")
public class DockerComposeApplication {public static void main(String[] args) {SpringApplication.run(DockerComposeApplication.class, args);}}
5. yml配置
spring:datasource:url: jdbc:mysql://mysql:3306/docker-compose-boot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTCusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver
server:###端口号port: 8080servlet:##设置springboot项目访问路径context-path: /gblfy
6. 测试案例
package com.gblfy;import com.gblfy.entity.DockerComposeEntity;
import com.gblfy.mapper.DockerComposeMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;/**** @Author gblfy* @Date 2022-03-20 8:16**/
@RestController
public class DockerComposeController {@Resourceprivate DockerComposeMapper dockerComposeMapper;@PostMapping("/insert")public String insert() {DockerComposeEntity dockerComposeEntity = new DockerComposeEntity();dockerComposeEntity.setId(1);dockerComposeEntity.setName("zhangsan");dockerComposeMapper.insert(dockerComposeEntity);return "success";}
}
7. 打包
mvn clean package -Dmaven.test.skip=true
二、制作 DockerFile和docker-compose.yml
2.1. 制作 DockerFile
# Docker image for springboot file run
#VERSION 0.0.1
#Author: GBLFY
# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER gblfy.com
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp,测试可以这样,企业中这里建议docker真实目录并赋予权限
VOLUME /tmp
# 将jar包添加到容器中并更名为docker-compose-boot.jar
ADD docker-compose-0.0.1-SNAPSHOT.jar docker-compose-boot.jar
# 运行jar包
RUN bash -c 'touch /docker-compose-boot.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/docker-compose-boot.jar"]
#暴露8080端口
EXPOSE 8080
2.2. docker-compose.yml
version: "3.0"
services:mysql: # mysql服务image: mysql:5.7command: --default-authentication-plugin=mysql_native_password #解决外部无法访问ports:- "3306:3306" #容器端口映射到宿主机的端口environment:MYSQL_ROOT_PASSWORD: 'root'MYSQL_ALLOW_EMPTY_PASSWORD: 'no'MYSQL_DATABASE: 'docker-compose-boot'MYSQL_USER: 'gblfy'MYSQL_PASSWORD: '123456'networks:- gblfy_webdocker-compose-boot-web: #自己单独的springboot项目hostname: gblfybuild: ./ #需要构建的Dockerfile文件ports:- "38000:8080" #容器端口映射到宿主机的端口depends_on: #web服务依赖mysql服务,要等mysql服务先启动- mysqlnetworks:- gblfy_web
networks: ## 定义服务的桥gblfy_web:
这里演示为了看测试效果,关键的目录映射没有设置,企业中是需要把数据目录和日志目录映射到宿主机目录,简言之,目录挂载
三、打包部署
3.1. 资料上传
docker-compose-0.0.1-SNAPSHOT.jar
docker-compose.yml
Dockerfile
3.2. 启动docker-compose
cd /app/docker-compose-boot
docker-compose up
日志(拉取mysql5.7镜像和jdk8镜像我已经操作过了,下面日志是将springboot项目打包成镜像)
Building docker-compose-boot-web
Step 1/7 : FROM java:8---> d23bdf5b1b1b
Step 2/7 : MAINTAINER gblfy.com---> Running in b052905b2072
Removing intermediate container b052905b2072---> 980a20b17ff7
Step 3/7 : VOLUME /tmp---> Running in d62f63bb0b52
Removing intermediate container d62f63bb0b52---> 51a9d0815625
Step 4/7 : ADD docker-compose-0.0.1-SNAPSHOT.jar docker-compose-boot.jar---> 8c574ff477a7
Step 5/7 : RUN bash -c 'touch /docker-compose-boot.jar'---> Running in 3722a261b8ed
Removing intermediate container 3722a261b8ed---> b6892e5f045a
Step 6/7 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/docker-compose-boot.jar"]---> Running in 5bc7270bbfd7
Removing intermediate container 5bc7270bbfd7---> d7c53524cf66
Step 7/7 : EXPOSE 8080---> Running in 92e2c377c0ca
Removing intermediate container 92e2c377c0ca---> f18c2c26bf27
Successfully built f18c2c26bf27
Successfully tagged docker-compose-boot_docker-compose-boot-web:latest
WARNING: Image for service docker-compose-boot-web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating docker-compose-boot_mysql_1 ... done
Creating docker-compose-boot_docker-compose-boot-web_1 ... done
3.3. 创建表
3.4. 接口测试
http://192.168.92.134:38000/gblfy/insert
3.5. 数据验证
3.6. Compose常用命令
docker-compose -h # 查看帮助docker-compose up # 创建并运行所有容器
docker-compose up -d # 创建并后台运行所有容器
docker-compose -f docker-compose.yml up -d # 指定模板
docker-compose down # 停止并删除容器、网络、卷、镜像。docker-compose logs # 查看容器输出日志
docker-compose pull # 拉取依赖镜像
dokcer-compose config # 检查配置
dokcer-compose config -q # 检查配置,有问题才有输出docker-compose restart # 重启服务
docker-compose start # 启动服务
docker-compose stop # 停止服务