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.
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-engineInstall required packages (yum-utils provides yum-config-manager, plus device‑mapper dependencies).
yum install -y yum-utils device-mapper-persistent-data lvm2Add the Docker yum repository.
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repoList 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 versionOptional: 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-composeCheck the installed version.
docker-compose --versionPull a Tomcat Image
Search for Tomcat images and pull the latest one.
docker search tomcatdocker pull tomcatDeploy 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=trueUpload 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 tomcatFlags 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 psRun a JAR Directly with OpenJDK Image
Search for a Java image and use OpenJDK.
docker search javadocker 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.jarAll steps complete – the Spring Boot application is now running inside Docker.
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.
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.
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.
