Cloud Native 6 min read

How to Set Up Docker on CentOS and Deploy a Spring Boot App in Containers

This step‑by‑step guide shows how to install and configure Docker on CentOS 7, optionally set up docker‑compose, pull a Tomcat image, build a Spring Boot project, package it, and run it inside a Docker container, including commands for mounting, port mapping, and verifying the deployment.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Set Up Docker on CentOS and Deploy a Spring Boot App in Containers

Environment: SpringBoot2.6.12 + Docker + Centos7 + JDK8

1. Install and configure Docker

Update yum packages to the latest version. <code>yum update</code>

Remove any old Docker versions. <code>yum remove docker docker-common docker-selinux docker-engine</code>

Install required utilities for yum-config-manager and device‑mapper. <code>yum install -y yum-utils device-mapper-persistent-data lvm2</code>

Add the Docker yum repository. <code>yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo</code>

List all available Docker CE versions. <code>yum list docker-ce --showduplicates | sort -r</code>

Install Docker CE. <code>yum install docker-ce</code>

Start the Docker service. <code>systemctl start docker</code>

Verify the Docker version. <code>docker version</code>

2. Install docker‑compose (optional)

Download docker‑compose binary. <code>curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose</code>

Add execution permission. <code>chmod +x /usr/local/bin/docker-compose</code>

Create a symbolic link. <code>ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose</code>

Verify the version. <code>docker-compose --version</code>

Search for a Tomcat image. <code>docker search tomcat</code>

Pull the Tomcat image. <code>docker pull tomcat</code>

3. Deploy the Spring Boot project

Create a Spring Boot project named spring-boot-docker and add the following dependencies in pom.xml :

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;</code>

Add a simple controller:

<code>@RestController
@RequestMapping("/demo")
public class DemoController {
    @GetMapping("/index")
    public Object index() {
        return "docker container running...";
    }
}</code>

Define the main application class:

<code>@SpringBootApplication
public class SpringBootDockerApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringBootDockerApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDockerApplication.class, args);
    }
}</code>

Package the application:

<code>mvn clean package -Dmaven.test.skip=true</code>

Upload the generated JAR/WAR to the server (illustrated below).

Run the container with the packaged artifact. Example for a WAR on Tomcat:

<code>docker run -d --name demo-server -v /root/apps/spring-boot-docker-1.0.0.war:/usr/local/tomcat/webapps/spring-boot-docker-1.0.0.war -p 8080:8080 tomcat</code>

Key options:

-v : mount the project file into the container

-p : map host port to container port (host:container)

-d : run in detached (background) mode

--name : assign a name to the container

tomcat : the image used

Check running containers:

<code>docker ps</code>

If you prefer to run the JAR directly, pull an OpenJDK image and mount the JAR:

<code>docker run -d --name demo-server -v /root/apps/spring-boot-docker-1.0.0.jar:/usr/spring-boot-docker-1.0.0.jar -p 8081:8080 openjdk java -jar /usr/spring-boot-docker-1.0.0.jar</code>

After the container starts, you can access the endpoint http://<host>:8080/demo/index and see the message "docker container running..." indicating a successful deployment.

DockerContainerizationSpring BootTutorialCentOS
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login 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.