Containerizing and Deploying Your Spring Boot Application with Docker
Introduction to Docker:
Docker is an open-source platform that automates the deployment, scaling, and management of applications. It enables developers to create lightweight, portable, and self-sufficient containers from any application.
In this guide, we will walk you through the process of containerizing a Spring Boot application using Docker. We'll use an example application named springboot-docker-demo to illustrate the steps.
Build the Spring Boot Application JAR
First, ensure that your Spring Boot application is packaged as a JAR file. Navigate to the root directory of your Spring Boot project and run the following command:
./mvnw clean package
This will create a JAR file in the target directory of your project.
Step 1: Create a Dockerfile
First, create a Dockerfile in the root directory of your Spring Boot application with the following contents:
FROM eclipse-temurin:17
LABEL maintainer="[email protected]"
WORKDIR /app
COPY target/springboot-docker-demo-0.0.1-SNAPSHOT.jar /app/springboot-docker-demo.jar
ENTRYPOINT ["java","-jar","springboot-docker-demo.jar"]
Note: The application name springboot-docker-demo is used here as an example. Replace it with the actual name of your application and JAR file.
Step 2: Build the Docker Image
Navigate to the directory containing your Dockerfile using the terminal or command prompt. Run the following command to build the Docker image:
docker build -t springboot-docker-demo .
Step 3: Run the Docker Image in a Container
Run the following command to create and start a Docker container from the image:
docker run -p 8081:8080 springboot-docker-demo
To run your Dockerized Spring Boot application, use docker run -p 8081:8080 springboot-docker-demo to map port 8081 on your computer to port 8080 inside the Docker container. Access your application at https://localhost:8081after starting the container.
Step 4: Run the Container in Detached Mode
To run the container in the background, use the detached mode option (-d):
docker run -p 8081:8080 -d springboot-docker-demo
Step 5: View Logs of the Running Container
When you run a container in detached mode, Docker gives you a random container ID. You can use this ID to view the logs. For example, if your container ID is 3e562275b5c839602273933e325c459d8175d2cee679d6173062b672dc96ddbb, you can view the logs using:
领英推荐
docker logs -f 3e5
Here, 3e5 is the first three digits of the container ID.
Step 6: List Running Containers
To see which Docker containers are currently running, use:
docker ps
Step 7: Stop a Running Container
To stop a container that is running in the background, use:
docker stop 3e56
Here, 3e56 is the first four digits of the container ID.
Step 8: Push the Docker Image to Docker Hub
First, log in to your Docker Hub account from the terminal:
docker login
Step 9: Tag the Docker Image
Tag your local image with your Docker Hub repository name:
docker tag springboot-docker-demo nisanth2004/springboot-docker-demo:0.1.RELEASE
Step 10: Push the Image to Docker Hub
Finally, push the tagged image to Docker Hub:
docker push <dockerId>/springboot-docker-demo:0.1.RELEASE
Replace <dockerId> with your Docker ID or Docker Hub username. This command uploads your Docker image springboot-docker-demo tagged with 0.1.RELEASE to Docker Hub.
To pull an image from Docker Hub, use:
docker pull <dockerId>/springboot-docker-demo:0.1.RELEASE
This command retrieves the Docker image springboot-docker-demo with tag 0.1.RELEASE from Docker Hub to your local machine.
Conclusion:
Once you've Dockerized your Spring Boot application and shared it on Docker Hub, you'll see how straightforward it becomes. This process not only enhances your development skills but also makes your applications more portable and easier to manage in various environments. Enjoy Dockerizing!
Dock your Spring Boot app, share it on Docker Hub, and sail smoothly through development challenges with portability and ease!
Developer marketing manager
8 个月very informative