Cloud Native 6 min read

How to Install Docker on CentOS and Deploy a Spring Boot Application

This step‑by‑step guide shows how to set up Docker on CentOS 7, install Docker Compose, pull a Tomcat image, create a Spring Boot project, package it, and run it inside a Docker container using both Tomcat and OpenJDK images.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Install Docker on CentOS and Deploy a Spring Boot Application

Environment: SpringBoot 2.2.10.RELEASE + Docker + CentOS 7 + JDK 8

Install and Configure Docker

Update yum packages to the latest version. yum update Remove old Docker packages.

yum remove docker docker-common docker-selinux docker-engine

Install required packages (yum-utils provides yum-config-manager, plus device‑mapper dependencies).

yum install -y yum-utils device-mapper-persistent-data lvm2

Add the Docker yum repository.

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

List all available Docker CE versions. yum list docker-ce --showduplicates | sort -r Install Docker CE. yum install docker-ce Start the Docker service. systemctl start docker Verify the Docker version.

docker version

Optional: Install Docker‑Compose

Download Docker‑Compose binary.

Add execution permission. chmod +x /usr/local/bin/docker-compose Create a symbolic link.

ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Check the installed version.

docker-compose --version

Pull a Tomcat Image

Search for Tomcat images and pull the latest one.

docker search tomcat
docker pull tomcat

Deploy the Spring Boot Project

Create a new Spring Boot project named spring-boot-docker.

pom.xml Dependency

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Controller

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

Application Class

@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);
    }
}

Package the Application

mvn clean package -Dmaven.test.skip=true

Upload the generated JAR/WAR to the server.

Run with Tomcat Image

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

Flags explanation:

-v: mount the project into the container

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

-d: run in background

--name: container name

tomcat: image used

Check Running Containers

docker ps

Run a JAR Directly with OpenJDK Image

Search for a Java image and use OpenJDK.

docker search java
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

All steps complete – the Spring Boot application is now running inside Docker.

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.

DockerDeploymentcontainerizationSpring BootTomcatCentOSDocker Compose
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

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.