{{announcement.body}}
{{announcement.title}}

Docker With Spring Boot and MySQL: Docker Compose (Part 2)

DZone 's Guide to

Docker With Spring Boot and MySQL: Docker Compose (Part 2)

The second part of this tutorial series demonstrates how to use docker-compose with the Docker CLI and a sample application to run multiple containers.

· Cloud Zone ·
Free Resource

Image title

docker-compose helps manage multiple containers

In my previous article, I wrote about Docker, the CLI commands needed to run a database, and Spring Boot applications. We used Dockerfile to set up the environment and run the application by running containers separately and then building a link between them. But for multiple container applications we can use the docker-compose tool. Docker CLI can manage a single container, but docker-compose can manage multiple containers and define the dependent services. 

Important Terms

If we want to run services with the docker-compose tool we have to follow these steps that are also defined in Docker documentation.

  1. We need to define the application environment with a Dockerfile, so it can be reproduced anywhere.
  2. We need to define the services that make up the application in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker-compose commands to run/stop the container or deploy/undeploy the application.

We need a docker-compose.yml file to write the services. In a Dockerfile, we defined the environment of the application, and in docker-compose file we write down the other properties of services, like which service will run on which port, which service will be dependent on other services, which port will be forwarded to other port for public access, define network, cluster applications, etc.  

You may also enjoy: Getting Started With Docker Compose

Installation

In my previous article, I described the steps to install Docker. So, we can check that docker-compose is installed by running the command  docker-compose -v. In my case it returns:  docker-compose version 1.24.1, build 4667896b 

Code Download and Run

Now we can download the code from here. Or we can clone from and checkout the feature/docker branch.  Here we can see the Dockerfile is similar to the code described on my previous article.

Dockerfile




x


1
FROM java:8
2
COPY /build/libs/book-manager-1.0-SNAPSHOT.jar book-manager-1.0-SNAPSHOT.jar
3
ENTRYPOINT ["java","-jar","book-manager-1.0-SNAPSHOT.jar"]


And the docker-compose.yml file looks like this:

YAML




xxxxxxxxxx
1
32


1
version: '3'
2
services:
3
  docker-mysql:
4
    restart: always
5
    container_name: docker-mysql
6
    image: mysql
7
    environment:
8
      MYSQL_DATABASE: book_manager
9
      MYSQL_ROOT_PASSWORD: root
10
      MYSQL_ROOT_HOST: '%'
11
    volumes:
12
      - ./sql:/docker-entrypoint-initdb.d
13
 
          
14
    ports:
15
      - "6033:3306"
16
    healthcheck:
17
      test: "/usr/bin/mysql --user=root --password=root--execute \"SHOW DATABASES;\""
18
      interval: 2s
19
      timeout: 20s
20
      retries: 10
21
 
          
22
  book-manager-app:
23
    restart: on-failure
24
    build: ./
25
    expose:
26
      - "10222"
27
    ports:
28
      - 10222:10222
29
    environment:
30
      WAIT_HOSTS: mysql:3306
31
    depends_on:
32
      - docker-mysql


I defined two services named by docker-mysql and book-manager-app. Service book-manager-app is dependent on docker-mysql. We are using docker-compose version 3. MySQL will run on the 3306 port at a docker container but we can access it publicly from 6033. book-manager-app will run on port 10222.  We have an initial DML and DDL file at the SQL directory which will run during startup time of Docker and MySQL setup.

Run Application With docker-compose

Now we will apply some commands to run our application. I think we have already downloaded project from the above link. We will go to the project root directory. To run the application we will use following commands:

  •  docker-compose up — This will execute Dockerfile commands and will run services defined in the docker-compose file.
  •  docker-compose down — This will stop and remove all containers that were running by docker-compose file.
  •  docker-compose up --build — If we do an update on the Dockerfile, the war/jar file, or the docker-compose file, then we have to execute this command to get updated data on the Docker machine.

So, I think after running docker-compose up, It runs the application with all services in the Docker machine. Ignore some initial exception logs for database communication. To check whether it runs or not, we can check http://localhost:10222/book. We can see the list of books here. 

Happy Coding!!!

Further Reading

A Developer's Guide To Docker (Part 2): The Dockerfile

Speed Up Development with Docker Compose

Topics:
docker ,mysql ,docker-compose ,devops ,spring boot ,docker compose ,cloud

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}