自学内容网 自学内容网

Springboot项目打包成镜像、使用docker-compose启动

Springboot项目打包成镜像、使用docker-compose启动

1、创建一个boot项目

1、添加依赖

<?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 https://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.5.4</version>
    </parent>

    <groupId>com.wn</groupId>
    <artifactId>demo-compose</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-compose</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2、entity实体类:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
}

3、mapper类

import com.wn.entity.User;

public interface UserMapper{
    @Insert("insert into user(id,name) values (#{id},#{name})")
    void insert(User user);
}

4、controller类

@RestController
public class UserController {

    @Resource
    private UserMapper userMapper;

    @GetMapping("/insert")
    public String insert() {
        User user = new User();
        user.setId(1);
        user.setName("zhangsan");
        userMapper.insert(user);
        return "success";
    }
}

5、添加yml配置文件

spring:
  datasource:
    url: jdbc:mysql://47.94.85.176:3306/docker-compose-boot
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
server:
  ###端口号
  port: 8080
2、创建Dockerfile文件
# 基础镜像使用java
FROM openjdk:8
# VOLUME 指定了临时文件目录为/dockercompose/app
VOLUME /dockercompose/app
# 将jar包添加到容器中并更名为docker-compose-boot.jar
ADD compose-boot.jar docker-compose-boot.jar
# 运行jar包
RUN bash -c 'touch /docker-compose-boot.jar'
ENTRYPOINT ["java","-jar","/docker-compose-boot.jar"]
#暴露8080端口
EXPOSE 8080
3、创建一个docker-compose.yml文件
version: "3.0"
services:
  mysql: # mysql服务
    image: mysql:5.7
    restart: always
    container_name: mysql5.7
    command: --default-authentication-plugin=mysql_native_password # 解决外部无法访问
    volumes:
      - ./data:/var/lib/mysql  #挂载目录,持久化存储
    ports:
      - "3306:3306"        # 容器端口映射到宿主机的端口
    environment:
      MYSQL_ROOT_PASSWORD: 'root'            # root用户的密码,设置为root。
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'       # 确保必须设置密码,这里设置为no,意味着不允许空密码。
      MYSQL_DATABASE: 'docker-compose-boot'  # 创建的默认数据库名,这里设置为docker-compose-boot
  docker-compose-boot-web: # 自己单独的springboot项目
    hostname: scott
    build: ./              # 需要构建的Dockerfile文件
    ports:
      - "8080:8080"       # 容器端口映射到宿主机的端口
    depends_on:           # web服务依赖mysql服务,要等mysql服务先启动
      - mysql
4、上传到linux进行启动部署

image-20230321115218477

5、启动
 docker-compose -f docker-compose.yml up -d

image-20230321115345529
停止:

docker-compose stop

image-20230321115451920


原文地址:https://blog.csdn.net/hekai7217/article/details/140606129

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!