Deployment and DevOps: Docker, Kubernetes, and CI/CD for Scala Applications

Modern Scala applications require robust deployment and DevOps practices for reliable production operations. This comprehensive lesson covers Docker containerization, Kubernetes orchestration, CI/CD pipelines, infrastructure as code, monitoring, and production deployment strategies for enterprise-scale applications.

Docker Containerization

Multi-Stage Docker Builds for Scala Applications

# Dockerfile - Optimized multi-stage build for Scala applications
# Stage 1: Build environment
FROM hseeberger/scala-sbt:17.0.2_1.6.2_2.13.8 AS builder

# Set working directory
WORKDIR /app

# Copy dependency files first for better layer caching
COPY project/build.properties project/
COPY project/plugins.sbt project/
COPY project/Dependencies.scala project/
COPY build.sbt ./

# Download dependencies (cached if dependencies haven't changed)
RUN sbt update

# Copy source code
COPY src ./src

# Build the application
RUN sbt clean compile test stage

# Stage 2: Runtime environment
FROM openjdk:17-jre-slim AS runtime

# Install system dependencies
RUN apt-get update && apt-get install -y \
    curl \
    dumb-init \
    && rm -rf /var/lib/apt/lists/*

# Create non-root user for security
RUN groupadd -r appuser && useradd -r -g appuser appuser

# Set working directory
WORKDIR /app

# Copy application from builder stage
COPY --from=builder /app/target/universal/stage ./

# Change ownership to non-root user
RUN chown -R appuser:appuser /app

# Switch to non-root user
USER appuser

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  CMD curl -f http://localhost:8080/health || exit 1

# Expose port
EXPOSE 8080

# Use dumb-init to handle signals properly
ENTRYPOINT ["/usr/bin/dumb-init", "--"]

# Start the application
CMD ["bin/my-scala-app", "-Dconfig.resource=production.conf"]

# Dockerfile.native - GraalVM Native Image build
FROM ghcr.io/graalvm/graalvm-ce:java17-22.3.0 AS native-builder

# Install native-image
RUN gu install native-image

# Install build dependencies
RUN microdnf install -y gcc glibc-devel zlib-devel

# Set working directory
WORKDIR /app

# Copy SBT and project files
COPY project ./project
COPY build.sbt ./

# Download dependencies
RUN sbt update

# Copy source code
COPY src ./src

# Build native image
RUN sbt graalvm-native-image:packageBin

# Runtime stage for native image
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.7 AS native-runtime

# Install runtime dependencies
RUN microdnf install -y \
    glibc-langpack-en \
    && microdnf clean all

# Create non-root user
RUN adduser -r -u 1001 appuser

# Set working directory
WORKDIR /app

# Copy native binary
COPY --from=native-builder /app/target/graalvm-native-image/my-scala-app ./

# Change ownership
RUN chown appuser:appuser /app/my-scala-app && chmod +x /app/my-scala-app

# Switch to non-root user
USER appuser

# Expose port
EXPOSE 8080

# Health check for native image
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
  CMD ./my-scala-app --health-check || exit 1

# Start the native application
CMD ["./my-scala-app"]

Docker Compose for Development


# docker-compose.yml - Development environment
version: '3.8'