IDEA 中如何使用 Docker:从环境配置到 Spring Boot 容器化调试的深度实战

欢迎你来读这篇博客,这篇博客主要讲解如何在 IntelliJ IDEA 中深度使用 Docker。它不是一篇“点点菜单”的入门笔记,而是尽量从真实 Java 后端开发场景出发,把 IDEA、Docker、Docker Compose、Spring Boot、本地依赖服务、容器调试、私服镜像、常见坑和工程化最佳实践串起来。

序言

很多 Java 开发者刚开始用 Docker 时,会陷入两个极端:

  • 一个极端是只会在命令行里 docker rundocker compose up,IDEA 里的 Docker 功能完全不用;
  • 另一个极端是所有东西都想在 IDEA 图形界面里点出来,结果一出问题就不知道背后到底执行了什么 Docker 命令。

比较推荐的做法是:IDEA 负责提升开发体验,Docker CLI / Compose 文件负责沉淀工程标准。

也就是说,Dockerfile、compose.yml.env、启动脚本这些东西必须能脱离 IDEA 独立运行;IDEA 的价值是帮你更快地连接 Docker daemon、查看镜像和容器、启动依赖服务、调试容器中的 Java 程序、管理日志、执行容器命令,以及把“本地开发环境”和“真实运行环境”之间的鸿沟缩小。

一句话:别让 IDEA 变成 Docker 的黑盒,也别把 Docker 用成纯命令行苦力。两者配合起来,开发体验才会起飞。

版本与前置说明

本文写作时间为 2026-06-12,主要参考 IntelliJ IDEA 2026.1 Help、Docker 官方文档以及 Spring Boot 官方文档。

本文默认环境如下:

工具 推荐版本 / 说明
IntelliJ IDEA Ultimate 更完整;若看不到 Docker 功能,先检查 Docker 插件是否启用
Docker Desktop / Docker Engine Mac / Windows 建议 Docker Desktop;Linux 可直接使用 Docker Engine
Docker Compose 推荐使用 Compose V2,即 docker compose,不是老的 docker-compose
JDK JDK 17 / 21 均可,本文示例偏向 JDK 21
Spring Boot Spring Boot 3.x / 4.x 思路一致
Maven / Gradle 本文主要以 Maven 为例,Gradle 可类比

注意:IDEA 的 Docker 插件能力会随版本变化而调整。本文会给出“IDEA 界面操作 + 等价 CLI 命令 + 工程文件”的组合方式,这样就算菜单位置有轻微变化,你也不会迷路。

一、IDEA 中 Docker 能做什么?

在 IDEA 里使用 Docker,并不是只为了“看容器列表”。真正有价值的是下面这些能力:

flowchart LR
    A[IDEA Docker 能力] --> B[连接 Docker daemon]
    A --> C[管理镜像 Images]
    A --> D[管理容器 Containers]
    A --> E[运行 Dockerfile]
    A --> F[运行 Docker Compose]
    A --> G[容器日志与 Exec]
    A --> H[Java / Spring Boot 容器调试]
    A --> I[连接私有 Registry]
    A --> J[Run Target 容器化运行]

    E --> E1[Build Image]
    E --> E2[Run Container]
    F --> F1[启动多服务依赖]
    F --> F2[服务伸缩 / Stop / Down]
    H --> H1[Run Target Debug]
    H --> H2[Remote JVM Debug]

更具体地说,IDEA Docker 集成可以覆盖以下场景:

  1. 连接本机 Docker Desktop、Linux Docker Engine、远程 Docker daemon 或 Docker Context。
  2. Services 工具窗口中查看镜像、容器、Compose 服务、Registry。
  3. 从 Docker Hub 或私有镜像仓库拉取镜像。
  4. 基于 Dockerfile 构建镜像,并创建 Docker Run Configuration。
  5. 基于 Docker Compose 启动一组依赖服务,例如 PostgreSQL、Redis、Nacos、Kafka、Elasticsearch。
  6. 对运行中的容器执行 ExecInspectShow Processes、查看日志、查看文件系统。
  7. 将 Java / Spring Boot 的 Run Configuration 运行在 Docker 或 Docker Compose Run Target 中。
  8. 对容器中的 Java 程序进行 Debug。
  9. 推送镜像到私有仓库。
  10. 把 Docker 容器作为本地开发依赖环境,而不是把数据库、Redis、MQ 全塞进本机。

我的建议是:本地依赖服务用 Compose,应用本身用 Dockerfile / Run Target / Remote Debug,镜像发布用 CLI 或 CI,IDEA 负责日常调试与可视化管理。

二、安装与启用 Docker 支持

2.1 安装 Docker

Mac / Windows 推荐安装 Docker Desktop。Docker Desktop 会提供 Docker Engine、Docker CLI、Docker Compose 以及图形化管理能力。

安装后先在终端验证:

1
2
3
4
docker version
docker info
docker compose version
docker ps

如果 docker ps 能正常返回,说明本机 Docker daemon 已经可用。

Linux 环境下常见安装后校验:

1
2
3
4
5
sudo systemctl status docker
sudo systemctl enable docker
sudo systemctl start docker

docker version

如果 Linux 上普通用户执行 docker ps 报权限错误,一般是当前用户没有 Docker socket 访问权限。可以把用户加入 docker 组:

1
2
3
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

但是要注意,docker 组拥有近似 root 级别的能力,不能随便把不可信用户加入这个组。Docker 不是玩具车,是叉车,能搬东西,也能撞墙。

2.2 启用 IDEA Docker 插件

在 IDEA 中进入:

1
2
3
4
Settings / Preferences
-> Plugins
-> Installed
-> Docker

确认 Docker 插件已启用。

如果你使用的是 macOS,快捷键一般是:

1
Cmd + ,

Windows / Linux 一般是:

1
Ctrl + Alt + S

然后检查:

1
2
Build, Execution, Deployment
-> Docker

如果这里能看到 Docker 配置页面,说明 IDEA 已经识别到 Docker 支持。

JetBrains 官方文档说明,Docker 插件是 IDEA Docker 功能的基础,并且在 Ultimate 订阅中通常默认可用。如果你看不到相关功能,优先检查插件是否被禁用。

三、连接 Docker Daemon

IDEA 操作 Docker 的前提是:IDEA 必须能连接到 Docker daemon。

进入:

1
2
3
Settings / Preferences
-> Build, Execution, Deployment
-> Docker

点击 + 添加 Docker 连接。

常见连接方式有几种。

3.1 Docker Desktop for Mac / Windows

如果你使用 Docker Desktop,一般选择默认连接即可。

配置成功后,IDEA 底部会出现类似:

1
Connection successful

然后打开:

1
View -> Tool Windows -> Services

或者使用快捷键:

1
Alt + 8

在 Services 窗口中应该可以看到 Docker 节点。

3.2 Linux 本地 Docker Engine

Linux 本机 Docker 通常通过 Unix socket 连接:

1
unix:///var/run/docker.sock

如果 IDEA 连接失败,先在终端确认:

1
2
ls -l /var/run/docker.sock
docker ps

如果命令行都无法访问,IDEA 肯定也连不上。不要急着怀疑 IDEA,先把 Docker 本身跑通。

3.3 Colima / Rancher Desktop

在 macOS 上,如果不想使用 Docker Desktop,也可以使用 Colima 或 Rancher Desktop。

以 Colima 为例:

1
2
3
4
5
brew install colima docker docker-compose
colima start

docker context ls
docker ps

然后在 IDEA 的 Docker 配置中选择对应 Docker Context 或 socket。

常见 Colima socket 路径类似:

1
unix:///Users/<your-user>/.colima/default/docker.sock

不同机器路径可能不同,以 docker context inspect 输出为准。

3.4 Docker Context

Docker Context 用来在多个 Docker daemon 之间切换,例如:

1
2
3
docker context ls
docker context use default
docker context inspect default

典型场景:

1
2
3
本机 Docker Desktop
远程测试服务器 Docker Engine
远程 CI 构建机 Docker Engine

如果你在 IDEA 中选择 Docker Context,记住一个原则:IDEA 看到的是 Docker daemon 所在机器的文件系统,而不是永远看到你本机的文件系统。

这点在 bind mount 场景非常容易踩坑,后面会专门讲。

3.5 远程 Docker Daemon

远程 Docker daemon 可以通过 SSH 或 TCP/TLS 连接。

推荐优先使用 SSH 或 Docker Context,不建议裸露 Docker TCP socket。原因很简单:Docker daemon 权限非常高,一旦暴露不当,基本等于把服务器钥匙挂门口,还贴了个“请自取”。

如果确实要远程连接,至少要做到:

  1. 不使用未加密 TCP。
  2. 配置 TLS 证书。
  3. 只允许内网访问。
  4. 使用防火墙限制来源 IP。
  5. 不把 Docker socket 挂载给不可信容器。
  6. 不在生产机器上随意让本地 IDEA 直接操作 Docker。

开发机可以方便,生产机必须克制。

四、认识 IDEA 的 Services 工具窗口

IDEA 现在很多运行时资源都聚合在 Services 工具窗口里,包括:

  • Run / Debug Configuration
  • Docker
  • Docker Compose
  • Database
  • Application Server
  • Kubernetes
  • 其他插件提供的服务

打开方式:

1
View -> Tool Windows -> Services

快捷键:

1
Alt + 8

连接 Docker 后,你通常会看到类似结构:

1
2
3
4
5
6
7
8
Services
└── Docker
└── Docker Desktop / default
├── Containers
├── Images
├── Networks
├── Volumes
└── Docker Compose

4.1 Images 节点

Images 节点可以用来:

  • 查看本地镜像;
  • 拉取镜像;
  • 删除镜像;
  • 基于镜像创建容器;
  • 查看镜像详情;
  • 查看镜像 layer;
  • 推送镜像到 Registry。

比如拉取 PostgreSQL 镜像:

1
2
3
Services -> Docker -> Images
-> 在 Images Console 输入 postgres:16
-> Ctrl + Enter

等价 CLI:

1
docker pull postgres:16

4.2 Containers 节点

Containers 节点可以用来:

  • 启动 / 停止容器;
  • 查看容器日志;
  • 进入容器执行命令;
  • Inspect 容器;
  • 查看容器进程;
  • 查看端口映射;
  • 查看环境变量;
  • 查看 volume 绑定;
  • 查看容器文件系统。

常用操作:

1
2
3
4
5
右键容器 -> Exec
右键容器 -> Inspect
右键容器 -> Show Processes
右键容器 -> Attach
右键容器 -> Show Files

等价 CLI:

1
2
3
4
docker exec -it <container> /bin/bash
docker inspect <container>
docker top <container>
docker logs -f <container>

4.3 Docker Compose 节点

Docker Compose 启动的容器在 IDEA 中会归到 Compose 节点下,而不是简单地混在普通 Containers 里。

这非常适合管理本地依赖环境,例如:

1
2
3
4
5
project-local
├── app
├── postgres
├── redis
└── nacos

你可以在 IDEA 中:

  • 启动某个 Compose 配置;
  • 停止某个 service;
  • Down 整个 Compose project;
  • 查看每个 service 的日志;
  • scale 某个 service;
  • 跳回对应的 compose.yml 文件。

五、实战一:在 IDEA 中启动一个 PostgreSQL 容器

先来一个最贴近后端开发的例子:用 Docker 启动 PostgreSQL,作为本地开发数据库。

5.1 命令行写法

如果用 CLI,我们通常会这样写:

1
2
3
4
5
6
7
8
docker run -d \
--name local-postgres \
-e POSTGRES_DB=finance \
-e POSTGRES_USER=finance \
-e POSTGRES_PASSWORD=finance123 \
-p 5432:5432 \
-v local-postgres-data:/var/lib/postgresql/data \
postgres:16

验证:

1
docker ps

连接:

1
psql -h localhost -p 5432 -U finance -d finance

5.2 IDEA 图形界面写法

在 IDEA 中可以这样做:

1
2
3
4
Services -> Docker -> Images
-> Pull postgres:16
-> 右键 postgres:16
-> Create Container

配置容器参数:

配置项 示例
Container name local-postgres
Environment variables POSTGRES_DB=financePOSTGRES_USER=financePOSTGRES_PASSWORD=finance123
Ports 5432:5432
Volumes local-postgres-data:/var/lib/postgresql/data
Run options 按需添加

运行后,IDEA 会创建一个 Docker Image Run Configuration。以后你可以直接在 Run Configuration 中启动它。

5.3 用 IDEA Database 连接容器数据库

打开:

1
View -> Tool Windows -> Database

添加 PostgreSQL 数据源:

Host localhost
Port 5432
User finance
Password finance123
Database finance

这样你就可以在 IDEA 里同时管理:

1
代码 + Docker 容器 + 数据库连接 + SQL Console

这个体验对 Java 后端非常舒服,尤其是写本地联调、查数据、看日志的时候。

六、实战二:用 Docker Compose 管理本地依赖服务

单个容器可以用 docker run,但是后端项目一般不止一个依赖。

比如一个 Spring Boot 服务可能依赖:

  • PostgreSQL
  • Redis
  • Nacos
  • Kafka
  • Elasticsearch

这时不要在 IDEA 中手动点 5 个容器。更推荐写 compose.yml,然后让 IDEA 读取它。

6.1 推荐项目结构

1
2
3
4
5
6
7
8
9
10
finance-demo/
├── docker/
│ ├── compose.yml
│ ├── compose.override.yml
│ ├── .env
│ └── init-sql/
│ └── 001_init.sql
├── src/
├── pom.xml
└── README.md

6.2 编写 compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
name: finance-demo

services:
postgres:
image: postgres:16
container_name: finance-postgres
restart: unless-stopped
environment:
POSTGRES_DB: finance
POSTGRES_USER: finance
POSTGRES_PASSWORD: finance123
TZ: Asia/Shanghai
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
- ./init-sql:/docker-entrypoint-initdb.d:ro
healthcheck:
test: ["CMD-SHELL", "pg_isready -U finance -d finance"]
interval: 10s
timeout: 5s
retries: 5
networks:
- finance-net

redis:
image: redis:7.4
container_name: finance-redis
restart: unless-stopped
command: ["redis-server", "--appendonly", "yes"]
ports:
- "6379:6379"
volumes:
- redis-data:/data
networks:
- finance-net

networks:
finance-net:
driver: bridge

volumes:
postgres-data:
redis-data:

6.3 在 IDEA 中运行 Compose

方式一:右键 compose.yml

1
2
右键 docker/compose.yml
-> Run 'docker/compose.yml'

方式二:创建 Run Configuration。

1
2
3
4
Run -> Edit Configurations
-> +
-> Docker
-> Docker Compose

配置:

配置项 示例
Name Local Infra
Compose files docker/compose.yml
Services postgres, redis
Environment variables 按需配置
Before launch 可选

等价 CLI:

1
2
cd docker
docker compose up -d

停止:

1
docker compose stop

销毁容器和网络:

1
docker compose down

连 volume 一起删:

1
docker compose down -v

开发时,down -v 要谨慎。你以为你删的是容器,其实顺手把数据库数据也扬了。Docker 不会骂你,它只会非常高效地执行你的错误命令。

6.4 Spring Boot 连接本地容器

application-local.yml

1
2
3
4
5
6
7
8
9
10
11
spring:
datasource:
url: jdbc:postgresql://localhost:5432/finance
username: finance
password: finance123
driver-class-name: org.postgresql.Driver

data:
redis:
host: localhost
port: 6379

本地启动时:

1
./mvnw spring-boot:run -Dspring-boot.run.profiles=local

或者在 IDEA 的 Spring Boot Run Configuration 中配置:

1
Active profiles: local

6.5 把 Compose 加到 Before Launch

很多时候,你希望点击 Spring Boot 的 Run 按钮时,IDEA 自动先启动 PostgreSQL 和 Redis。

在 Spring Boot Run Configuration 中:

1
2
3
4
Modify options
-> Add before launch task
-> Run Another Configuration
-> 选择 Local Infra

这样流程就变成:

sequenceDiagram
    participant Dev as Developer
    participant IDEA as IntelliJ IDEA
    participant Compose as Docker Compose
    participant PG as PostgreSQL
    participant Redis as Redis
    participant App as Spring Boot App

    Dev->>IDEA: 点击 Run DemoApplication
    IDEA->>Compose: Before Launch 启动 Local Infra
    Compose->>PG: 启动 postgres
    Compose->>Redis: 启动 redis
    PG-->>Compose: healthcheck ready
    Redis-->>Compose: ready
    IDEA->>App: 启动 Spring Boot local profile
    App->>PG: JDBC 连接
    App->>Redis: Redis 连接

这对本地开发很舒服:不用每次手动启动依赖,也不会把本机环境装得乱七八糟。

七、实战三:用 Dockerfile 容器化 Spring Boot 应用

上面我们只是用 Docker 管理依赖服务。接下来把 Spring Boot 应用本身也容器化。

7.1 示例 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
public class HealthController {

@GetMapping("/api/health")
public Map<String, Object> health() {
return Map.of(
"status", "UP",
"time", LocalDateTime.now().toString()
);
}
}

7.2 Maven 打包

1
./mvnw clean package -DskipTests

生成:

1
target/demo-0.0.1-SNAPSHOT.jar

7.3 编写 Dockerfile:基础版

项目根目录创建 Dockerfile

1
2
3
4
5
6
7
8
9
10
FROM eclipse-temurin:21-jre

WORKDIR /app

ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/app/app.jar"]

构建:

1
docker build -t finance-demo:0.0.1 .

运行:

1
2
3
4
docker run -d \
--name finance-demo \
-p 8080:8080 \
finance-demo:0.0.1

验证:

1
curl http://localhost:8080/api/health

7.4 编写 Dockerfile:更适合工程实践的版本

基础版够简单,但不是很工程化。一个稍微像样点的版本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
FROM eclipse-temurin:21-jre AS runtime

LABEL maintainer="your-team@example.com"
LABEL app="finance-demo"

WORKDIR /app

ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar

ENV TZ=Asia/Shanghai \
JAVA_OPTS="-XX:MaxRAMPercentage=75.0 -XX:+ExitOnOutOfMemoryError" \
SPRING_PROFILES_ACTIVE=prod

EXPOSE 8080

RUN addgroup --system app && adduser --system --ingroup app app
USER app:app

ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar /app/app.jar"]

几点说明:

  1. 使用 JRE 镜像,而不是 JDK 镜像,减小运行时镜像体积。
  2. 使用非 root 用户运行应用。
  3. 通过 JAVA_OPTS 控制 JVM 参数。
  4. 通过 SPRING_PROFILES_ACTIVE 控制环境。
  5. MaxRAMPercentage 更适合容器环境下的内存限制。

7.5 添加 .dockerignore

项目根目录添加 .dockerignore

1
2
3
4
5
6
7
8
9
10
11
12
13
14
.git
.idea
*.iml
.DS_Store

.mvn/wrapper/maven-wrapper.jar
.mvn/wrapper/MavenWrapperDownloader.java

logs
/tmp

src/test
**/target/*
!target/*.jar

没有 .dockerignore 的项目,构建上下文可能会非常大。你以为 Docker 在认真构建镜像,其实它可能先在默默搬你整个 .gittarget 垃圾堆。

7.6 在 IDEA 中运行 Dockerfile

打开 Dockerfile,左侧 gutter 通常会出现 Docker 图标。

可以选择:

1
2
Run on Docker
-> Build Image

或者:

1
2
Run on Docker
-> New Run Configuration

重点配置:

配置项 建议值
Dockerfile 项目根目录 Dockerfile
Context folder 项目根目录
Image tag finance-demo:0.0.1
Container name finance-demo
Bind ports 8080:8080
Environment variables SPRING_PROFILES_ACTIVE=local
Run options 按需增加 --network--memory

IDEA 会创建一个 Dockerfile Run Configuration,底层相当于执行:

1
2
docker build -t finance-demo:0.0.1 .
docker run --name finance-demo -p 8080:8080 finance-demo:0.0.1

不同点在于 IDEA 会把构建日志、容器日志、容器信息集成到 Services 窗口中。

八、实战四:Spring Boot + PostgreSQL + Redis 全量 Compose

如果要把应用和依赖都交给 Compose 管理,可以这样写。

8.1 完整 compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
name: finance-demo

services:
app:
build:
context: ..
dockerfile: Dockerfile
image: finance-demo:0.0.1
container_name: finance-demo-app
restart: unless-stopped
environment:
SPRING_PROFILES_ACTIVE: docker
JAVA_OPTS: "-XX:MaxRAMPercentage=75.0 -XX:+ExitOnOutOfMemoryError"
DB_HOST: postgres
DB_PORT: 5432
DB_NAME: finance
DB_USERNAME: finance
DB_PASSWORD: finance123
REDIS_HOST: redis
REDIS_PORT: 6379
ports:
- "8080:8080"
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_started
networks:
- finance-net

postgres:
image: postgres:16
container_name: finance-postgres
restart: unless-stopped
environment:
POSTGRES_DB: finance
POSTGRES_USER: finance
POSTGRES_PASSWORD: finance123
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
- ./init-sql:/docker-entrypoint-initdb.d:ro
healthcheck:
test: ["CMD-SHELL", "pg_isready -U finance -d finance"]
interval: 10s
timeout: 5s
retries: 5
networks:
- finance-net

redis:
image: redis:7.4
container_name: finance-redis
restart: unless-stopped
command: ["redis-server", "--appendonly", "yes"]
ports:
- "6379:6379"
volumes:
- redis-data:/data
networks:
- finance-net

networks:
finance-net:
driver: bridge

volumes:
postgres-data:
redis-data:

8.2 application-docker.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8080

spring:
datasource:
url: jdbc:postgresql://${DB_HOST:postgres}:${DB_PORT:5432}/${DB_NAME:finance}
username: ${DB_USERNAME:finance}
password: ${DB_PASSWORD:finance123}
driver-class-name: org.postgresql.Driver

data:
redis:
host: ${REDIS_HOST:redis}
port: ${REDIS_PORT:6379}

注意,在容器内部连接 PostgreSQL 时,不应该写:

1
jdbc:postgresql://localhost:5432/finance

因为在 app 容器内部,localhost 指的是 app 容器自己,而不是 postgres 容器。

正确写法是使用 Compose service name:

1
jdbc:postgresql://postgres:5432/finance

这个坑非常经典,基本每个 Docker 初学者都会踩一次。踩过之后你就懂了:容器世界里的 localhost,有时候是个小骗子。

8.3 IDEA 中启动全量 Compose

创建 Docker Compose Run Configuration:

1
2
3
4
Run -> Edit Configurations
-> +
-> Docker
-> Docker Compose

配置:

配置项
Name Finance Demo Compose
Compose files docker/compose.yml
Services app, postgres, redis
Build 勾选 build 或使用 --build

启动后可在 Services 中看到:

1
2
3
4
Docker Compose: finance-demo
├── app
├── postgres
└── redis

可以分别查看日志:

1
2
3
app -> Logs
postgres -> Logs
redis -> Logs

也可以右键 finance-demo

1
2
3
Stop
Down
Jump to Source

九、实战五:在 IDEA 中调试 Docker 里的 Java 应用

IDEA 调试容器中的 Java 应用主要有两种方式:

  1. 使用 IDEA 的 Docker / Docker Compose Run Target。
  2. 使用 Remote JVM Debug 手动 attach。

两种都要掌握。

9.1 方式一:Docker Run Target

适合场景:

  • 你想像本地运行一样点击 Run / Debug;
  • 代码仍在本机;
  • IDEA 负责把构建产物复制到容器中运行;
  • 你希望运行环境来自指定 Docker 镜像。

操作路径:

1
2
3
4
Run -> Edit Configurations
-> 选择你的 Spring Boot / Application 配置
-> Run on
-> Docker

如果没有现成 target,可以选择:

1
2
3
Manage targets
-> +
-> Docker

配置时需要关注:

配置项 说明
Docker server 选择本机或远程 Docker daemon
Image 可以 pull 现有镜像,也可以从 Dockerfile 构建
Dockerfile 如果要构建镜像,需要指定 Dockerfile
Context folder Docker build context
Java runtime IDEA 会尝试自动识别容器内 JDK/JRE
Maven / Gradle runtime 如果要在容器内构建,需要容器内有 Maven / Gradle

启动时,IDEA 会:

sequenceDiagram
    participant IDEA as IntelliJ IDEA
    participant Docker as Docker Daemon
    participant C as Container
    participant JVM as JVM

    IDEA->>Docker: 准备 Docker Run Target
    Docker->>C: 创建/启动容器
    IDEA->>C: 拷贝构建产物或源码
    IDEA->>JVM: 使用容器内 Java Runtime 启动应用
    IDEA->>JVM: Debug 时建立调试连接

这种方式的优点是集成体验好,缺点是对 IDEA 配置依赖更强。团队协作时,不能只靠这个方式,还要保留 Dockerfile / Compose 的命令行可运行能力。

9.2 方式二:Docker Compose Run Target

适合场景:

  • 你的应用必须和数据库、Redis 等服务在同一个 Compose 网络里;
  • 希望 Spring Boot 应用跑在 Compose 的 app service 中;
  • 想直接在 IDEA 里 Debug 容器中的 Spring Boot。

操作路径:

1
2
3
4
Run -> Edit Configurations
-> 选择 Spring Boot 配置
-> Run on
-> Docker Compose

然后选择:

1
2
compose.yml
service: app

IDEA 会对 service 进行 introspection,识别容器内 Java runtime。

如果识别失败,通常是因为:

  1. 镜像里没有 JDK / JRE;
  2. 镜像太精简,缺少必要命令;
  3. ENTRYPOINT / CMD 过于特殊;
  4. 架构不匹配,例如 arm64 / amd64;
  5. 容器启动后立刻退出。

9.3 方式三:Remote JVM Debug

这是最通用、也最值得掌握的方式。

在容器中启动 Java 时加上 JDWP 参数:

1
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

Dockerfile 可以这样写:

1
2
3
4
5
6
7
8
9
10
11
FROM eclipse-temurin:21-jre

WORKDIR /app
COPY target/*.jar app.jar

ENV JAVA_OPTS=""

EXPOSE 8080
EXPOSE 5005

ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar /app/app.jar"]

Compose 中添加:

1
2
3
4
5
6
7
8
9
services:
app:
image: finance-demo:0.0.1
ports:
- "8080:8080"
- "5005:5005"
environment:
JAVA_OPTS: >-
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

启动:

1
docker compose up -d --build

IDEA 中创建 Remote JVM Debug:

1
2
3
Run -> Edit Configurations
-> +
-> Remote JVM Debug

配置:

配置项
Host localhost
Port 5005
Module classpath 选择你的应用 module

然后点击 Debug,访问接口:

1
curl http://localhost:8080/api/health

断点命中后,你就可以像本地调试一样查看变量、调用栈、表达式。

9.4 suspend=y 和 suspend=n 的区别

1
2
suspend=n:应用正常启动,调试器后连上
suspend=y:应用启动时等待调试器连接,连上后才继续执行

开发环境如果要调试启动阶段问题,可以用:

1
-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005

否则建议:

1
suspend=n

不然容器看起来像“卡死”了,其实它只是在等你连接调试器。别冤枉它,它只是太听话。

9.5 容器调试常见问题

问题 原因 解决
IDEA 连不上 5005 端口没映射 Compose 添加 5005:5005
断点不命中 镜像里的代码版本和本地源码不一致 重新 build 镜像,确认 git commit 一致
断点显示灰色 classpath / module 选择错误 Remote Debug 里选择正确 module
容器启动后退出 Java 参数错误或配置缺失 看容器日志
Mac 上端口冲突 本机已有服务占用 lsof -i :5005 / 修改端口
容器内连不上数据库 写了 localhost 使用 Compose service name

十、实战六:使用 Spring Boot build-image 构建镜像

除了 Dockerfile,Spring Boot 还支持通过 Cloud Native Buildpacks 构建 OCI 镜像。

Maven:

1
./mvnw spring-boot:build-image

指定镜像名:

1
2
./mvnw spring-boot:build-image \
-Dspring-boot.build-image.imageName=finance-demo:0.0.1

Gradle:

1
./gradlew bootBuildImage

优点:

  1. 不需要手写 Dockerfile。
  2. 默认会生成比较规范的镜像。
  3. 通常会使用非 root 用户运行。
  4. 和 Spring Boot 打包体系集成好。

缺点:

  1. 定制系统包不如 Dockerfile 直观。
  2. 构建过程依赖 Docker daemon。
  3. 网络环境不好时拉 builder image 可能慢。
  4. 对底层镜像、启动脚本的掌控感弱一些。

推荐选择:

场景 推荐方案
快速生成 Spring Boot 镜像 spring-boot:build-image
需要精细控制系统依赖、字体、时区、证书 Dockerfile
生产镜像规范统一 Dockerfile + CI 模板
团队不熟 Dockerfile build-image 起步,然后逐步标准化

在 IDEA 中可以通过 Maven 工具窗口执行:

1
Maven -> Plugins -> spring-boot -> spring-boot:build-image

或者在 Terminal 里执行命令。

构建完成后,回到 Services 的 Images 节点即可看到生成的镜像。

十一、私有镜像仓库 Registry 的使用

企业里基本不会只用 Docker Hub,常见私服包括:

  • Harbor
  • Nexus Repository
  • GitLab Container Registry
  • 阿里云 ACR
  • AWS ECR
  • GitHub Container Registry

11.1 IDEA 中配置 Registry

在 Services 窗口中:

1
2
3
Services
-> Add Service
-> Docker Registry

填写:

配置项 示例
Registry https://harbor.example.com
Username your-name
Password / Token 私服密码或 token

连接成功后,可以在 IDEA 中浏览 Registry 镜像,并执行 Pull / Push。

11.2 CLI 登录私服

1
docker login harbor.example.com

打 tag:

1
docker tag finance-demo:0.0.1 harbor.example.com/backend/finance-demo:0.0.1

推送:

1
docker push harbor.example.com/backend/finance-demo:0.0.1

拉取:

1
docker pull harbor.example.com/backend/finance-demo:0.0.1

11.3 镜像命名规范建议

1
<registry>/<namespace>/<app-name>:<version>

示例:

1
2
3
harbor.example.com/finance/settlement-service:3.1.23
harbor.example.com/finance/settlement-service:3.1.23-20260612-001
harbor.example.com/finance/settlement-service:git-a1b2c3d

不建议生产环境长期使用:

1
latest

latest 最大的问题是不可追踪。今天的 latest 和明天的 latest 可能不是一个东西,出了问题追起来很痛苦。

十二、IDEA + Docker 的推荐工程结构

一个比较舒服的后端项目结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
settlement-service/
├── .dockerignore
├── Dockerfile
├── docker/
│ ├── compose.yml
│ ├── compose.dev.yml
│ ├── compose.test.yml
│ ├── .env.example
│ ├── init-sql/
│ │ └── 001_init.sql
│ └── README.md
├── scripts/
│ ├── docker-build.sh
│ ├── docker-run.sh
│ └── docker-clean.sh
├── src/
│ ├── main/
│ └── test/
├── pom.xml
└── README.md

12.1 docker-build.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env bash
set -euo pipefail

APP_NAME="settlement-service"
VERSION="${1:-0.0.1-SNAPSHOT}"
IMAGE="${APP_NAME}:${VERSION}"

echo "Building jar..."
./mvnw clean package -DskipTests

echo "Building image: ${IMAGE}"
docker build -t "${IMAGE}" .

echo "Done: ${IMAGE}"

12.2 docker-run.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env bash
set -euo pipefail

APP_NAME="settlement-service"
VERSION="${1:-0.0.1-SNAPSHOT}"
IMAGE="${APP_NAME}:${VERSION}"
CONTAINER_NAME="${APP_NAME}"

docker rm -f "${CONTAINER_NAME}" 2>/dev/null || true

docker run -d \
--name "${CONTAINER_NAME}" \
-p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=local \
"${IMAGE}"

docker logs -f "${CONTAINER_NAME}"

12.3 docker-clean.sh

1
2
3
4
5
6
#!/usr/bin/env bash
set -euo pipefail

docker container prune -f
docker image prune -f
docker volume prune -f

清理脚本不要在生产机器乱跑,尤其是 volume prune。本地开发环境可以用,生产环境请三思再三思。

12.4 IDEA Run Configuration 如何配合脚本

可以在 IDEA 中配置:

1
2
3
Run -> Edit Configurations
-> +
-> Shell Script

然后让 IDEA 调用:

1
2
scripts/docker-build.sh 0.0.1-SNAPSHOT
scripts/docker-run.sh 0.0.1-SNAPSHOT

这样既保留了 IDEA 一键运行的体验,又不会把逻辑锁死在 IDEA 配置里。

十三、开发环境与生产环境要分开

Docker Compose 很适合开发环境,但不要把开发用 Compose 原封不动搬到生产。

13.1 开发环境 compose.dev.yml

1
2
3
4
5
6
7
8
9
services:
app:
environment:
SPRING_PROFILES_ACTIVE: dev
ports:
- "8080:8080"
- "5005:5005"
volumes:
- ../logs:/app/logs

13.2 测试环境 compose.test.yml

1
2
3
4
5
6
services:
app:
environment:
SPRING_PROFILES_ACTIVE: test
ports:
- "18080:8080"

13.3 合并运行

1
2
3
4
docker compose \
-f docker/compose.yml \
-f docker/compose.dev.yml \
up -d --build

IDEA Docker Compose Run Configuration 也可以指定多个 Compose 文件。

建议:

文件 用途
compose.yml 通用基础定义
compose.dev.yml 本地开发覆盖配置
compose.test.yml 测试环境覆盖配置
.env.example 环境变量模板
.env 本地真实变量,不提交 Git

十四、容器日志、Exec、Inspect 的高频用法

14.1 查看日志

IDEA:

1
Services -> 选择容器 -> Logs

CLI:

1
docker logs -f finance-demo-app

只看最近 200 行:

1
docker logs --tail=200 -f finance-demo-app

14.2 进入容器

IDEA:

1
右键容器 -> Exec -> /bin/bash

CLI:

1
docker exec -it finance-demo-app /bin/bash

如果镜像没有 bash:

1
docker exec -it finance-demo-app /bin/sh

14.3 查看环境变量

1
docker exec -it finance-demo-app env

14.4 查看容器 IP

1
docker inspect finance-demo-app | grep IPAddress

14.5 查看进程

1
docker top finance-demo-app

14.6 查看端口映射

1
docker port finance-demo-app

这些操作 IDEA 都可以点出来,但建议你也知道背后的命令。会用图形界面是效率,会用命令行是底气。

十五、Testcontainers:更适合自动化测试的 Docker 用法

本地开发依赖可以用 Docker Compose,但自动化测试更推荐 Testcontainers。

典型 Maven 依赖:

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
@SpringBootTest
class DemoApplicationTests {

@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16");

@Test
void contextLoads() {
}
}

运行测试时,Testcontainers 会自动启动临时 PostgreSQL 容器,测试结束后清理。

IDEA 中运行 JUnit 测试即可。如果 Docker daemon 可用,测试会自动拉起容器。

适用建议:

场景 推荐
本地长期依赖服务 Docker Compose
单元测试 Mock / Fake
集成测试 Testcontainers
端到端联调 Compose / 测试环境

十六、常见问题与排查

16.1 IDEA 看不到 Docker 菜单

排查:

  1. Docker 插件是否启用。
  2. IDEA 版本是否支持完整 Docker 集成。
  3. 是否使用 Ultimate 版本。
  4. 是否重启 IDEA。
  5. 是否在 Settings 中能看到 Docker 配置。

16.2 Connection failed

先执行:

1
docker ps

如果 CLI 不通,先修 Docker。

Mac / Windows:

1
2
3
确认 Docker Desktop 正在运行
确认 Docker Desktop 没有卡在 starting
确认当前 Docker Context 正确

Linux:

1
2
3
sudo systemctl status docker
ls -l /var/run/docker.sock
groups

16.3 Cannot connect to the Docker daemon

常见原因:

  • Docker Desktop 没启动;
  • Docker Engine 没启动;
  • 当前用户无权限;
  • Docker Context 指向了不存在的 daemon;
  • Colima / Rancher Desktop 未启动;
  • IDEA 使用的 socket 和 CLI 使用的 socket 不一致。

排查命令:

1
2
3
4
5
docker context ls
docker context inspect
echo $DOCKER_HOST
echo $DOCKER_CONTEXT
docker info

16.4 Bind mount 找不到文件

如果使用远程 Docker daemon,要牢记:bind mount 绑定的是 daemon 所在机器的路径,不是 IDEA 所在机器的路径。

例如你在本机 IDEA 中配置:

1
/Users/mario/project:/app

但 Docker daemon 在远程 Linux 服务器上,它会在远程服务器找:

1
/Users/mario/project

结果当然找不到。

解决方案:

  1. 本地开发使用本机 Docker daemon。
  2. 远程构建使用 Git 拉代码,不依赖本机 bind mount。
  3. 使用 SSH target 或 CI 构建。
  4. 不要混淆 Docker client 和 Docker daemon 的文件系统。

16.5 Mac / Windows volume 映射失败

Docker Desktop 的 daemon 实际运行在 Linux VM 中。虽然它会帮你做路径共享,但不是所有路径都默认可访问。

解决:

1
Docker Desktop -> Settings -> Resources -> File Sharing

把项目目录加入共享路径。

16.6 端口被占用

1
2
3
lsof -i :8080
lsof -i :5432
lsof -i :6379

杀进程:

1
kill -9 <pid>

或者改 Compose 端口:

1
2
ports:
- "15432:5432"

这表示:

1
宿主机 15432 -> 容器 5432

Spring Boot 本机连接时写:

1
jdbc:postgresql://localhost:15432/finance

容器内连接时仍然写:

1
jdbc:postgresql://postgres:5432/finance

16.7 容器内访问宿主机服务

Mac / Windows Docker Desktop 通常可以使用:

1
host.docker.internal

例如容器内访问宿主机 8080:

1
http://host.docker.internal:8080

Linux 上不一定默认支持,需要额外配置,例如:

1
2
extra_hosts:
- "host.docker.internal:host-gateway"

16.8 镜像构建很慢

优化建议:

  1. 添加 .dockerignore
  2. 把变化少的层放前面。
  3. 依赖下载层和业务代码层拆开。
  4. 使用 BuildKit。
  5. 不要把整个项目无脑 COPY 到镜像里。
  6. 本地 Maven 仓库用缓存策略。

一个多阶段构建示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM maven:3.9.9-eclipse-temurin-21 AS build
WORKDIR /workspace

COPY pom.xml .
COPY .mvn .mvn
COPY mvnw .
RUN ./mvnw -q -DskipTests dependency:go-offline

COPY src src
RUN ./mvnw -q -DskipTests package

FROM eclipse-temurin:21-jre
WORKDIR /app
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

16.9 容器日志中文乱码

优先设置容器环境变量:

1
2
3
4
environment:
LANG: C.UTF-8
LC_ALL: C.UTF-8
JAVA_TOOL_OPTIONS: "-Dfile.encoding=UTF-8"

Spring Boot 日志也建议统一 UTF-8。

16.10 数据库初始化 SQL 不执行

PostgreSQL 官方镜像只会在数据目录第一次初始化时执行:

1
/docker-entrypoint-initdb.d

如果 volume 已经存在,后面再改 SQL 不会重新执行。

解决:

1
2
docker compose down -v
docker compose up -d

注意,这会删除数据库 volume。

16.11 Compose 修改后不生效

尝试:

1
docker compose up -d --build --force-recreate

或者在 IDEA Compose 节点中执行 Down 后重新 Run。

16.12 IDEA 启动的容器和命令行看到的不一致

常见原因:

  1. IDEA 连接的是另一个 Docker Context。
  2. IDEA 使用的是 Colima socket,命令行使用 Docker Desktop。
  3. 当前项目设置里 Docker server 选错。
  4. 环境变量 DOCKER_HOST 影响了 CLI。

排查:

1
2
docker context ls
docker info | grep -i context

然后检查 IDEA:

1
Settings -> Build, Execution, Deployment -> Docker

十七、安全与规范:别把开发便利变成生产事故

Docker 在开发环境很方便,但安全边界必须清楚。

17.1 不要随便暴露 Docker socket

危险写法:

1
2
volumes:
- /var/run/docker.sock:/var/run/docker.sock

这会让容器拥有操作宿主机 Docker daemon 的能力。如果容器被攻破,宿主机也可能被攻破。

除非你非常清楚自己在做什么,否则不要这样挂。

17.2 不要把密钥写进镜像

错误示例:

1
2
ENV DB_PASSWORD=real-prod-password
ENV AWS_SECRET_ACCESS_KEY=xxxx

正确做法:

  1. 本地用 .env
  2. 测试环境用 CI/CD Secret。
  3. 生产用 Kubernetes Secret / Vault / 云厂商密钥管理。
  4. 镜像中只放程序,不放环境秘密。

17.3 容器尽量非 root 运行

Dockerfile 中添加:

1
2
RUN addgroup --system app && adduser --system --ingroup app app
USER app:app

17.4 bind mount 尽量只读

1
2
volumes:
- ./config:/app/config:ro

如果只是读取配置,就不要给写权限。

17.5 不要生产环境直接用开发 Compose

开发 Compose 常常有:

  • 明文密码;
  • Debug 端口;
  • 本地目录挂载;
  • 宽松网络;
  • 默认账号;
  • 暴露数据库端口。

这些在生产都是雷。

十八、IDEA + Docker 的最佳实践清单

18.1 本地开发最佳实践

  • 依赖服务用 Docker Compose 管理。
  • 应用本身本地 Run,必要时再容器 Run。
  • 数据库、Redis、MQ 不直接装本机,除非有特殊原因。
  • .env 不提交 Git,提交 .env.example
  • Compose 服务名要稳定,例如 postgresredisnacos
  • 本机端口尽量避免和系统服务冲突。
  • Debug 端口只在开发环境开放。

18.2 Dockerfile 最佳实践

  • 使用明确版本 tag,不要无脑 latest
  • 添加 .dockerignore
  • 优先非 root 用户运行。
  • 不在镜像中写死密码。
  • 不把源码、.git、IDE 配置复制进镜像。
  • 运行时镜像尽量小。
  • JVM 参数适配容器内存。
  • 构建产物和运行环境分离。

18.3 IDEA 使用最佳实践

  • IDEA 负责可视化管理,Compose / Dockerfile 负责工程事实。
  • Run Configuration 可以本地保存,但关键启动逻辑最好脚本化。
  • Services 中能看到问题,但最终要会用 CLI 复现。
  • Docker Context 要确认,避免连错 daemon。
  • 容器调试时,确保镜像代码和本地源码一致。
  • 私服账号不要写在项目文件里。

18.4 团队协作最佳实践

  • docker/README.md 写清楚本地环境启动方式。
  • 新人只需要执行一条命令就能启动依赖:
1
docker compose -f docker/compose.yml up -d
  • 数据库初始化脚本版本化。
  • 本地端口规范化。
  • 不同服务命名统一。
  • 镜像 tag 规则统一。
  • CI 构建镜像,不依赖某个人 IDEA 上的配置。

十九、一套完整的本地开发流程

综合前面的内容,一个推荐流程如下:

flowchart TD
    A[Clone 项目] --> B[安装 Docker / 启动 Docker daemon]
    B --> C[IDEA 启用 Docker 插件]
    C --> D[连接 Docker daemon]
    D --> E[运行 docker/compose.yml 启动依赖]
    E --> F[IDEA Database 连接 PostgreSQL]
    F --> G[本地启动 Spring Boot profile=local]
    G --> H[开发 / 调试 / 写接口]
    H --> I[需要容器化验证]
    I --> J[Dockerfile 构建镜像]
    J --> K[Compose 启动 app + infra]
    K --> L[Remote JVM Debug 或 Run Target Debug]
    L --> M[提交 Dockerfile / Compose / README]

对应命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1. 启动依赖
cd docker
docker compose up -d

# 2. 回项目根目录,本地启动应用
cd ..
./mvnw spring-boot:run -Dspring-boot.run.profiles=local

# 3. 构建镜像
./mvnw clean package -DskipTests
docker build -t finance-demo:0.0.1 .

# 4. 全量容器化运行
docker compose -f docker/compose.yml up -d --build

# 5. 查看日志
docker logs -f finance-demo-app

在 IDEA 中对应:

1
2
3
4
5
Services -> Run Compose
Database -> Connect PostgreSQL
Run -> Spring Boot local
Dockerfile -> Build Image / Run Container
Remote JVM Debug -> Attach 5005

二十、一个可直接复用的模板

20.1 Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
FROM eclipse-temurin:21-jre

WORKDIR /app

ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar

ENV TZ=Asia/Shanghai \
JAVA_OPTS="-XX:MaxRAMPercentage=75.0 -XX:+ExitOnOutOfMemoryError" \
SPRING_PROFILES_ACTIVE=docker

EXPOSE 8080
EXPOSE 5005

RUN addgroup --system app && adduser --system --ingroup app app
USER app:app

ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar /app/app.jar"]

20.2 .dockerignore

1
2
3
4
5
6
7
8
9
.git
.idea
*.iml
.DS_Store
logs
tmp
src/test
**/target/*
!target/*.jar

20.3 docker/compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
name: finance-demo

services:
app:
build:
context: ..
dockerfile: Dockerfile
image: finance-demo:0.0.1
container_name: finance-demo-app
environment:
SPRING_PROFILES_ACTIVE: docker
JAVA_OPTS: "-XX:MaxRAMPercentage=75.0 -XX:+ExitOnOutOfMemoryError"
DB_HOST: postgres
DB_PORT: 5432
DB_NAME: finance
DB_USERNAME: finance
DB_PASSWORD: finance123
REDIS_HOST: redis
REDIS_PORT: 6379
ports:
- "8080:8080"
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_started
networks:
- finance-net

postgres:
image: postgres:16
container_name: finance-postgres
environment:
POSTGRES_DB: finance
POSTGRES_USER: finance
POSTGRES_PASSWORD: finance123
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
- ./init-sql:/docker-entrypoint-initdb.d:ro
healthcheck:
test: ["CMD-SHELL", "pg_isready -U finance -d finance"]
interval: 10s
timeout: 5s
retries: 5
networks:
- finance-net

redis:
image: redis:7.4
container_name: finance-redis
command: ["redis-server", "--appendonly", "yes"]
ports:
- "6379:6379"
volumes:
- redis-data:/data
networks:
- finance-net

networks:
finance-net:
driver: bridge

volumes:
postgres-data:
redis-data:

20.4 docker/compose.debug.yml

1
2
3
4
5
6
7
8
9
services:
app:
ports:
- "5005:5005"
environment:
JAVA_OPTS: >-
-XX:MaxRAMPercentage=75.0
-XX:+ExitOnOutOfMemoryError
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

启动 Debug 版:

1
2
3
4
docker compose \
-f docker/compose.yml \
-f docker/compose.debug.yml \
up -d --build

IDEA Remote JVM Debug:

1
2
3
Host: localhost
Port: 5005
Module classpath: 当前 Spring Boot module

结语

IDEA 中使用 Docker 的核心,不是“我能不能在 IDE 里点一个容器出来”,而是:

  1. 能不能快速搭建一致的本地依赖环境;
  2. 能不能把应用放到接近真实的容器环境中运行;
  3. 能不能在容器里照样 Debug;
  4. 能不能让 Dockerfile / Compose 成为团队共享资产;
  5. 能不能在出了问题时,从 IDEA 视角切回 Docker CLI 视角快速定位。

成熟的 Docker 开发方式不是“命令行党”和“IDE 党”互相鄙视,而是两者组合:

1
2
3
Dockerfile / Compose / CLI 负责确定性
IDEA / Services / Run Configuration 负责效率
CI/CD / Registry 负责交付

真正工程化的本地环境,应该是新同事拉下代码后,看一眼 README,执行几条命令,IDEA 一打开就能看到容器、数据库、日志、调试入口,而不是在群里问三天:“这个 Redis 到底装哪个版本?”

参考资料

启示录

善用其器,而不困于器。

工程之道,不在炫技,而在让复杂系统稳定、可复现、可交付。


IDEA 中如何使用 Docker:从环境配置到 Spring Boot 容器化调试的深度实战
https://allendericdalexander.github.io/2026/06/12/devTools/idea-docker-deep-practice/
作者
AtLuoFu
发布于
2026年6月12日
许可协议