Buildkite Agent Stack
Buildkite agents with Docker-in-Docker.
Overview
Buildkite Agent is a self-hosted build runner that connects to Buildkite's hosted platform to execute CI/CD pipelines. Originally developed by Buildkite as an alternative to fully cloud-hosted CI solutions, it allows organizations to run builds on their own infrastructure while leveraging Buildkite's powerful pipeline management interface. The agent polls for jobs from Buildkite's servers and executes them locally, providing complete control over the build environment while maintaining the convenience of a managed CI/CD platform.
This stack combines Buildkite Agent with Docker-in-Docker (DinD) to create an isolated containerized CI environment capable of building Docker images and running container-based workloads. The Docker daemon runs in a separate privileged container, while Buildkite agents connect to it via TCP, enabling secure Docker operations without exposing the host's Docker socket. A local Docker registry completes the stack, providing immediate storage for built images during multi-stage builds or testing scenarios.
Development teams and DevOps engineers who need to run Docker-based builds in Buildkite will find this configuration particularly valuable. Unlike cloud-hosted CI runners with limited customization, this self-hosted approach provides full control over the build environment, custom tooling installation, and network access while maintaining isolation between builds through containerization.
Key Features
- Buildkite Agent with automatic job polling and pipeline execution
- Isolated Docker-in-Docker daemon with TLS certificate authentication
- Local Docker registry for immediate image storage and caching
- Privileged DinD container enabling full Docker functionality including volume mounts
- Agent auto-registration with configurable names and metadata
- Shared certificate volume for secure agent-to-daemon communication
- Horizontal scaling through additional agent containers
- Build artifact persistence through dedicated Docker volumes
Common Use Cases
- 1Running Docker image builds and multi-stage builds in Buildkite pipelines
- 2Testing containerized applications with full Docker Compose support
- 3Creating CI environments for teams migrating from Jenkins to Buildkite
- 4Executing integration tests requiring Docker containers and networking
- 5Building and pushing images to private registries from Buildkite jobs
- 6Running security scans and vulnerability assessments on Docker images
- 7Supporting development teams needing custom build tools and environments
Prerequisites
- Buildkite organization with agent registration token
- Docker host with at least 4GB RAM for DinD operations
- Available port 5000 for local Docker registry access
- Privileged container support on the Docker host
- Network connectivity to api.buildkite.com for agent communication
- Understanding of Buildkite pipeline YAML configuration
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 dind: 3 image: docker:24-dind4 container_name: buildkite-dind5 restart: unless-stopped6 privileged: true7 environment: 8 - DOCKER_TLS_CERTDIR=/certs9 volumes: 10 - dind_certs:/certs11 - dind_data:/var/lib/docker1213 agent-1: 14 image: buildkite/agent:latest15 container_name: buildkite-agent-116 restart: unless-stopped17 environment: 18 - BUILDKITE_AGENT_TOKEN=${AGENT_TOKEN}19 - BUILDKITE_AGENT_NAME=agent-120 - DOCKER_HOST=tcp://dind:237621 volumes: 22 - dind_certs:/certs/client:ro23 depends_on: 24 - dind2526 registry: 27 image: registry:228 container_name: buildkite-registry29 restart: unless-stopped30 ports: 31 - "${REGISTRY_PORT:-5000}:5000"32 volumes: 33 - registry_data:/var/lib/registry3435volumes: 36 dind_certs: 37 dind_data: 38 registry_data: .env Template
.env
1# Buildkite Agents2AGENT_TOKEN=your_buildkite_agent_token3REGISTRY_PORT=5000Usage Notes
- 1Get agent token from Buildkite
- 2Agents auto-register
- 3Local registry at localhost:5000
- 4Scale by adding more agents
Individual Services(3 services)
Copy individual services to mix and match with your existing compose files.
dind
dind:
image: docker:24-dind
container_name: buildkite-dind
restart: unless-stopped
privileged: true
environment:
- DOCKER_TLS_CERTDIR=/certs
volumes:
- dind_certs:/certs
- dind_data:/var/lib/docker
agent-1
agent-1:
image: buildkite/agent:latest
container_name: buildkite-agent-1
restart: unless-stopped
environment:
- BUILDKITE_AGENT_TOKEN=${AGENT_TOKEN}
- BUILDKITE_AGENT_NAME=agent-1
- DOCKER_HOST=tcp://dind:2376
volumes:
- dind_certs:/certs/client:ro
depends_on:
- dind
registry
registry:
image: registry:2
container_name: buildkite-registry
restart: unless-stopped
ports:
- ${REGISTRY_PORT:-5000}:5000
volumes:
- registry_data:/var/lib/registry
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 dind:5 image: docker:24-dind6 container_name: buildkite-dind7 restart: unless-stopped8 privileged: true9 environment:10 - DOCKER_TLS_CERTDIR=/certs11 volumes:12 - dind_certs:/certs13 - dind_data:/var/lib/docker1415 agent-1:16 image: buildkite/agent:latest17 container_name: buildkite-agent-118 restart: unless-stopped19 environment:20 - BUILDKITE_AGENT_TOKEN=${AGENT_TOKEN}21 - BUILDKITE_AGENT_NAME=agent-122 - DOCKER_HOST=tcp://dind:237623 volumes:24 - dind_certs:/certs/client:ro25 depends_on:26 - dind2728 registry:29 image: registry:230 container_name: buildkite-registry31 restart: unless-stopped32 ports:33 - "${REGISTRY_PORT:-5000}:5000"34 volumes:35 - registry_data:/var/lib/registry3637volumes:38 dind_certs:39 dind_data:40 registry_data:41EOF4243# 2. Create the .env file44cat > .env << 'EOF'45# Buildkite Agents46AGENT_TOKEN=your_buildkite_agent_token47REGISTRY_PORT=500048EOF4950# 3. Start the services51docker compose up -d5253# 4. View logs54docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/buildkite-agent-stack/run | bashTroubleshooting
- Agent shows 'connection refused' errors: Verify BUILDKITE_AGENT_TOKEN is valid and network allows HTTPS to api.buildkite.com
- Docker commands fail with 'Cannot connect to Docker daemon': Check dind container is running and DOCKER_HOST points to tcp://dind:2376
- Permission denied accessing /certs directory: Ensure dind_certs volume is properly mounted and containers have started in correct order
- Registry push fails with 'server gave HTTP response to HTTPS client': Configure Docker daemon in DinD with --insecure-registry flag for local registry
- Agent not appearing in Buildkite dashboard: Verify agent token permissions and check agent container logs for registration errors
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download