‘Docker’ is a container system where one can inject various services into the container. It ensures service portability and environment similarity for both development and production environment. Additionally, it may support the features for other environment as well. In this post, we will focus on using the Docker container system in Spring Boot application development.

Creating application docker image using DOCKERFILE

In this section, we will do the following things – 

  1. Wrap the application in a package of JAR file
  2. Use JDK enabled image as the container
  3. Copy the JAR file to the docker container
  4. Run the JAR file

We are using Gradle (use mvn package command for maven). 

gradlew bootJar

Lets say this JAR file name is spring-boot-mysql-0.0.1-SNAPSHOT.jar. The following commands are used to build the JAR file. This JAR file is created in build/libs folder.
Create a Dockerfile (file name as Dockerfile) inside the application root folder with the following content – 

# use with jdk 11 alpine base image
FROM adoptopenjdk/openjdk11:alpine-jre

# make a documentation that this service communicates via 8080 port. this does not do 
EXPOSE 8080

# refer application jar file in a variable
ARG JAR_FILE=build/libs/spring-boot-mysql-0.0.1-SNAPSHOT.jar

# change working diretory to /opt/app
WORKDIR /opt/app

# add the jar file to /opt/app
ADD ${JAR_FILE} app.jar

# run the jar file using java
ENTRYPOINT ["java", "-jar", "app.jar"]

Build the application’s docker image using the docker build command. Along with it, the t option is used to add a tag to the image.

docker build -t spring-boot-mysql:0.0.1 .

Now if we try the docker image’s command, spring-boot-mysql should be in the images list with the tag 0.0.1. Run the docker image with docker run command.

docker run -d -p 8080:8080 spring-boot-mysql:0.0.1

Use MySQL from Docker

We can run MySQL service from docker and connect Spring Boot application to that containarized MySQL. We will use a docker-compose file here. This is a yml file which can be used to configure multiple containers, volumes, networks etc. 
We will create docker-compose.yml file in the application root directory.

version: '3.5'

volumes:
    mysql_data:

services:    
    mysql:
        image: mysql:5.7.29
        environment:
            - MYSQL_DATABASE=notebook_blogservice_db
            - MYSQL_ROOT_PASSWORD=root
        ports:
            - "3307:3306"
        restart: always
        volumes:
            - mysql_data:/var/lib/mysql

In this docker-compose.yml file we have a docker volume as mysql_data (line 3) and use it in mysql service (line 15). Docker volume is used to sync data between host and container. Data will still available in host even after deleting the container (unless you use docker-composer down-v command). This volume is fully managed by docker service. There is another way to sync data which is mentioned later.
We are mapping host 3307 port with container’s 3306 port. We are assuming that host already have a mysql service with 3306 port. Other wise we can use host’s 3306 port.
We will do to the docker-compose.yml file location and use following command to create the volume and container. This command will create all the volumes, networks, containers mentioned in this file.

$ docker-compose up
$ docker-compose up -d    # detached mode

We can inspect the components-

$ docker-compose ps    # check docker compose info
$ docker ps            # mysql container in the container list
$ docker volume ls     # mysql_data volume in the volume list

Docker bind mount mechanism can also be used for data syncing between container and host OS. A host folder will be directly mapped to a container folder in service’s volume section.

version: '3.5'

services:    
    mysql:
        image: mysql:5.7.29
        environment:
            - MYSQL_DATABASE=notebook_blogservice_db
            - MYSQL_ROOT_PASSWORD=root
        ports:
            - "3307:3306"
        restart: always
        volumes:
            - .mysql_data:/var/lib/mysql

Here mysql data will be stored in mysql_data folder in docker-compose.yml file level.

We can use the container mysql connection string in our Spring Boot application after executing the docker-compose up command. We will add followings properties in Spring Boot application.properties file –

app.db.host=localhost
spring.datasource.url=jdbc:mysql://${app.db.host}:3307/spring-boot-mysql_db

Now if we run the project it will use container mysql.

These are some helpful docker-compose commands to use.

docker-compose stop        # stop all the containers, networks
docker-compose start       # start all the containers, networks
docker-compose down        # stop and remove all the containers, networks
docker-compose down -v     # stop and remove all the container, networks and volumes
Previous ArticleNext Article

This post has 1 Comment

1

Leave a Reply

Your email address will not be published. Required fields are marked *