title: Docker compose cache dependensies but not the code
body: <p>I am a Java backend developer and i want to dockerize the backend part in order for my front end college to easily run in on his local machine. I will describe my problem based on demo project. So the demo project consists out of mysql and a spring boot web app. The app is works fine when i am using <strong>docker-compose</strong>, all the dependecies are download before the start and the endpoint &quot;test&quot; is returning &quot;Some message&quot;. The problem occurse when i change the code and <strong>docker-compose down</strong> and <strong>docker-compose up</strong> the app, the code isnt changing. What i can do to solve this problem is to package the app on local machine and then deliver it to docker, but then the local machine has to has maven in order to compile it, and i dont want that, i want my college just to use docker and dont care about how all this thing works. Another solution could be not to cache, but then the dependencies have to be downloaded every time we restart the app, that is also not acceptable. So my question is &quot;<strong>Is there a way to cache the dependecies (to download it once) but not the code, so that when i am starting the app, a fresh app ,with updated code, would compile?</strong>&quot; (Sorry for my english). Here is the demo project that i am testing on:</p>
  <p>The controller:</p>
  <pre><code>@RestController
  @RequestMapping(&quot;message&quot;)
  @RequiredArgsConstructor
  public class Controller {
  
      private final MessageRepository messageRepository;
  
  
      @GetMapping(&quot;/{id}&quot;)
      public Message getById(@PathVariable Long id) {
          return messageRepository.findById(id).get();
      }
  
      @PostMapping
      public Message save(@RequestBody Message message){
          return messageRepository.save(message);
      }
  
      @GetMapping(&quot;/test&quot;)
      public String ss(){
          return &quot;Some message&quot;;
      }
  
  }
  </code></pre>
  <p>The model:</p>
  <pre><code>@Getter
  @Setter
  @Entity
  @NoArgsConstructor
  @AllArgsConstructor
  @Table(name = &quot;messages&quot;)
  public class Message {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = &quot;id&quot;, nullable = false)
      private Long id;
      @Column(name = &quot;text&quot;)
      private String text;
  }
  </code></pre>
  <p>The repository:</p>
  <pre><code>@Repository
  public interface MessageRepository extends JpaRepository&lt;Message,Long&gt; {}
  </code></pre>
  <p>property file:</p>
  <pre><code>spring:
    datasource:
      url: jdbc:mysql://localhost:3306/test?useUnicode=yes&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true
      username: root
      password: 123456789
      driver-class-name: com.mysql.cj.jdbc.Driver
      sql-script-encoding: UTF-8
    jpa:
      database-platform: org.hibernate.dialect.MySQL8Dialect
      hibernate:
        ddl-auto: create-drop
  </code></pre>
  <p>Docker file that is in root of the spring project:</p>
  <pre><code>FROM maven:3.8.4-jdk-11
  WORKDIR /demo
  COPY . .
  RUN mvn clean install package -Dmaven.test.skip
  CMD mvn spring-boot:run
  </code></pre>
  <p>Docker compose file:</p>
  <pre><code>version: &quot;3.8&quot;
  
  services:
    mysqldb:
      image: mysql:8.0.27
      restart: unless-stopped
      environment:
        - MYSQL_DATABASE=db
        - MYSQL_ROOT_PASSWORD=123456789
      ports:
        - 3307:3306
      volumes:
        - db:/var/lib/mysql
    app:
      depends_on:
        - mysqldb
      build: ./demo
      ports:
        - 8080:8080
      environment:
        SPRING_APPLICATION_JSON: '{
          &quot;spring.datasource.url&quot;  : &quot;jdbc:mysql://mysqldb:3306/db?useUnicode=yes&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true&quot;,
          &quot;spring.datasource.username&quot; : &quot;root&quot;,
          &quot;spring.datasource.password&quot; : &quot;123456789&quot;,
          &quot;spring.jpa.database-platform&quot; : &quot;org.hibernate.dialect.MySQL8Dialect&quot;,
          &quot;spring.jpa.hibernate.ddl-auto&quot; : &quot;update&quot;
        }'
      volumes:
        - .:/root
      stdin_open: true
      tty: true
  
  volumes:
    db:
  </code></pre>