YourNote 803: Deploying Application with Docker - zhamri/CSC584-Enterprise_Programming GitHub Wiki
Deploying a JSP Web Application with Docker to DigitalOcean
This guide explains how to build a JSP web application, package it into a Docker image, push it to Docker Hub, and deploy it on a DigitalOcean server.
Note
If you are using an Apple Silicon Mac (M1/M2/M3), you must build a multi-platform Docker image (
linux/amd64andlinux/arm64) so it can run on DigitalOcean (AMD64).
Step 1. Create the Dockerfile
Create a file named Dockerfile in the root directory of your project.
FROM tomcat:10.1-jdk21
# Remove default Tomcat applications
RUN rm -rf /usr/local/tomcat/webapps/*
# Copy WAR file into Tomcat
COPY target/sttpk2023-payroll.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
CMD ["catalina.sh", "run"]
Your project structure should look like:
sttpk2023-payroll
│
├── Dockerfile
├── pom.xml
│
└── target
└── sttpk2023-payroll.war
Step 2. Build the WAR File
Compile the project using Maven.
mvn clean package
Verify the generated WAR file.
ls target
Expected output:
sttpk2023-payroll.war
Step 3. Create a Docker Buildx Builder (One-Time Setup)
On macOS:
docker buildx create --name mybuilder --use
Verify:
docker buildx ls
Example:
NAME/NODE DRIVER/ENDPOINT
mybuilder * docker-container
Step 4. Login to Docker Hub
docker login
Example:
Username: zhamri
Password: ********
Login Succeeded
Step 5. Build and Push the Docker Image
Since the project is built on Apple Silicon, build a multi-platform image.
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t zhamri/sttpk2023-payroll:0.0.1 \
--push .
This command:
- Builds the Docker image
- Creates both AMD64 and ARM64 versions
- Pushes the image directly to Docker Hub
Step 6. Verify the Docker Image
docker buildx imagetools inspect zhamri/sttpk2023-payroll:0.0.1
Expected output:
Name: docker.io/zhamri/sttpk2023-payroll:0.0.1
Manifests
linux/amd64
linux/arm64
If both architectures appear, the image is ready for deployment.
Step 7. Login to the DigitalOcean Server
ssh root@YOUR_SERVER_IP
Example:
ssh [email protected]
Step 8. Pull the Docker Image
docker pull zhamri/sttpk2023-payroll:0.0.1
Verify:
docker images
Expected:
REPOSITORY TAG
zhamri/sttpk2023-payroll 0.0.1
Step 9. Run the Docker Container
docker run -d \
--name payroll \
--restart unless-stopped \
-p 8080:8080 \
zhamri/sttpk2023-payroll:0.0.1
Verify:
docker ps
Expected:
CONTAINER ID IMAGE STATUS
abcd1234 zhamri/sttpk2023-payroll:0.0.1 Up 5 seconds
Step 10. Test the Application
Open:
http://YOUR_SERVER_IP:8080
Example:
http://159.xxx.xxx.xxx:8080
Your JSP application should appear.
Step 11. Using Docker Compose (Recommended)
Create docker-compose.yml.
services:
payroll:
image: zhamri/sttpk2023-payroll:0.0.1
container_name: payroll
restart: unless-stopped
ports:
- "8080:8080"
Start:
docker compose up -d
Stop:
docker compose down
Step 12. Configure Nginx Reverse Proxy
Create an Nginx configuration.
server {
listen 80;
server_name payroll.zhamri.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Enable the configuration.
sudo ln -s /etc/nginx/sites-available/payroll \
/etc/nginx/sites-enabled/
Test:
sudo nginx -t
Reload Nginx.
sudo systemctl reload nginx
Step 13. Enable HTTPS
sudo certbot --nginx -d payroll.zhamri.com
Step 14. Deploy a New Version
Build and push version 0.0.2.
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t zhamri/sttpk2023-payroll:0.0.2 \
--push .
On DigitalOcean:
docker pull zhamri/sttpk2023-payroll:0.0.2
Stop the old container.
docker stop payroll
Remove it.
docker rm payroll
Run the new version.
docker run -d \
--name payroll \
--restart unless-stopped \
-p 8080:8080 \
zhamri/sttpk2023-payroll:0.0.2
Useful Docker Commands
View Images
docker images
View Running Containers
docker ps
View All Containers
docker ps -a
View Container Logs
docker logs payroll
Follow Container Logs
docker logs -f payroll
Stop a Container
docker stop payroll
Start a Container
docker start payroll
Restart a Container
docker restart payroll
Remove a Container
docker rm payroll
Remove an Image
docker rmi zhamri/sttpk2023-payroll:0.0.1
Complete Workflow
JSP Project
│
▼
mvn clean package
│
▼
target/sttpk2023-payroll.war
│
▼
docker buildx build
│
▼
Docker Hub
│
▼
DigitalOcean
│
▼
docker pull
│
▼
docker run
│
▼
Nginx Reverse Proxy
│
▼
HTTPS
│
▼
https://payroll.zhamri.com
This deployment workflow supports both Apple Silicon (ARM64) and Intel/AMD64 Linux servers, making it suitable for local development and production deployment on DigitalOcean.