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.
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-engineInstall required utilities for yum-config-manager and device‑mapper.
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 -rInstall Docker CE. yum install docker-ce Start the Docker service. systemctl start docker Verify the Docker version.
docker version2. 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-composeAdd execution permission. chmod +x /usr/local/bin/docker-compose Create a symbolic link.
ln -s /usr/local/bin/docker-compose /usr/bin/docker-composeVerify the version. docker-compose --version Search for a Tomcat image.
docker search tomcatPull the Tomcat image.
docker pull tomcat3. 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 tomcatKey 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 psIf 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.jarAfter the container starts, you can access the endpoint http://<host>:8080/demo/index and see the message "docker container running..." indicating a successful deployment.
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.
