Vvvvvvvvvvvvvvvvvvvvvvvvvvvveffortless Containerization - Deploying Spring Boot and MySQL With Docker and Docker Compose - DEV Community
Vvvvvvvvvvvvvvvvvvvvvvvvvvvveffortless Containerization - Deploying Spring Boot and MySQL With Docker and Docker Compose - DEV Community
Rajdip Bhattacharya
Posted on Aug 17, 2023
4 1 1
Greetings!
What is Containerization?
Before we dive into that, first let us understand the old school way of doing it. Make a
supposition that you are developing an application. You know every inch of this
application. Be it the databases or the environments, you have everything set up.
Now consider that this application is done with development. You now want to host it
someplace. For instance, let's say you decided to use Amazon EC2 for its ease of use.
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 1/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
Now let's look at the steps that you would perform to get this application running (at
least).
While this might look trivial, it becomes a pain when you have to do it over and over
again, for all of your applications. There is always a chance that some OS feature
would break your application, some dependency won't be installed, some
environmental variable might not be configured. It becomes extremely difficult to
debug such applications. Here is where containerization comes to our rescue.
A container includes everything needed for an application to run: the code, runtime,
system libraries, and settings. This self-contained unit ensures that the application
behaves consistently regardless of the environment it's deployed in. Containers are
lightweight, portable, and can be easily moved between different host systems or
cloud platforms without significant modifications.
Isolation: Containers are isolated from each other and from the host system,
preventing conflicts between dependencies and runtime environments.
Consistency: Containers ensure that an application behaves the same way in every
environment, reducing the "it works on my machine" problem.
Roadmap
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 3/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
Once you are satisfied, generate the project, extract it, and open it with your favourite
code editor.
Since this blog is focussed on getting Docker set up, I would be skipping the code
explanation in here. You can always clone the repository and check the code.
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 5/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
As you can see, I have injected environmental variables. This would allow us to
resolve the values at runtime. It gives us the flexibility to configure our application
without actually touching any of the code. Any change that you would like to make
to the environments, all you need to do is tweak the values as per your wish in the
system's environment.
I will be using a .env file to feed the values into the application. Nearly every IDE has
the support for doing so. In case you can't figure out how to include a .env file into
your execution environment, you can try setting those values in the environment of
your OS. Alternatively, you can let that untouched, since all the keys have a default
value assigned to them.
Now, we have our application ready. Before we are actually able to run it, we need to
launch MySQL, which we will do next.
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 6/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
With that, we have a brand new mysql container up and running which you can check
using:
docker ps
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 7/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
At this point of time, we are ready to launch our springboot application. Go to the
root of the project, and run
mvn spring-boot:run
mvnw spring-boot:run
curl http://localhost:8080/actuator/health
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 8/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
Notice that I'm still not hard coding the environmental variables. Also, note that, the
last line mentions using the command java -jar target/application.jar to launch the
container. For this to happen, we need to first set the build name to application in
the pom.xml .
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 9/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
To optimize the docker build process, I have first copied the pom.xml and then
resolved the dependencies before actually copying our soruce code. This is done with
the purpose of reducing the number of layers docker rebuild during its build process.
Source code is bound to change often. Putting that at the very top would mean all
the subsequent layers would be rebuilt.
Docker networking
Before we get started with running the application, let's first get a few points right
about networking in docker. When we run a docker image, the container boots up
into a separate docker network that works in isolation to our host network and other
docker containers. Hence, container A can't ping container B if they are running on
different networks. In our case, we would be running the springboot application and
MySQL database. So if we let them run in different networks, our containers won't be
able to intercommunicate.
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 10/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 11/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
docker network ls
Now, we need to migrate our MySQL container to this network. We do this by:
Now, we can verify that these commands work by inspecting the Containers section
in the output of this command:
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 12/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
communicate with the container's 3306 port via our hosts 3306 port.
We can bypass this by instructing docker to run the container directly on the host's
network. This can be done by:
Notice that I replaced the -p flag with the --network flag. When we are using the
host network driver, port mappings are neglected by docker.
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 13/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
Now that we know the fixes, let's move towards making the application work.
Next, run
docker run --name temp --rm -p 8080:8080 --env-file .env.local --network dummy-netwo
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 14/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
In case you get any error stating that the DB connection fail, I would like to point you
to the file. In there, we have a key called DB_HOST with the value set as
.env.local
springboot-test . This is the name we used when launching the MySQL docker
container. The above command will only work when these conditions are satisfied:
Alternatively, if you want to use some other name for the MySQL container, you can
should the name in the .env.local file aswell.
Now that we have everything up and running, we can verify our network again using
the docker network inspect dummy-network command.
For doing this, let us first create the docker-compose.yaml file in the root directory
of the project.
version: '3.8'
services:
mysql:
container_name: mysql
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=admin1234
networks:
- stack
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-padmin1
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 15/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
interval: 30s
timeout: 10s
retries: 3
application:
container_name: application
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
env_file:
- .env.docker
networks:
- stack
depends_on:
mysql:
condition: service_healthy
networks:
stack:
name: stack
driver: bridge
As you can see, we have created a network named stack . We have created two
services - application and mysql . Both of these services come under the stack
network. In the .env.docker file, I have set the DB_HOST to mysql . This name
corresponds to the name of the service. In the docker file, I have added a
dependency of mysql in application service. This means that the application service
wont start before the mysql service reaches the service_healthy state.
docker compose up
This command will take some time to start up. A few flags that might come in handy:
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 16/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
--build :
Rebuilds the images. Useful when you have made any changes to the
source code.
When you are done playing around with, you can shut down the entire thing by
using:
Conclusion
So that was all about using docker to make your lives easier. I hope you have quiet a
few tricks by now. Feel free to leave a comment in case you find something off.
AWS PROMOTED
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 17/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
Let’s Talk About Data is an information gold mine for data professionals looking
for the latest news and guidance on AWS Data & Analytics services and
partners.
Learn More
Rajdip Bhattacharya
Google DSC'22 Core Tech Lead & Cloud Facilitator || Full Stack Developer || Cloud Engineer ||
Game Dev || Cyber Security || Blockchain Developer || Open-source contributor
LOCATION
West Bengal, India
EDUCATION
Narula Institute of Technology
PRONOUNS
He/Him
WORK
Student and Intern
JOINED
Aug 7, 2023
Automating Python Deployments with GitHub Actions, AWS ECR, and AWS Lambda
#devops #python #cloud #aws
Sentry PROMOTED
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 18/19
7/15/24, 1:31 PM Effortless Containerization: Deploying Spring Boot and MySQL with Docker and Docker Compose - DEV Community
Watch video
https://dev.to/thecodersden/effortless-containerization-deploying-spring-boot-and-mysql-with-docker-and-docker-compose-e8n 19/19