In the world of modern application development, containerization has become a key practice for building, deploying, and scaling applications. Docker, with its simplicity and efficiency, has emerged as a popular choice for containerization. In this guide, we will walk through the process of creating a Docker image for your Spring Boot application, allowing you to package your application and its dependencies into a single, portable container.
add pull policy and image tag in your build section in your pom.xml file. In your image.name tag there are 3 things you need to specify
so the final syntax will look like this {docker_user_name}/{application_name}:{application_version}
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <name>{your_docker_user_name}/demo-${project.artifactId}:${project.version}</name> </image> <pullPolicy>IF_NOT_PRESENT</pullPolicy> </configuration> </plugin> </plugins> </build>
mvn spring-boot:build-image
this process will take few minutes, after completion of the process you will see a success or failure message in the console
[INFO] Successfully built image 'docker.io/{your_docker_user_name}/demo-learn-docker:0.0.1-SNAPSHOT' [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 02:58 min [INFO] Finished at: 2023-12-14T19:51:14+05:30 [INFO] ------------------------------------------------------------------------
if your image is successfully created on docker hug registry then one link prefixed by docker.io will be printed in the log
bingo! our application is successfully converted to a docker image and our image path will be {your_docker_user_name}/demo-learn-docker:0.0.1-SNAPSHOT, this is the image path that we will be using for creating a docker container for our image.
viveksoni@viveksoni100:~$ docker run -p 9190:9090 viveksoni100/demo-learn-docker:0.0.1-SNAPSHOT
here -p stands for port, my 9190 is a local port and 9090 is a docker configured port
you'll see your application's log in the terminal and your container will also available in docker desktop
As we deployed our application successfully as a docker container, lets check if it is working fine
go to your favorite browser and hit http://localhost:9190/motivate
you'll see random motivational quotes are reflecting on your browser
congratulations! we've successfully deployed our springboot application to docker registry, and containerize it. From now on you just have to give your image path to the deployment team and they'll up your service within minutes, they don't have to install Java, Tomcat or any other dependencies just run your image in the docker container and magic will unfold by itself
Dockerizing your Spring Boot application is a transformative step that brings numerous benefits to your development and deployment processes. By encapsulating your application and its dependencies into a portable container, you've created an environment-agnostic package that can run consistently across various platforms. The ability to deploy this containerized application seamlessly provides advantages in terms of scalability, resource utilization, and ease of management.
As you continue your journey into containerization, consider exploring advanced Docker features such as multi-container setups with Docker Compose, optimizing Docker images for size and performance, and integrating Docker with orchestration tools like Kubernetes for scalable and resilient deployments.