Skip to main content

Overview

Trippins uses a multi-stage Docker build process to create an optimized production image that combines the Angular frontend and Spring Boot backend into a single deployable container.
The Docker setup uses a three-stage build process: Angular compilation, Maven build with frontend integration, and final runtime image.

Architecture

The deployment consists of two main services:

MySQL Database

MySQL 8.0.33 container for persistent data storage

Application Server

Spring Boot application with embedded Angular frontend

Dockerfile

The Dockerfile uses a multi-stage build to optimize the final image size:
FROM node:19.0.1 AS angular
WORKDIR /code
COPY ./frontend/package*.json ./frontend/angular.json ./frontend/tsconfig*.json /code/
RUN npm install && npm install chart.js
COPY ../frontend/src /code/src
RUN npm run build -- --configuration production --base-href=/new/

FROM maven:3.8.4-openjdk-17 AS builder
WORKDIR /daw
COPY ./backend/pom.xml .  
RUN mvn dependency:go-offline
COPY ./backend/src ./src
RUN mkdir -p src/main/resources/static/new
COPY --from=angular /code/dist/trippins-app/browser/ src/main/resources/static/new
RUN mvn clean package -DskipTests

FROM openjdk:17-jdk-slim
WORKDIR /daw
COPY --from=builder /daw/target/*.jar app.jar
EXPOSE 443
CMD ["java", "-jar", "app.jar"]

Build Stages

1

Stage 1: Angular Build

  • Base image: node:19.0.1
  • Installs npm dependencies including chart.js
  • Builds Angular application for production
  • Sets base href to /new/ for routing
2

Stage 2: Maven Build

  • Base image: maven:3.8.4-openjdk-17
  • Downloads Maven dependencies offline
  • Copies compiled Angular files to Spring Boot static resources
  • Packages the application as a JAR file
3

Stage 3: Runtime Image

  • Base image: openjdk:17-jdk-slim
  • Copies only the compiled JAR from builder stage
  • Exposes port 443 for HTTPS traffic
  • Runs the Spring Boot application

Docker Compose Configuration

The docker-compose.yml file orchestrates both the database and application services:
version: '3.9'

services:
  db:
    image: mysql:8.0.33
    container_name: trippins-db
    restart: always
    environment:
      - MYSQL_DATABASE=Trippins
      - MYSQL_ROOT_PASSWORD=password
    ports:
      - "3307:3306"
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - trippins-network

  web:
    build:
      context: ..
      dockerfile: docker/Dockerfile
    image: jantoniio3/trippins:latest
    container_name: trippins-app
    ports:
      - "443:8443"  # HTTPS port mapping
    depends_on:
      - db
    environment:
      - SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/Trippins
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_PASSWORD=password
      - JWT_SECRET=password
      - SERVER_SSL_ENABLED=true
      - SERVER_PORT=8443
    restart: on-failure
    networks:
      - trippins-network

volumes:
  db_data:

networks:
  trippins-network:

Service Configuration

Image: MySQL 8.0.33Environment Variables:
  • MYSQL_DATABASE: Trippins
  • MYSQL_ROOT_PASSWORD: password
Port Mapping: 3307:3306 (host:container)Volume: db_data mounted to /var/lib/mysql for data persistenceNetwork: Connected to trippins-network
Build Context: Parent directory with Dockerfile in docker/ subdirectoryImage: jantoniio3/trippins:latestPort Mapping: 443:8443 (HTTPS)Dependencies: Waits for db service to startEnvironment Variables:
  • SPRING_DATASOURCE_URL: Connection to MySQL container
  • SPRING_DATASOURCE_USERNAME: root
  • SPRING_DATASOURCE_PASSWORD: password
  • JWT_SECRET: password
  • SERVER_SSL_ENABLED: true
  • SERVER_PORT: 8443
Restart Policy: on-failureNetwork: Connected to trippins-network

Deployment Commands

Build and Run Locally

# Navigate to docker directory
cd TrippinsApp/docker

# Build and start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

Production Deployment Script

The image.sh script automates building, tagging, and pushing the image to Docker Hub:
echo "🔨 Construyendo imagen Docker desde la raíz del proyecto..."

docker build -t trippins:latest -f ../docker/Dockerfile ../

echo "🏷️ Etiquetando imagen..."
docker tag trippins:latest jantoniio3/trippins:latest

echo "📤 Subiendo a Docker Hub..."
docker push jantoniio3/trippins:latest

echo "🔄 Actualizando imagen en el servidor..."

echo "🔄 Actualizando imagen de docker"
docker-compose pull

echo "✅ ¡Todo listo!"
1

Build Image

Builds the Docker image from the project root using the Dockerfile
2

Tag Image

Tags the image with the Docker Hub repository name
3

Push to Registry

Pushes the tagged image to Docker Hub
4

Update Server

Pulls the latest image on the deployment server
Make sure to configure your Docker Hub credentials before running the deployment script:
docker login

Port Mappings

ServiceHost PortContainer PortProtocolDescription
Database33073306TCPMySQL database access
Application4438443HTTPSApplication HTTPS endpoint
The application runs on port 8443 internally but is exposed on port 443 for standard HTTPS access.

Networking

All services communicate through a custom bridge network called trippins-network. This allows:
  • Service discovery by container name
  • Isolated network environment
  • Secure inter-service communication
The backend connects to the database using the hostname db instead of localhost due to Docker’s internal DNS resolution.

Volume Management

Database Persistence

The db_data volume ensures that database data persists across container restarts:
# Backup database volume
docker run --rm -v trippins_db_data:/data -v $(pwd):/backup \
  ubuntu tar czf /backup/db-backup.tar.gz /data

# Restore database volume
docker run --rm -v trippins_db_data:/data -v $(pwd):/backup \
  ubuntu tar xzf /backup/db-backup.tar.gz -C /

# Remove volume (destructive)
docker volume rm trippins_db_data

Environment Variables

The following environment variables can be overridden in the docker-compose file:
SPRING_DATASOURCE_URL
string
required
JDBC URL for MySQL database connectionDefault: jdbc:mysql://db:3306/Trippins
SPRING_DATASOURCE_USERNAME
string
required
Database usernameDefault: root
SPRING_DATASOURCE_PASSWORD
string
required
Database passwordDefault: password
JWT_SECRET
string
required
Secret key for JWT token generationDefault: password
SERVER_SSL_ENABLED
boolean
required
Enable/disable SSLDefault: true
SERVER_PORT
number
required
Internal application portDefault: 8443

Troubleshooting

Check logs for errors:
docker-compose logs web
docker-compose logs db
Verify database is ready:
docker exec -it trippins-db mysql -uroot -ppassword
Ensure the database container is running:
docker-compose ps
Check network connectivity:
docker exec -it trippins-app ping db
Change the host port mapping in docker-compose.yml:
ports:
  - "8443:8443"  # Use different host port
Clear Docker build cache:
docker-compose build --no-cache

Next Steps

Configuration

Configure application settings and environment variables

Database Setup

Learn about the database schema and initialization

Build docs developers (and LLMs) love