Make Spring Boot with Docker compose


2021-05-28 23:32 (KST)

Language :

Hi, I’m Lovefield.

Recently i making server infrastructure. The infrastructure can be configured in many ways, but we are introducing a container-based Docker. Now let’s talk about how to put the Spring Boot on Docker. We have a spring-boot-sample made by Devellany, so let’s take a look at it together!

First we make working directory.

 - app
 - db
    - data

I using docker-compose. Docker is controls container, while docker-compose is manage multiple containers. So I’m using it because I’m planning to use the container in division to detail. And assign and use on directory per container to match the directory structure with docker-compose.

What is docker-compose?

A tool for managing the container space by Docker in one file. The yml file is filled out and multiple containers can be managed at once using the docker-compose command.

Let’s make docker-compose.yml file. docker-compose has version 1 to 3. Specify version 3 to use the latest version.

version : “3”

services followed by the version is a practical set of containers. A single container is created with the value you enter in services and operates organically. There are two containers to use this time : "db" and "app".

Write "db" container information to install MySQL.

        container_name: spring-db
        image: mysql
            MYSQL_DATABASE: spring_db
            MYSQL_USER: spring
            MYSQL_PASSWORD: spring_pw
            MYSQL_ROOT_PASSWORD: root_pw
            - ./db/data:/var/lib/mysql:rw
            - “3306:3306”
        restart: always

container_name : set container name
image : using image name and tag with Docker hub image list
environment : set environment value in container
volumes : matching local directory to container directory
ports : matching externally ports with Docker internal ports
restart : whether to restart when terminated, such as a container error

Each property plays the that role. I will be install MySQL to "db" container. So set container name spring-db. If you want connect to DB, use name spring-db:3306. Because containers created with docker-compose.yml are automatically included in one network group. We can find each other using the container name and port number.

Well I can make image to use dockerfile, but I didn't need that. Because I didn't need complicated image. So I make container with MySql image with Docker hub image list.

Use environment value to set default DB option.
Use MYSQL_DATABASE value to set DB name,
Use MYSQL_USER value to set DB user name,
Use MYSQL_PASSWORD value to set DB user password,
Use MYSQL_ROOT_PASSWORD value to set DB root password,
If you specify each, initial setup of MySQL for "db" container is completed.

"/var/lib/mysql:rw" is the actual storage path of MySQL. If you run the Docker, you’ll get a lot of files in "db/data". This is what you see when you access the DB. The reason for this is to prevent internal data from being blown away when the container is shut down or deleted. The DB remains the same when data is stored locally and containers area re-created.

Next is "app" container to running Spring Boot.

        container_name: spring-app
        image: openjdk:11-jdk
            - “8082:8082”
            - ./app:/app
        working_dir: /app
        command: [“./gradlew”, “bootrun”]
            - db
        restart: always

You can see different command to "db" container. Each property plays the next role.
working_dir : the path where the container runs and then command runs
command : run command with Bash or PowerShell
depends_on : prioritize container execution. Execute the current container after the container entered as a child is normally executed

Container name is "spring-app". spring-boot-sample is using Java 11. So I set the image to openjdk:11-jdk.

Spring Boot is designated as 8082 port using internal Tomcat. If you connect with 8082 port from the outside, let's match the docker's internal port to look at 8082.

Align to the app directory where you want to save the Spring Boot configuration file. When you browse the app directory inside the container, you will find and connect to the app directory in local.

working_dir specifies the path where the container will command by default. Spring Boot source is in the app, so i set path to app.

Sprint Boot run command is "./gradlew bootrun". So we change write [“./gradlew”, “bootrun]. This format is from docker-compose.

Specifies that the "app" container is run after the "db" container is run using depends_on. This will run MySQL first, then the 'app' directory.

Now we need to clone spring-boot-samples within the "app" directory.

cd app & git clone .

Import code in "app" folder to use Git clone. We already "app" folder, So we must using "." option when you didn't want the new folder to "app" directory.

spring-boot-sample needs two default settings.
Frist spring-boot-sample has "node module" so we must run the following command:

cd src/main/resources & npm install

If you finished, Next is the application.yml setting.

    host: #SMTP_HOST
    port: #SMTP_PORT
    username: #ID
    password: #PASSWORD
          auth: false
          timeout: 50000
            enable: true
  host: http://localhost:8082
  title: Devellany's sample
  version: ver.prototype
  help-email: #MAIL_SENDER_ADDRESS
  token-available-period: 60

Create the file as shown above.

OK, We almost done!

docker-compose up -d

Using the above command to run docker-compose.Then Docker will download the necessary image and create the container according to the instructions written.If you see the completion mark, go to "http://localhost:8082" and check if it comes out normally.Even if the completion mark is displayed, it takes one to two more minutes for Spring Boot to run.

If you no longer use the containers you have configured,

docker-compose down

The above command removes all containers registered in the docker-compose.yml file and releases the network configuration.

So I used Docker to upload Spring Boot. There's no setting that's harder than you think, right? If you follow this article, you can quickly and easily build a local development environment. If you have any questions, please leave a comment!


Web Front-End developer

하고싶은게 많고, 나만의 서비스를 만들고 싶은 변태스러운 개발자입니다.