Docker-compose部署Springboot+Mysql

Jar包镜像的制作
下面的项目会有一点点问题,就是docker-compose文件对不上,请参照下面的进行修改。此外,application.yml也必须进行相应的更改,需要更改的是数据库的username)。

spring.datasource.druid.url=jdbc:mysql://sysql:3306/myblog?useUnicode=true&characterEncoding=utf-8&useSSL=false

创建并编辑Dockerfile文件

touch Dockerfile && vim Dockerfile
内容:

FROM java:8

VOLUME /home/sunyue/docker

RUN mkdir -p /home/sunyue/docker

WORKDIR /home/sunyue/docker

COPY my_blog-0.0.1-SNAPSHOT.jar /home/sunyue/docker/app.jar

CMD ["java", "-Xmx200m", "-Djava.security.egd=file:/dev/./urandom","-jar","/home/sunyue/docker/app.jar"]

docker-compose.yml的创建

创建docker-compose.yml文件: touch docker-compose.yml

version : '3'
services:
  sysql:
    restart: always
    image: mysql:5.7.22
    container_name: sysql
    ports:
      - 3306:3306
    environment:
      TZ: Asia/Shanghai
      MYSQL_DATABASE: myblog
      MYSQL_ROOT_PASSWORD: sunyue
    command:
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M
      --sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
    volumes:
      - /var/lib/mysql

  springappserver:
    container_name: springappserver
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "80:80"
    depends_on:
      - sysql
    links:
      - sysql
    volumes:
      - ./upload:/home/sunyue/docker/upload

图片上传注意点:上面有个数据卷(- ./upload:/home/sunyue/docker/upload),容器路径/home/sunyue/docker/upload挂载服务器本地地址./upload,./表示当前路径,我部署的根目录是/home/sunyue/docker,所有的图片都放在/home/sunyue/docker/upload下。我不知道是不是必须保证/home/sunyue/docker/upload必须容器中和服务器中需要一致,不一致springboot好像无法加载图片。
附上图片映射处理类MyWebAppConfigurer

package com.sunyue.myblog;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * 资源映射路径
 */
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
    //跨域
    /*   @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index");
    }*/

    /* @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
         registry.addResourceHandler("/upload/**").addResourceLocations("file:F:/img/");
     }*/
    // 获取配置文件中图片的路径
    @Value("${cbs.imagesPath}")//cbs.imagesPath=/home/sunyue/docker/upload/
    private String mImagesPath;


    // 访问图片方法
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //埋个坑,上线时判断路径用
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {  //如果是Windows系统
            if (mImagesPath.equals("") || mImagesPath.equals("${cbs.imagesPath}")) {
                String imagesPath = MyWebAppConfigurer.class.getClassLoader().getResource("").getPath();
                if (imagesPath.indexOf(".jar") > 0) {
                    imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
                } else if (imagesPath.indexOf("classes") > 0) {
                    imagesPath = "file:" + imagesPath.substring(0, imagesPath.indexOf("classes"));
                }
                imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/")) + "/images/";
                mImagesPath = imagesPath;
            }
            LoggerFactory.getLogger(MyWebAppConfigurer.class).info("imagesPath1=" + mImagesPath);
            //这个地方也只能是绝对路径
            registry.addResourceHandler("/images/**").addResourceLocations("file:" + mImagesPath);
            WebMvcConfigurer.super.addResourceHandlers(registry);
        } else {  //linux 和mac
            // imagesPath2=/home/sunyue/docker/upload/
            LoggerFactory.getLogger(MyWebAppConfigurer.class).info("imagesPath=" + mImagesPath);
            //registry.addResourceHandler("/images/**").addResourceLocations(mImagesPath);
            //绝对路径
            registry.addResourceHandler("/images/**").addResourceLocations("file:" + mImagesPath);
            WebMvcConfigurer.super.addResourceHandlers(registry);
        }


    }

}

运行

docker-compose up -d后台启动

docker-compose stop关闭

补充:
重新部署需要吧镜像文件删了,不然还是以前的容器,不会因为你修改Dockerfile和docker-compose.yml修改而变化,具体处理如下:

docker stop $(docker ps -a -q) //  stop停止所有容器
docker  rm $(docker ps -a -q) //   remove删除所有容器
sudo docker rmi $(docker images -q)删除所有镜像
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值