Deploying a Spring Boot Application to Docker Using IDEA and Maven
This tutorial walks through preparing the environment, configuring Docker remote access, creating a Spring Boot project with Maven, writing Dockerfile and related configuration files, building the image, and running the container, demonstrating how to deploy a Java web application to Docker using IntelliJ IDEA.
IDEA is a powerful Java IDE, Spring Boot is the most popular microservice framework in the Java ecosystem, and Docker is the leading container technology; this guide shows how to combine them to deploy a Spring Boot application.
1. Preparation
Docker installation
Refer to the official Docker documentation: https://docs.docker.com/install/
Configure Docker remote connection port
vi /usr/lib/systemd/system/docker.serviceFind the ExecStart line and append -H tcp://0.0.0.0:2375 to enable remote access.
Restart Docker
systemctl daemon-reload
systemctl start dockerOpen firewall port
firewall-cmd --zone=public --add-port=2375/tcp --permanentInstall IDEA plugins and restart
(Images omitted for brevity.)
Connect to remote Docker
Edit the Docker configuration in IDEA, set the remote Docker address, and verify the connection; the remote containers and images are listed upon success.
2. Create Spring Boot project
Generate a Spring Boot project (e.g., via IDEA) and obtain the following project structure.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>docker-demo</groupId>
<artifactId>com.demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<docker.image.prefix>com.demo</docker.image.prefix>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath/>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<tasks>
<copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>Create Dockerfile (src/main/docker/Dockerfile)
FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]application.properties (src/main/resources)
logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990DockerApplication.java
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}DockerController.java
@RestController
public class DockerController {
static Log log = LogFactory.getLog(DockerController.class);
@RequestMapping("/")
public String index() {
log.info("Hello Docker!");
return "Hello Docker!";
}
}3. Build and run
Execute Maven packaging to produce the JAR, then use the Docker Maven plugin (or Docker CLI) to build the image, e.g., docker build -t docker-demo:1.1 ., and run the container with port and volume bindings:
docker run -d -p 8990:8990 -v /host/logs:/home/developer/app/logs --name docker-server docker-demo:1.1After the container starts, access http://localhost:8990/ in a browser to see the "Hello Docker!" response, and view logs in the mounted host directory.
Thus, deploying a Spring Boot Java web application to Docker via IDEA and Maven becomes straightforward and efficient.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
