Statement : With the help of Docker, one can easily create, deploy and run their applications. So in this article, we’ll learn how one can deploy their Microserives (Spring Boot Application connected with Mysql Backend) to Docker Container.
Prerequisites : Please insure that Docker, Java, Mysql and mvn is installed on your machine. Now, please follow the below steps to dockerize your microservice –
- Build your code : Maven is used as the build automation tool, so we need to build our code through the below command so that we can get the complete jar file having the actual application code with all the required dependencies.
mvn clean install
- Create Dockerfile : Go to the root of the application where pom.xml is contained. Below is the content of my Dockerfile –
#Fetch the base Jav8 image FROM java:8 #Expose the local application port EXPOSE 8080 #Place the jar file to the docker location ADD /target/microservicedemo-1.0-SNAPSHOT.jar microservicedemo-1.0-SNAPSHOT.jar #Place the config file as a part of application ADD src/main/resources/application.properties application.properties #execute the application ENTRYPOINT ["java","-jar","microservicedemo-1.0-SNAPSHOT.jar"]
- Go to Spring Boot applicaion.properties where you have mentioned the backend url in terms of mysql database.
spring.datasource.url = jdbc:mysql://localhost:3306/microservice // Change the above url to the below one spring.datasource.url = jdbc:mysql://mymicroservicesqldbdb:3306/microservice
- When we run any application on Docker Container, we need to tell the existence of mysql backend if any with the help of docker networking. So, instead of running the mysql instance locally, we need to run one more docker container for mysql. For that, we need to create one network so that both the application and mysql docker container can talk to each other.
docker network create account-mysql
- Spring Boot application has the requirement of connecting with Mysql instance, so firstly I need to run mysql container and I can pull that mysql image from docker hub directly.
docker container run --name mymicroservicesqldbdb --network account-mysql -e MYSQL_USER=demo -e MYSQL_PASSWORD=demo -e MYSQL_DATABASE=microservice -e MYSQL_ROOT_PASSWORD=root -d mysql:8
- Build Docker Image : Now, it is the time to build the actual docker image.
docker build -f Dockerfile -t microservice .
- Run the Docker Image :
docker run --network account-mysql -p 8000:8080 -t microservice
Option -p
publishes or maps host system port 8000 (where you need to host the application like local machine) to container port 8080 (where your actual application code resides).
- To find the details of your running container
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fb15604af25b microservice "java -jar microserv…" About a minute ago Up About a minute 0.0.0.0:8000->8080/tcp serene_chaplygin d1609d184de5 mysql:8 "docker-entrypoint.s…" 55 minutes ago Up 55 minutes 3306/tcp, 33060/tcp mymicroservicesqldbdb
You can find the complete code in my github repository. Now you can test your REST APIs through the url http://localhost:8000 using any of the REST client . Hope it helps you to deckerize your microservice easily. 🙂