Article to dockerize simple spring boot application as front end module and mongodb as backend module.

Dockerization Layout

├── docker-compose.yml
├── mongodb
│   ├── Dockerfile
│   └── mongodb-server
│       ├── bin	
└── service
    ├── config
    │   ├── application.properties
    │   └── logback.xml
    ├── mybootservice.jar
    ├── Dockerfile
    ├── jdk1.8.0_65	    
    └── run.sh

docker-compose.yml

version: '2'

services:
  service:
    build: ./service/
    restart: always
    ports:
      - "80:8080"
    networks:
      - front-tier
      - back-tier
    volumes:
      - /docker/vol/mybootservice/service/logs:/opt/apps/service/logs

  mongodb:
    build: ./mongodb/
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - /docker/vol/mybootservice/mongodb/data:/data/db
    networks:
      - back-tier

networks:
  front-tier:
    driver: bridge
  back-tier:
    driver: bridge

Service module

File listing

drwxr-xr-x 4 root root     4096 Dec 12 04:17 ./
drwxr-xr-x 4 root root     4096 Dec 12 05:32 ../
drwxr-xr-x 2 root root     4096 Dec  9 09:48 config/
-rw-r--r-- 1 root root 25151195 Dec 12 04:17 mybootservice.jar
-rw-r--r-- 1 root root      617 Dec 12 01:01 Dockerfile
drwxr-xr-x 8 root root     4096 Dec  9 03:33 jdk1.8.0_65/
-rw-r--r-- 1 root root      704 Dec  9 09:26 run.sh

Dockerfile

FROM ubuntu:16.10

MAINTAINER kartik@test.com

RUN apt-get update && apt-get install -y iputils-ping  && rm -rf /var/lib/apt/lists/*

ADD jdk1.8.0_65 /opt/apps/jdk1.8.0_65
ADD config /opt/apps/service/config
ADD run.sh /opt/apps/service/run.sh
ADD mybootservice.jar /opt/apps/service/mybootservice.jar

ENV JAVA_HOME /opt/apps/jdk1.8.0_65
ENV PATH $PATH:$JAVA_HOME/bin

RUN chmod +x /opt/apps/service/run.sh   
RUN chmod +x /opt/apps/service/mybootservice.jar

VOLUME ["/opt/apps/service/logs"]

EXPOSE 8080

CMD ["/opt/apps/service/run.sh"]
  • Always make sure apt-get update is mentioned when any package is need ed to be added, else new packages cannot be added
  • shell script and jar should be applied with executable permissions, else script as well as jar will not run, else you will get error like cannot access jar

run.sh

#!/bin/bash

CONFIG_LOC=/opt/apps/service/config
JAR_NAME=/opt/apps/service/mybootservice.jar
SPRING_CONFIG_FILE=$CONFIG_LOC/application.properties
LOG_CONFIG_FILE=$CONFIG_LOC/logback.xml

java -jar $JAR_NAME --debug --spring.config.location=$SPRING_CONFIG_FILE --logging.config=$LOG_CONFIG_FILE
  • Make sure to add the #!/bin/bash, else script will not run in docker container.
  • Make sure jar should be applied with executable permissions.

config/application.properties

This is the spring boot properties

server.address=0.0.0.0
server.port=8080
spring.data.mongodb.host=mongodb
spring.data.mongodb.port=27017
spring.data.mongodb.repositories.enabled=false
spring.data.mongodb.uri=mongodb://mongodb/test
  • The mongodb is the hostname added automatically after linking the containers. Its taken care by the docker-compose internally.

config/logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
        <!-- [1] -->
        <file>/opt/apps/service/logs/app.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>/opt/apps/service/logs/app.log%i</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <logger name="com.hcc" level="DEBUG">
        <appender-ref ref="FILE" />
    </logger>

    <!-- Setting the root level of logging to INFO -->
    <root level="INFO">	        
        <appender-ref ref="FILE" />
    </root>

</configuration>


MongoDB Module

Dockerfile

FROM ubuntu:16.10

MAINTAINER kartik@test.com

RUN apt-get update && apt-get install -y iputils-ping  && rm -rf /var/lib/apt/lists/*

ADD mongodb-server /opt/apps/mongodb-server

VOLUME ["/data/db"]

EXPOSE 27017

CMD ["/opt/apps/mongodb-server/bin/mongod", "--dbpath=/data/db", "--port=27017"]
  • Make sure to expose correct port
  • Make sure to add the port exposed in the CMD if not the default, else mongo will not run in that port.
  • The data path is of the docker.