Cloud Native 10 min read

Step-by-Step Guide to Deploying a Spring Boot Application with Docker

This tutorial walks through installing Docker, configuring remote access, creating a Spring Boot project, writing the necessary Dockerfile and Maven settings, building the image, and running the container, demonstrating how IDEA, Spring Boot, and Docker combine to simplify Java web deployment.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Step-by-Step Guide to Deploying a Spring Boot Application with Docker

IDEA is a powerful Java IDE, Spring Boot is the most popular micro‑service framework in the Java ecosystem, and Docker is the hottest container technology; combining them creates a seamless development workflow.

1. Development Preparation

1. Docker Installation

You can refer to:

https://docs.docker.com/install/

2. Configure Docker Remote Connection Port

vi /usr/lib/systemd/system/docker.service

Locate the ExecStart line and append -H tcp://0.0.0.0:2375 at the end, as shown in the screenshot.

3. Restart Docker

systemctl daemon-reload
systemctl start docker

4. Open Port

firewall-cmd --zone=public --add-port=2375/tcp --permanent

5. Install IDEA Plugins and Restart

6. Connect to Remote Docker

1. Edit Configuration

2. Fill Remote Docker Address

3. Connection Successful – Containers and Images Listed

2. Create Project

1. Create Spring Boot Project

Project structure diagram:

1. Configure 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>/</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>

2. Create Docker Directory and Dockerfile

FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

3. Create application.properties in resources

logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990

4. Create DockerApplication Class

@SpringBootApplication
public class DockerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DockerApplication.class, args);
    }
}

5. Create DockerController Class

@RestController
public class DockerController {
    static Log log = LogFactory.getLog(DockerController.class);

    @RequestMapping("/")
    public String index() {
        log.info("Hello Docker!");
        return "Hello Docker!";
    }
}

6. Add Additional Configuration

Image tag: specifies image name docker-demo and tag 1.1.

Bind ports: maps host port to container port in the form [hostPort]:[containerPort].

Bind mounts: mounts a host directory into the container; the Spring Boot logs are written to /home/developer/app/logs/ inside the container, so mounting a host directory persists the logs outside the container.

7. Maven Package

8. Run

9. Run Success

10. Browser Access

11. Log View

Thus, deploying a Spring Boot project to Docker via IDEA is accomplished easily; it is surprising how simple and convenient deploying a Java web application can be.

Source: juejin.im/post/5d026212f265da1b8608828b

Instead of searching for interview questions online, follow us now!

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCloud NativeDockermavencontainerizationSpring BootIDEA
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.