Category Archives: Docker

Adding docker support in Spring Boot application

Adding docker containerization support in a working Spring Boot application is surprisingly easy.

Step 1: Add the docker maven plugin to your pom file.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.4</version>
            <configuration>
                <repository>${project.artifactId}</repository>
            </configuration>
        </plugin>
    </plugins>
</build>

Step 2: Build the application as well as the docker image

mvn clean package install dockerfile:build

Step 3: I have a local docker registry setup in my Lan so I prefer to push this image to my network registry manually (this can be automated as well however).

docker tag my-app:latest my_network_registry_host:5000/my-app

Step 4: Now we can bootup this docker image anywhere in the network.

docker run \
--detach \
--link <any database link> \
-e <Environment variable 1> \
-e <Environment variable 2> \
-p 8080:8080 \
--name my-app \
my_network_registry_host:5000/simply-hr
 Step 5: Checkout the application URL @ http://localhost:8080. Tail the log if you want to
docker logs --follow simply-hr

Setting up Redmine docker instance

In order to setup a fully functional Redmine server via Docker I use the following consolidated command:

docker run \
--detach \
--name redmine \
--link mysql:mysql \
--publish 3000:3000 \
--env="REDMINE_DB_MYSQL: redmine" \
--env="REDMINE_DB_PASSWORD: redmine" \
--volume /Volumes/DATA/Abhinav/Docker/redmine/files:/usr/src/redmine/files \
redmine

This connects my mysql docker instance with sonar and it is able to create tables and start using MySQL properly for storing all it’s data.

Setting up Sonarqube docker instance

In order to setup a fully functional Sonarqube server via docker I use the following consolidated command:


docker run \
--detach \
--name=sonarqube \
--publish 9000:9000 \
--publish 9092:9092 \
--env="SONARQUBE_JDBC_USERNAME=sonar" \
--env="SONARQUBE_JDBC_PASSWORD=sonar" \
--env="SONARQUBE_JDBC_URL=jdbc:mysql://mysql:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true" \
--link mysql:mysql \
sonarqube:5.1

This connects my dockerized mysql instance with sonar and it is able to create tables and start using MySQL properly for storing all it’s data.

Setting up MySQL docker instance

In order to setup a fully functional MySQL server via docker I use the following consolidated command:

docker run \
--detach \
--name=mysql \
--env="MYSQL_ROOT_PASSWORD=abcd1234" \
--publish 6603:3306 \
--volume=~/Docker/mysql/conf.d:/etc/mysql/conf.d \
--volume=~/Docker/mysql/mysql-datadir:/var/lib/mysql \
mysql

The advantage of the above approach is that the configuration files can be defined under ~/Docker/mysql/conf.d folder on my system and it will be picked up whenever I bootup this instance. The second thing is that all information written by this instance will be stored outside of VM in the path ~/Docker/mysql/mysql-datadir on my system. So In case this VM goes away due to some unexplained reasons I can still bootup another docker instance of MySQL and point it to this data directory.