home / skills / leavesfly / jimi / docker-setup

This skill guides Dockerized Java Spring Boot setup, using multi-stage builds and docker compose to streamline packaging, deployment, and environment

npx playbooks add skill leavesfly/jimi --skill docker-setup

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
1.2 KB
---
name: docker-setup
description: Docker 容器化配置指南
version: 1.0.0
category: devops
triggers:
  - docker
  - 容器化
  - dockerfile
  - docker compose
scriptPath: docker-check.sh
scriptType: bash
autoExecute: true
scriptTimeout: 10
---

# Docker 容器化技能包

## Dockerfile 最佳实践

### Java Spring Boot 应用

```dockerfile
# 多阶段构建
FROM maven:3.8-openjdk-17 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn clean package -DskipTests

FROM openjdk:17-jdk-slim
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
```

## Docker Compose

```yaml
version: '3.8'
services:
  app:
    build: .
    ports:
      - "8080:8080"
    environment:
      - SPRING_PROFILES_ACTIVE=prod
    depends_on:
      - mysql
  
  mysql:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=password
    volumes:
      - mysql_data:/var/lib/mysql

volumes:
  mysql_data:
```

## 常用命令

```bash
# 构建镜像
docker build -t myapp:latest .

# 运行容器
docker run -d -p 8080:8080 myapp:latest

# 查看日志
docker logs -f container_id

# 进入容器
docker exec -it container_id /bin/bash
```

Overview

This skill provides a practical Docker containerization guide focused on Java Spring Boot applications. It bundles a recommended multi-stage Dockerfile, a Docker Compose example for app+MySQL, and commonly used Docker commands. The content is aimed at making builds reproducible, images lean, and local deployments consistent.

How this skill works

The skill shows how to use a multi-stage Dockerfile: build the artifact with Maven in a build stage, then copy the produced JAR into a slim runtime image to keep final images small. It includes a Docker Compose file to orchestrate the application and a MySQL service with a persistent volume. Common CLI commands are provided for building images, running containers, inspecting logs, and entering containers for debugging.

When to use it

  • Packaging a Spring Boot application for consistent runtime across environments
  • Setting up a local development stack with application and database using Docker Compose
  • Optimizing CI pipelines by building artifacts in a separate build stage
  • Reducing final image size and attack surface for production deployments
  • Quickly reproducing bugs by running the exact image used in CI or production

Best practices

  • Use multi-stage builds: compile in a full image (Maven) and copy the artifact into a minimal JRE image
  • Expose only required ports and avoid running as root inside the container
  • Externalize configuration via environment variables or mounted config files; use secrets for credentials
  • Mount persistent volumes for stateful services like databases to avoid data loss
  • Add healthchecks and graceful shutdown support so orchestration tools can manage restarts properly
  • Keep images small by using slim base images and cleaning build caches

Example use cases

  • Local development: spin up app + MySQL with docker-compose and iterate quickly
  • CI pipeline: build artifact in a builder stage, run lightweight tests in the runtime image
  • Production deployment: publish the optimized runtime image to a registry and deploy to orchestrators
  • Debugging: run container locally, use docker logs and docker exec to inspect runtime issues
  • Migration: containerize database-dependent apps to standardize environments across teams

FAQ

How can I reduce the final image size?

Use multi-stage builds, choose slim or distroless base images, and avoid copying build caches or source files into the runtime image.

How do I persist database data across container restarts?

Declare and mount a named volume for the database service in docker-compose (or bind a host directory) so the database files live outside the container lifecycle.