Compose Spring Boot app

Suppose we want to deploy Spring Boot application named kcms via docker.

Suppose we use nginx for redirection and mongoDB for storing data.

To resolve this task we will prepare the dist/ directory with following content:

dist/
    docker-compose.yml
    kcms.dockerfile
    kcms-0.1.0.jar
    volumes/
        app/
        database/
            backup/
            data/
            init/
                mongo-init.js
        nginx/
            conf.d/
                app.conf

Where

  1. kcms-0.1.0.jar - compiled Spring Boot application
  2. kcms.dockerfile - dockerfile for making container with Spring Boot application (read more)
  3. volumes/ - directory for volumes of our containers
    • app/ - directory, which is used by application for storing uploaded or created resources (read more)
    • database/ - directory for volumes related with mongoDB. For example, mongo-init.js will be used for creation database at first time.
    • nginx/ - directory for volume related with nginx.

And finally, docker-compose.yml will have following content.

version: "3.3"
services:
  kcms-mongodb:
    container_name: kcms-mongodb
    image: mongo:latest
    restart: always
    volumes:
      - ./volumes/database/data:/data
      - ./volumes/database/backup:/backup
      - ./volumes/database/init/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=myadminpsw

    ports:
      - "27017:27017"
    networks:
      - my-net

  kcms-app:
    container_name: kcms-app
    #image: kcms 
    build: # or build image from dockerfile
      dockerfile: kcms.dockerfile
      args:
        JAR_FILE: kcms-0.1.0.jar
      context: .
    restart: always
    ports:
      - 8080:8080 # Replace the port of your application here if used
    volumes:
      - ./volumes/app/:/app/resources
    depends_on:
      - kcms-mongodb
    networks:
      - my-net

  nginx:
    container_name: kcms-nginx
    image: nginx:1.13
    restart: always
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./volumes/nginx/conf.d:/etc/nginx/conf.d
    depends_on:
      - kcms-app

networks:
  my-net:

Here we define 3 services for mongoDB, Spring Boot app and nginx.

mongoDb and the app are running on the same local network "my-net", otherwise the database will be inaccessible to the application.