Docker zero to advanced

Published: August 2025

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:

Basic Commands

Here are some basic Docker commands to get you started:

How to Dockerize Spring Boot Application

To Dockerize a Spring Boot application, follow these steps:

  1. Create a Dockerfile in the root directory of your Spring Boot project.
  2. Write the Dockerfile to specify how to build the Docker image.
  3. Example Dockerfile:
                    FROM openjdk:11-jre
                    COPY target/your-app.jar app.jar
                    ENTRYPOINT ["java", "-jar", "/app.jar"] 
                    
  4. Build the Docker image using the command docker build -t your-image-name .
  5. Run the Docker container using docker run -p 8080:8080 your-image-name
  6. Access your Spring Boot application at http://localhost:8080.

Docker image push to Docker Hub

To push your Docker image to Docker Hub, follow these steps:

  1. Log in to Docker Hub using the command docker login.
  2. Tag your image with your Docker Hub username: docker tag your-image-name your-dockerhub-username/your-image-name.
  3. Push the image to Docker Hub: docker push your-dockerhub-username/your-image-name.
  4. 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:

  1. Log in to Docker Hub using the command docker login.
  2. Pull the image from Docker Hub: docker pull your-dockerhub-username/your-image-name.
  3. Example:
                  docker pull bjitpdmad/api-gateway-service
                
  4. Run the pulled image using docker run -p 8080:8080 your-dockerhub-username/your-image-name.

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:

  1. Start with a basic Dockerfile that uses multi-stage builds.
  2. 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 .
  3. 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:builder
does the following:

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.