DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Running a Java App With MySQL in Any Docker Environment
  • Docker With Spring Boot and MySQL: Docker Swarm Part 3
  • Keep Your Application Secrets Secret
  • Spring Boot With Kubernetes

Trending

  • AI, ML, and Data Science: Shaping the Future of Automation
  • Agile and Quality Engineering: A Holistic Perspective
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  1. DZone
  2. Coding
  3. Frameworks
  4. Docker With Spring Boot and MySQL: Docker Compose (Part 2)

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.

By 
Sanjoy Kumer Deb user avatar
Sanjoy Kumer Deb
DZone Core CORE ·
Dec. 13, 19 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
36.1K Views

Join the DZone community and get the full member experience.

Join For Free

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

Speed Up Development with Docker Compose


Docker (software) Spring Framework Spring Boot MySQL application

Opinions expressed by DZone contributors are their own.

Related

  • Running a Java App With MySQL in Any Docker Environment
  • Docker With Spring Boot and MySQL: Docker Swarm Part 3
  • Keep Your Application Secrets Secret
  • Spring Boot With Kubernetes

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!