Introduction to Docker
Docker is a platform for developing, shipping, and running applications using containerization. It allows developers to package applications and all their dependencies into a standardized unit called a container.
What is Docker?
Docker is an open-source containerization platform. It allows you to package applications along with their dependencies, libraries, and configurations into containers. Containers are lightweight, portable, and consistent across different environments — development, testing, staging, and production.
Why use Docker?
Docker simplifies the process of application deployment, scaling, and management. It provides a consistent environment for applications, which helps eliminate the "it works on my machine" problem.
Getting Started with Docker
To start using Docker, you need to install Docker on your machine. You can download Docker Desktop for Windows or macOS, or install Docker Engine on Linux.
Key features of Docker containers include:
- Isolation: Each container runs in its own environment, ensuring that applications do not interfere with each other.
- Portability: Docker containers can run on any system that has Docker installed, making it easy to move applications between development, testing, and production environments.
- Scalability: Containers can be easily scaled up or down to handle varying loads, making them suitable for microservices architectures.
- Efficiency: Containers share the host OS kernel, which reduces overhead compared to traditional VMs, allowing for faster startup times and lower resource usage.
- Version Control: Docker images can be versioned, allowing developers to track changes and roll back to previous versions if needed.
Basic Commands
Here are some basic Docker commands to get you started:
docker run
: Run a container from an image.docker ps
: List running containers.docker images
: List available Docker images.docker pull
: Download an image from Docker Hub.docker build
: Build a Docker image from a Dockerfile.docker exec
: Execute a command in a running container.docker stop
: Stop a running container.docker rm
: Remove a stopped container.docker rmi
: Remove a Docker image.docker ps -a
: Displays all containers (both running and stopped).
How to Dockerize Spring Boot Application
To Dockerize a Spring Boot application, follow these steps:
- Create a Dockerfile in the root directory of your Spring Boot project.
- Write the Dockerfile to specify how to build the Docker image. Example Dockerfile:
- Build the Docker image using the command
docker build -t your-image-name .
- Run the Docker container using
docker run -p 8080:8080 your-image-name
- Access your Spring Boot application at
http://localhost:8080
.
FROM openjdk:11-jre COPY target/your-app.jar app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
Docker image push to Docker Hub
To push your Docker image to Docker Hub, follow these steps:
- Log in to Docker Hub using the command
docker login
. - Tag your image with your Docker Hub username:
docker tag your-image-name your-dockerhub-username/your-image-name
. - Push the image to Docker Hub:
docker push your-dockerhub-username/your-image-name
.
Example:
docker tag api-gateway-service:latest bjitpdmad/api-gateway-service docker push bjitpdmad/api-gateway-service
Docker pull image from Docker Hub
To pull your Docker image from Docker Hub, follow these steps:
- Log in to Docker Hub using the command
docker login
. - Pull the image from Docker Hub:
docker pull your-dockerhub-username/your-image-name
.
Example:
- Run the pulled image using
docker run -p 8080:8080 your-dockerhub-username/your-image-name
.
docker pull bjitpdmad/api-gateway-service
Example Workflow: Multi-Stage Build Debugging
When working with multi-stage builds in Docker, you might encounter issues that require debugging. Here’s a simple workflow to debug a multi-stage build:
- Start with a basic Dockerfile that uses multi-stage builds.
- Use the
docker build
command with the--target
option to build a specific stage. For example, if you have a stage named "builder", you can run:docker build --target builder -t my-app:builder .
docker build -t redis-build --target build .
- Run the container from the builder stage to inspect the environment:
docker run -it redis-build bash
By running the container interactively, you can check the contents of the build stage, verify that dependencies are installed correctly, and troubleshoot any issues.
Understanding the Command:
The command
docker run --rm -it my-app:builderdoes the following:
docker run
: This command is used to create and start a new container from a specified image.--rm
: This option automatically removes the container when it exits, keeping your system clean.-it
: This option allows you to run the container in interactive mode with a TTY, which is useful for debugging.my-app:builder
: This specifies the image to run, where "my-app" is the name of the image and "builder" is the tag or stage name.
By using this command, you can enter the container and interact with it, allowing you to debug issues related to the multi-stage build.
Example Multi-Stage Dockerfile:
# Stage 1: Builder FROM gradle:8.5-jdk21 AS builder WORKDIR /app COPY . . RUN gradle build # Stage 2: Runtime FROM openjdk:21-jdk-slim WORKDIR /app COPY --from=builder /app/build/libs/my-app.jar . CMD ["java", "-jar", "my-app.jar"]
This Dockerfile defines two stages: the first stage builds the application using Gradle, and the second stage runs the built application using OpenJDK.