In this tutorial, we are going to manually build the Spring Boot application, run it on a remote Ubuntu server with Docker, along with a MySQL database.
Prerequisites
We need to have the following running setup before we proceed with deployment.
- JDK 25 is installed on your local machine to build the Spring Boot app
- A ready spring-boot application to deploy
- Docker is installed on your Ubuntu server. Here is the tutorial post for this.
- MySQL 8 setup on your Ubuntu server. You can refer to the MySQL 8 setup tutorial.
Build the Spring-boot application
First lets manually build our ready Spring Boot application. Here, you don't have to add the production properties file to the application environment. As we are going to set up those properties file in server itself.
For Maven
bash
./mvnw clean packageIf you want to skip the test
bash
./mvnw clean package -DskipTestsFor Gradle
./gradlew buildNote: for these commands, you need to have a Java version installed on your system
Using Intellij Idea
Navigate to the right side Gradle tab and click the build as shown below.

All the build methods will create the build file inside the folder build/libs/csbyte-0.0.1-SNAPSHOT.jar.
Deploy on the Ubuntu server
Copy the build jar file
scp /path/to/the/jarfile ubuntu-user@server-ip-address:~/csbyte.jarNow, SSH into the Ubuntu server
ssh username@server-ip-addressCreate a Docker file
Now, let's create a Docker file for pulling JDK 25 and add the recently built jar file
touch DockerfileAdd the Docker config to build the image.
sudo vim Dockerfile # Pull jdk 25 from docker hub
FROM eclipse-temurin:25-jre-alpine
# Set the working directory inside the container
WORKDIR /app
# Copy the JAR you built manually into the container
COPY csbyte.jar app.jar
# Run the application
ENTRYPOINT ["java", "-jar", "app.jar"]Note: if you have the jar file in a different path with a different name, use the following command to copy the jar
COPY target/*.jar app.jarBuilding the Image
Now build the image with the following command:
docker build -t csbyte .Here, csbyte is the image name; you can use your own.
Output:
=> => extracting sha256:f20ccdd0c7bb68e396b2c66aa39ca2d60c83bd97e3b8ad8bb29b4eba112ef532 0.0s
=> => extracting sha256:5e109dd049422d199ac3c3328c12b15431660a3a1bc64d8fdd24f2f88da5efc4 0.0s
=> [internal] load build context 0.4s
=> => transferring context: 63.97MB 0.4s
=> [2/4] WORKDIR /app 0.1s
=> [3/4] COPY csbyte.jar app.jar
[+] Building 1.7s (8/8) FINISHED docker:default-t csbyte: Tags your image with a name- . : Tells Docker to look for the
Dockerfilein the current directory
Create a Configuration File
We are going to create our Spring Boot configuration properties file on our server. This is because the code in Spring Boot might be pushed to Git, and unwanted changes in the properties file will crash the system database, etc.
touch application-prod.propertiesAdd the desired configuration in these files. For now, we are using the MySQL config.
spring.datasource.url=jdbc:mysql://mysql-server:3306/csbyte?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=mysql-username
spring.datasource.password=your-mysql-password
spring.jpa.hibernate.ddl-auto=updateBuild the Docker container
Use the following command to build the Docker container:
docker run -d \
--name name-of-your-container \
--network tutorial-net \
-p 8080:8080 \
--restart unless-stopped \
-v $(pwd)/application-prod.properties:/app/config/application.properties \
csbytedocker run: create and run a new container from the image we created-d: run the container in the background--name name-of-your-container: name of the container--network tutorial-net: connect the container to the specific Docker network. It is especially required when we want to connect to other services like MySQL, which is in the same network.-p 8080:8080: maps the host port to the container port. The first is the port of the Ubuntu server, and the second is the port on which Spring Boot is listening inside the container.-v $(pwd)/application-prod.properties: the config file path created. This mounts the file in our Ubuntu server to the container.--restart unless-stopped: this helps to restart the app if it fails to keep it alive.csbyte: is the name of the Docker image created while building the Docker file.
Map the volume to the Ubuntu disk
If you are using local file storage or any other feature that needs data saving, we need to map that directory to the Docker. As Docker will wipe out the data when re-run or deleted and restarted. Please update the command:
bash
docker run -d \
--name name-of-your-container \
--network tutorial-net \
-p 8080:8080 \
--restart unless-stopped \
-v /opt/uploads:/opt/uploads:rw,Z \
-v $(pwd)/application-prod.properties:/app/config/application.properties \
csbyte-v /ubuntu-path-to-upload:/container-path-to-upload \ Make sure your app path matches this path.Here,
rw,Z this tells Docker to read and write, and the container is allowed to interact with the folder on Ubuntu. Z is for SELinux-enabled system (like CentOS/RHEL)Now your application will be live, open it using the IP address.
http://your-server-ip-address:8080Commands for Docker for troubleshooting
Check if the container is running:
sudo docker psShow application logs:
docker logs container-nameView logs in real-time:
sudo docker logs -f container-nameSee the last N line logs:
docker logs --tail 100 container-nameNow, we have successfully containerized our Spring Boot application and deployed it to the Ubuntu server. We will provide the nginx setup for reverse proxy and SSL setup in the next tutorial.
