Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dagger Services Demo

This project demonstrates Dagger services with container-to-container networking and local exposure.

What is Dagger?

Dagger is a programmable CI/CD engine that runs pipelines as containers. Instead of YAML configuration, you write type-safe Go code that works identically across local development and CI environments.

Key benefits:

  • Same build locally and in CI (no "works on my machine")
  • Fast feedback with aggressive caching
  • Portable across any CI platform
  • Programmatic service orchestration

What This Project Demonstrates

1. Service-to-Service Communication

Container networking via Dagger service bindings:

webapp.WithServiceBinding("redis", redisService)
// Creates DNS entry: webapp can now connect to "redis:6379"

2. Ephemeral Test Environments

Complete isolated stacks for testing:

  • Redis + webapp spun up per test
  • No port conflicts between parallel runs
  • Automatic cleanup

3. Reproducible Builds

Deterministic container builds with layer caching:

  • First build: ~30s
  • Cached builds: ~2s
  • Version-pinned dependencies

4. CI/CD as Code

GitHub Actions runs the same Dagger functions as local development.


Architecture

Local Machine
     │
     ├─── Dagger CLI
     │       │
     │       └─── Dagger Engine (BuildKit)
     │               │
     │               ├─── Webapp Container (Go)
     │               │       ↓
     │               └─── Redis Container
     │                     (Service Binding: "redis")
     │
     └─── Port Forward: localhost:8080 → webapp:8080

Tech Stack:

  • Dagger v0.19.2
  • Go 1.21
  • Redis 7
  • GitHub Actions

Quick Start

# Install Dagger
curl -L https://dl.dagger.io/dagger/install.sh | DAGGER_VERSION=0.19.2 sh
sudo mv bin/dagger /usr/local/bin

# Clone and test
git clone https://github.com/ram-git-dev/dagger-services-example.git
cd dagger-services-example

# Run integration test
dagger call simple-integration-test

# Start local environment
dagger call expose-locally up --ports 8080:8080

Test Endpoints

curl http://localhost:8080/health        # Health check
curl http://localhost:8080/counter       # Increment counter (Redis)
curl -X POST http://localhost:8080/set/key/value
curl http://localhost:8080/get/key

Project Structure

├── .github/workflows/     # CI/CD pipeline
├── app/                   # Go web application
│   ├── main.go           # HTTP server + Redis integration
│   └── go.mod            # App dependencies
├── main.go               # Dagger functions (pipeline code)
├── dagger.json           # Dagger module config
└── go.mod                # Dagger SDK dependencies

Key distinction:

  • main.go (root) = Infrastructure/CI code
  • app/main.go = Application code

Available Commands

# Build container
dagger call build-web-app

# Run integration tests
dagger call simple-integration-test

# Local development
dagger call expose-locally up --ports 8080:8080

See COMMANDS.md for full reference.


CI/CD Pipeline

Runs automatically on every push:

├─ Install Dagger v0.19.2
├─ Build web app
└─ Run integration tests

Pipeline executes the same Dagger functions used locally. No separate CI configuration needed.

View runs: Actions tab


Key Concepts

Service Binding

redisService := dag.Container().From("redis:7-alpine").AsService()
webapp.WithServiceBinding("redis", redisService)

Creates hostname redis that resolves to the Redis container. Application connects to redis:6379 - no hardcoded IPs.

Container Caching

Layer-based caching speeds up rebuilds:

  • Changed code → Rebuild from that layer onwards
  • Unchanged code → Use cached layers
  • Dependencies cached separately from application code

Idempotent Pipelines

Same inputs always produce same outputs. Critical for:

  • Reproducible builds
  • Debugging CI issues locally
  • Consistent deployments

Performance

Operation Cold Cached
Build 15s 2s
Integration test 3s 3s
CI pipeline 45s 30s

Resources


License

MIT - See LICENSE for details.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages