Docker Deployment

Containerizing Rust blockchain applications with Docker.

Intermediate⏱️ 45 min📚 Prerequisites: 1

Docker Deployment

Docker containerization makes deploying Rust applications consistent and portable across different environments.

Why Docker for Blockchain?

  • Consistency: Same environment everywhere
  • Isolation: No conflicts with host system
  • Portability: Run on any Docker host
  • Scalability: Easy to scale containers
  • Reproducibility: Same build = same result

Multi-Stage Dockerfile

Rust applications benefit from multi-stage builds:

DOCKERFILE
# Stage 1: Builder
FROM rust:1.75 as builder

WORKDIR /app
COPY Cargo.toml Cargo.lock ./
COPY src ./src

# Build release binary
RUN cargo build --release

# Stage 2: Runtime
FROM debian:bookworm-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Copy binary from builder
COPY --from=builder /app/target/release/blockchain-node /usr/local/bin/

# Run the application
CMD ["blockchain-node"]

Optimized Dockerfile

DOCKERFILE
FROM rust:1.75 as builder

# Cache dependencies
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
RUN mkdir src && echo "fn main() {}" > src/main.rs
RUN cargo build --release
RUN rm src/main.rs

# Build application
COPY src ./src
RUN touch src/main.rs
RUN cargo build --release

# Runtime stage
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/blockchain-node /usr/local/bin/
CMD ["blockchain-node"]

Docker Compose

For multi-container setups:

YAML
version: '3.8'
services:
  blockchain-node:
    build: .
    ports:
      - "8080:8080"
    environment:
      - RUST_LOG=info
      - DATABASE_URL=postgres://db:5432/blockchain
    depends_on:
      - db
  
  db:
    image: postgres:15
    environment:
      - POSTGRES_DB=blockchain
      - POSTGRES_PASSWORD=secret

Building and Running

BASH
# Build image
docker build -t blockchain-node .

# Run container
docker run -p 8080:8080 blockchain-node

# With environment variables
docker run -p 8080:8080 -e RUST_LOG=debug blockchain-node

# With volume for data
docker run -p 8080:8080 -v ./data:/app/data blockchain-node

Production Considerations

  • Security: Run as non-root user
  • Health checks: Add HEALTHCHECK directive
  • Resource limits: Set memory/CPU limits
  • Logging: Configure log drivers
  • Secrets: Use Docker secrets or env files

Code Examples

Dockerfile for Blockchain

Complete Dockerfile for Rust blockchain

RUST
# Dockerfile example
# FROM rust:1.75 as builder
#
# WORKDIR /app
# COPY . .
# RUN cargo build --release
#
# FROM debian:bookworm-slim
# COPY --from=builder /app/target/release/blockchain-node /usr/local/bin/
# CMD ["blockchain-node"]
fn main() {
    println!("Dockerfile structure:");
    println!("1. Builder stage: Compile Rust code");
    println!("2. Runtime stage: Minimal OS with just the binary");
    println!("3. Result: Small, secure container");
    println!("\nBuild commands:");
    println!("docker build -t blockchain-node .");
    println!("docker run -p 8080:8080 blockchain-node");
}

Explanation:

Multi-stage Docker builds create small, efficient containers. The builder stage compiles Rust, the runtime stage only contains the binary and minimal dependencies.

Docker Compose Setup

Multi-container blockchain deployment

RUST
fn main() {
    println!("Docker Compose services:");
    println!("1. blockchain-node: Your Rust application");
    println!("2. database: PostgreSQL for state storage");
    println!("3. redis: Caching layer (optional)");
    println!("4. nginx: Reverse proxy (optional)");
    println!("\nCommands:");
    println!("docker-compose up -d    # Start all services");
    println!("docker-compose logs    # View logs");
    println!("docker-compose down    # Stop all services");
}

Explanation:

Docker Compose orchestrates multiple containers. A blockchain node typically needs a database, and optionally caching and reverse proxy services.

Exercises

Create Dockerfile

Write a basic Dockerfile for a Rust application!

Medium

Starter Code:

RUST
# Dockerfile
# Use rust:1.75 as builder
# Set WORKDIR to /app
# Copy Cargo files
# Copy source code
# Build with --release
# Create runtime stage from debian:bookworm-slim
# Copy binary to /usr/local/bin
# Set CMD to run the binary