Simplify Local Development with Docker Compose – Part II

In this blog post, I will explain the ways to setup local development environment using docker compose.

This the second part in this series, you can read about first part here

In my first part, I have shown you setting local development environment using docker and docker compose. I was creating docker image and using docker compose run the container using that image.

In this post I will show you another way, where you don’t need to create docker image for testing your changes in container. We are going to use docker compose volume feature to test the changes in local development.

The volume feature maps the local file system to container file system, i.e it copies local files to container.

If you are deploying spring boot application, you can use following docker-compose file.

You can observer that I am mapping local jar to container.

version: '3'

services:
  springboot-crud-example:
    container_name: 'springboot-app'
    image: 'docker.io/eclipse-temurin:17.0.4_8-jdk-jammy'
    volumes :
      - ./target/sb-docker-localdev-0.0.1-SNAPSHOT.jar:/app.jar
    ports:
      - 8080:8080
      - 8000:8000
    entrypoint: ["java","-jar","/app.jar"]
    environment:
      SPRING_PROFILES_ACTIVE: container
      JAVA_OPTS:
        -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
    depends_on:
       - "postgres"


  postgres:
    container_name: 'postgresdb'
    image: 'docker.io/postgres:13.2'
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: admin
      POSTGRES_DB: eisCode language: Java (java)

Note : Pleas adjust the local jar file path as per your configuration.

The biggest advantage of this approach is , you do not need to build the image every time , there is a change. You can just build stat the container with docker-compose up command and test your changes.

If you are deploying the spring boot application as the war file, you can use the tomcat image and map the war file with volume command.

version: '3'

services:
  sb-war-app:
    image: 'docker.io/tomcat:9.0.68-jdk17'
    ports:
      - 8080:8080
    volumes:
      - ./target/springboot-crud-example-0.0.1-SNAPSHOT.war:/usr/local/tomcat/webapps/sbcrudexample.war
    environment:
      SPRING_PROFILES_ACTIVE: container
      JAVA_OPTS:
        -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
    depends_on:
      - "postgres"

  postgres:
    container_name: 'postgresdb'
    image: 'docker.io/postgres:13.2'
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: admin
      POSTGRES_DB: eisCode language: Java (java)

Similar Posts