美文网首页
Spring boot 构建docker镜像

Spring boot 构建docker镜像

作者: 咦咦咦萨 | 来源:发表于2018-03-06 17:03 被阅读0次
    1. 项目目录:
    image.png
    1. 调用docker mysql容器配置:
    # application.properties
    # 用mysql容器的别名替代ip
    spring.datasource.url=jdbc:mysql://mysql:3306/shop
    

    Mysql 容器中文编码设置(uft8),启动时添加如下命令:

    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --init-connect='SET NAMES utf8mb4;'
    

    参考:https://www.jianshu.com/p/085d4b79257c

    1. 在项目根目录创建Dockerfile文件:
    FROM openjdk:8-jdk-alpine
    VOLUME /tmp
    VOLUME /file
    ARG JAR_FILE
    ADD ${JAR_FILE} app.jar
    ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
    

    其中“VOLUME”可自行设置挂载卷。

    1. 在pom.xml中加入dockerfile相关插件:
    <properties>
      <!—- 镜像前缀名 —->
      <docker.image.prefix>mall</docker.image.prefix>
    </properties>
    

    Dockerfile插件:

    <!-- docker package -->
    <plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.3.6</version>
    <configuration>
      <repository>${docker.image.prefix}/${project.artifactId}</repository>
      <buildArgs>
       <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
      </buildArgs>
    </configuration>
    </plugin>
    
    1. 在maven配置文件setting.xml中加入如下内容:
    <pluginGroups>
        <pluginGroup>com.spotify</pluginGroup>
    </pluginGroups>
    
    1. 执行mvn构建命令:
    mvn clean install -Dmaven.test.skip=true dockerfile:build
    

    构建结果:

    构建结果.png

    此时该镜像已被docker加载,查看:docker images

    images.png
    1. 启动项目:
     docker run -d -p 8081:8081 -v /Users/admin/intellij/mall/file:/file --name mall --link mysql-container-name mall/mall:latest
    

    此处通过 --link命令链接容器,使web项目能通过别名访问mysql容器

    相关文章

      网友评论

          本文标题:Spring boot 构建docker镜像

          本文链接:https://www.haomeiwen.com/subject/oarrfftx.html