Skip to main content
The Blackjack API is fully containerized and can be run as a standalone Docker container. This page covers building the Docker image and running it locally.

Dockerfile

The project uses a multi-stage build to optimize the final image size:
# ---- build stage ----
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /app

COPY pom.xml .
RUN mvn -q -DskipTests dependency:go-offline

COPY src src
RUN mvn -q -DskipTests package

# ---- runtime stage ----
FROM eclipse-temurin:21-jre
WORKDIR /app

COPY --from=build /app/target/*.jar app.jar

EXPOSE 8080
ENTRYPOINT ["java","-jar","/app/app.jar"]

Build Process Explained

Stage 1: Build Stage

  1. Base Image: Uses maven:3.9-eclipse-temurin-21 which includes Maven and JDK 21
  2. Dependency Caching: Copies pom.xml first and downloads dependencies separately to leverage Docker layer caching
  3. Build Application: Copies source code and packages the application with mvn package
  4. Skip Tests: Uses -DskipTests to speed up the build (tests should run in CI/CD)

Stage 2: Runtime Stage

  1. Base Image: Uses eclipse-temurin:21-jre (JRE only, much smaller than JDK)
  2. Copy Artifact: Copies only the built JAR from the build stage
  3. Expose Port: Documents that the application listens on port 8080
  4. Entry Point: Runs the JAR file when the container starts

Building the Docker Image

Build the image from the project root:
docker build -t blackjack-api:latest .
With a custom tag:
docker build -t blackjack-api:v1.0.0 .
View the built image:
docker images | grep blackjack-api

Running the Docker Container

Prerequisites

The application requires MongoDB and MySQL to be running. You can run them separately:
# Start MongoDB
docker run -d -p 27017:27017 --name blackjack-mongo mongo:7

# Start MySQL
docker run -d -p 3306:3306 \
  --name blackjack-mysql \
  -e MYSQL_ROOT_PASSWORD=root \
  -e MYSQL_DATABASE=blackjack \
  -e MYSQL_USER=blackjack \
  -e MYSQL_PASSWORD=blackjack \
  mysql:8.0

Run the API Container

docker run -d \
  -p 8080:8080 \
  --name blackjack-api \
  -e SPRING_PROFILES_ACTIVE=docker \
  --link blackjack-mongo:mongo \
  --link blackjack-mysql:mysql \
  blackjack-api:latest

Environment Variables

The application supports multiple Spring profiles:
  • local - For local development
  • docker - For Docker deployment (uses service names as hostnames)
  • prod - For production deployment (uses environment variables)

Verify the Container

Check if the container is running:
docker ps | grep blackjack-api
View container logs:
docker logs blackjack-api
Follow logs in real-time:
docker logs -f blackjack-api

Using Pre-built Image from Docker Hub

The official image is available on Docker Hub:
docker pull ccasr/blackjack-api:latest
Run the pre-built image:
docker run -p 8080:8080 ccasr/blackjack-api:latest

Docker Commands Reference

Stop the Container

docker stop blackjack-api

Remove the Container

docker rm blackjack-api

Remove the Image

docker rmi blackjack-api:latest

Inspect Container

docker inspect blackjack-api

Execute Commands in Running Container

docker exec -it blackjack-api /bin/bash

Best Practices

.dockerignore

The project includes a .dockerignore file to exclude unnecessary files from the build context:
target
.idea
.vscode
*.iml
.DS_Store
**/.mvn/wrapper/maven-wrapper.jar
.git
.gitignore
This reduces build context size and speeds up the build process.

Health Checks

Add a health check to the Dockerfile (optional):
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
  CMD curl -f http://localhost:8080/actuator/health || exit 1

Resource Limits

Run with resource limits:
docker run -d \
  -p 8080:8080 \
  --memory="512m" \
  --cpus="1.0" \
  blackjack-api:latest

Testing the API

Once the container is running, test the API:
# Health check
curl http://localhost:8080/actuator/health

# Swagger UI
open http://localhost:8080/swagger-ui.html

# Create a new game
curl -X POST http://localhost:8080/game/new

Next Steps

Build docs developers (and LLMs) love