Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Zero-to-One Guide for Docker Orchestration and Deployment

Tech May 18 2

Prerequisites

This walkthrough assumes a Spring-Boot microservice already exists. Clone it to the server that will run the containers and make sure Docker and Docker Compose are installed.

Key Concepts in 60 Seconds

  • Dockerfile – recipe that producees an immutable image.
  • docker build – CLI command that turns the recipe into an image.
  • Compose file – YAML description of every container, network, and volume that makes up the whole system.

Multi-Stage Dockerfile for a Spring-Boot Service

# build stage
FROM maven:3.9-eclipse-temurin-17 AS build
WORKDIR /workspace
COPY pom.xml .
COPY src ./src
RUN mvn -B -q package -DskipTests

# runtime stage
FROM eclipse-temurin:17-jre-alpine
WORKDIR /app
COPY --from=build /workspace/target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

Save the file as Dockerfile in the repository root.

Building the Image

docker build -t backend:1.0.0 .

The -t flag assigns a name and tag; the trailing dot sets the build context to the current directory.

Private Registry Push

Log in to your registry (example uses Tencent Cloud Container Registry):

docker login ccr.ccs.tencentyun.com
docker tag backend:1.0.0 ccr.ccs.tencentyun.com/acme/backend:1.0.0
docker push ccr.ccs.tencentyun.com/acme/backend:1.0.0

Compose File for the Full Stack

version: "3.9"

services:
  api:
    image: ccr.ccs.tencentyun.com/acme/backend:1.0.0
    restart: unless-stopped
    ports:
      - "8080:8080"
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/appdb?useSSL=false&serverTimezone=UTC
      SPRING_DATASOURCE_USERNAME: app
      SPRING_DATASOURCE_PASSWORD: appsecret
    depends_on:
      mysql:
        condition: service_healthy

  mysql:
    image: mysql:8.0
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: rootpwd
      MYSQL_DATABASE: appdb
      MYSQL_USER: app
      MYSQL_PASSWORD: appsecret
    ports:
      - "3306:3306"
    volumes:
      - dbdata:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  dbdata:

Save the file as compose.yml.

Launch Everything

docker compose -f compose.yml up -d

Check the logs:

docker compose logs -f

When the api service logs Started Application in ... seconds, the stack is ready.

Quick Health Check

curl http://localhost:8080/actuator/health

A {"status":"UP"} response confirms the orchestration is working.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.