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)删除所有镜像