如何在开发环境中使用maven-docker以避免重复下载jar?

时间:2017-08-31 12:42:28

标签: java maven docker docker-compose development-environment

这是我的Dockerfile,但如果我更改了java代码,然后输入" docker-compose up -d --build"到终端,maven会重新下载jar,如何避免重复下载?

Dockerfile:

FROM maven:3.5.0-jdk-8
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn verify
ENTRYPOINT ["java","-jar","target/cloud-user.jar"]

搬运工-compose.yml:

version: "2"
services:
  cloud-nginx:
    build: cloud-nginx
    image: registry.cn-beijing.aliyuncs.com/wangdali/cloud-nginx
    restart: always
    ports: ["80:80"]
  cloud-consul:
    image: consul:0.9.2
    command: consul agent -dev -client 0.0.0.0
  cloud-front:
    build: cloud-front
    image: registry.cn-beijing.aliyuncs.com/wangdali/cloud-front
    restart: always
    volumes:
      - ./cloud-front/static:/app
  cloud-user:
    build: cloud-user
    image: registry.cn-beijing.aliyuncs.com/wangdali/cloud-user
    restart: always
  mysql: 
    image: mysql:5.7.19
    hostname: mysql
    volumes: 
      - ./docker/mysql/data:/var/lib/mysql 
      - ./docker/mysql/conf:/etc/mysql/conf.d 
      - /etc/localtime:/etc/localtime
    ulimits: 
      nproc: 65535
    mem_limit: 1024m 
    restart: always 
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"

我精简了我的测试项目,然后使用spring-boot插件进行maven

  

mvn spring-boot:run

启动项目,现在首先挂载.m2文件夹,然后运行mvn命令。 但是在我更改java源代码后,它不会被devtools自动加载,如何解决?

这是我的Dockerfile / docker-compose.yml:

Dockerfile:

version: "2"
services:
  user:
    build: user
    volumes:
      - $HOME/.m2/repository:/root/.m2/repository
      - ./user/src:/app/src
    ports:
      - "8080:8080"

搬运工-compose.yml:

FROM maven:3.5.0-jdk-8
WORKDIR /app
COPY pom.xml .
COPY src ./src
ENTRYPOINT ["mvn","spring-boot:run"]

1 个答案:

答案 0 :(得分:3)

解决了,我将目标文件夹挂载到容器,它可以自动加载,这是最终的dockerfile和docker-compose.yml,当然,在生产环境中,我们不能写:

Dockerfile:

FROM maven:3.5.0-jdk-8
WORKDIR /app
COPY pom.xml .
COPY src ./src
ENTRYPOINT ["mvn","spring-boot:run"]

搬运工-compose.yml

version: "2"
services:
  user:
    build: user
    volumes:
      - $HOME/.m2/repository:/root/.m2/repository
      - ./user/target:/app/target
    ports:
      - "8080:8080"
      - "5005:5005"

的pom.xml:

    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <mainClass>cn.codeforfun.Application</mainClass>
          <jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
        </configuration>
      </plugin>
    </plugins>