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. yum update Remove any old Docker versions.

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

Install required utilities for yum-config-manager and device‑mapper.

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

2. Install docker‑compose (optional)

Download docker‑compose binary.

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

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

Verify the version. docker-compose --version Search for a Tomcat image.

docker search tomcat

Pull the Tomcat image.

docker pull tomcat

3. Deploy the Spring Boot project

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

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

Add a simple controller:

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

Define the main 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 (illustrated below).

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

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

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:

docker ps

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

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

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.

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.

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

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.