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

  • Automate Spring Boot App Deployment With GitLab CI and Docker
  • Keep Your Application Secrets Secret
  • How to Activate New User Accounts by Email
  • AOT Compilation Make Java More Power

Trending

  • Concourse CI/CD Pipeline: Webhook Triggers
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • AI's Dilemma: When to Retrain and When to Unlearn?
  1. DZone
  2. Coding
  3. Frameworks
  4. Running a Java App With MySQL in Any Docker Environment

Running a Java App With MySQL in Any Docker Environment

In this article, take a look at using Docker network to take advantage of container isolation features.

By 
Alok Ranjan Meher user avatar
Alok Ranjan Meher
DZone Core CORE ·
Jul. 08, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
13.3K Views

Join the DZone community and get the full member experience.

Join For Free

We very often use a container for the deployment of a microservices application/java app. Using containers has various advantages, and Docker is one of the first choices to implement containerization. But for a beginner, using Docker with proper configuration is a tedious job, particularly if we use using a database as backends. Since containerization means isolation of any application to the external world, an application cannot access any of the services, which are running in a container. 

An application needs any kind of backend service. So the backend needs to expose to the external world. But, exposing the port of the database for the external world, is not a good idea. But this problem can be solved by connecting the container through Docker networking. By connecting the database with the java application through the docker network is the best option. This way, we can solve the portability issues in different environments like Dev, QA, and Prod.

A Spring Boot application running with MySQL in Docker container with Docker network.

application.properties for Springboot application, where MySQL is running in Docker container in a network with name app_network.

Properties files
 




x


 
1
# configure mysql database
2
spring.jpa.hibernate.ddl-auto=update
3
spring.datasource.url=jdbc:mysql://mysql_name:3306/database_name
4
spring.datasource.username=root
5
spring.datasource.password=password



The Spring Boot application can communicate to MySQL in Docker containers, through the docker network.

The name of Docker network is app_network:

Shell
 




xxxxxxxxxx
1


 
1
docker network create app_network



Dockerfile file for Spring Boot is configured as follows.

Dockerfile
 




xxxxxxxxxx
1


 
1
FROM openjdk:14
2
CMD [“mkdir”, “app”]
3
WORKDIR app/
4
COPY target/sampleApp.jar app/app.jar
5
EXPOSE 8082
6
CMD [“java”, “-jar”, “app/app.jar”]



Spring Boot application image name is sample_app.

Shell
 




x


 
1
docker build -t sample_app .



Running Spring Boot image in Docker with network app_network

Shell
 




x


 
1
docker run -d -it -p 8082:8080 --network app_network --name sample_app sample_app



MySQL is running in Docker as mysql_name with password "password" and database "database_name" as follows:

Shell
 




x


 
1
docker run -d -it --network app_network -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=database_name --name mysql_name mysql



Now Spring Boot application can be run in any Docker environment without any IP address conflict.

Docker (software) Spring Framework MySQL Spring Boot app application Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Automate Spring Boot App Deployment With GitLab CI and Docker
  • Keep Your Application Secrets Secret
  • How to Activate New User Accounts by Email
  • AOT Compilation Make Java More Power

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!