Introduction
AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces deployable software packages. One common use case is building Docker container images as part of CI/CD pipelines. However, CodeBuild can only execute Docker builds when the build environment is specifically configured to support Docker-in-Docker operations.
Without privileged mode enabled, Docker commands fail immediately because there is no accessible Docker daemon inside the build container. This fundamental infrastructure requirement is often overlooked, leading teams to misdiagnose the problem as an ECR authentication issue, Dockerfile syntax error, or IAM permission problem. Understanding the relationship between CodeBuild's privileged mode and Docker daemon availability is essential for successful container image builds.
Docker-in-Docker (DinD) requires privileged mode because it involves running a Docker daemon inside a Docker container. This daemon needs elevated privileges to manage container operations, create namespaces, and configure networking. AWS CodeBuild mitigates the security implications by isolating each build in its own ephemeral environment.
Symptoms
When CodeBuild attempts Docker operations without privileged mode enabled, you will observe these symptoms:
docker buildcommands fail immediately in build logs with daemon connection errors- Build logs report
Cannot connect to the Docker daemon at unix:///var/run/docker.sockor similar socket errors - ECR login steps using
aws ecr get-login-passwordsucceed, but subsequentdocker buildanddocker pushcommands still fail - The same Dockerfile and build process works perfectly on local machines and other CI platforms like GitHub Actions or Jenkins
docker versioncommand shows client version but fails on server/daemon section- Build phases complete up until the Docker commands are invoked, then abruptly fail
- Error messages like
error during connect: This error may indicate that the docker daemon is not running
Typical error output from CodeBuild logs:
[Container] 2024/01/15 10:23:45 Running command docker build -t myapp:latest .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock.
Is the docker daemon running on this host?
[Container] 2024/01/15 10:23:45 Phase complete: BUILD State: FAILEDCommon Causes
Several factors contribute to CodeBuild Docker build failures related to privileged mode:
- 1.Privileged mode disabled by default: AWS CodeBuild projects do not enable privileged mode by default. This is a security-conscious default, but it breaks Docker builds unless explicitly enabled.
- 2.Incorrect build environment image: Using a build environment image that doesn't include Docker tooling or has Docker binaries in non-standard paths. Standard CodeBuild images (
aws/codebuild/standard:7.0) include Docker, but custom images may not. - 3.Missing Docker socket: Even with privileged mode, if the Docker socket is not properly mounted or accessible, builds will fail. This is typically handled automatically by CodeBuild when privileged mode is enabled.
- 4.Compute type limitations: Certain older compute types or ARM-based environments may have different Docker support configurations that require additional setup.
- 5.Confusing registry auth with daemon access: Teams often spend time debugging ECR permissions when the actual blocker is the unavailable Docker daemon. Successful ECR login does not prove Docker daemon access.
- 6.Outdated buildspec configuration: Buildspec files that assume Docker availability without checking first can fail silently or with confusing error messages.
- 7.Organization-level restrictions: Some AWS organizations enforce policies that prevent enabling privileged mode on CodeBuild projects, requiring exceptions or alternative approaches.
Step-by-Step Fix
Follow these steps to diagnose and resolve CodeBuild Docker build failures:
Step 1: Verify current project configuration
Check whether privileged mode is currently enabled for your CodeBuild project:
```bash # Get detailed project configuration aws codebuild batch-get-projects \ --names my-project \ --query "projects[0].environment" \ --output json
# Or specifically check privileged mode status aws codebuild batch-get-projects \ --names my-project \ --query "projects[0].environment.privilegedMode" \ --output text ```
Expected output for a properly configured Docker build project:
{
"type": "LINUX_CONTAINER",
"image": "aws/codebuild/standard:7.0",
"computeType": "BUILD_GENERAL1_MEDIUM",
"privilegedMode": true
}If privilegedMode shows false or is absent, this is the root cause of Docker build failures.
Step 2: Enable privileged mode on the CodeBuild project
Update the project configuration to enable privileged mode:
```bash # Update project with privileged mode enabled aws codebuild update-project \ --name my-project \ --environment type=LINUX_CONTAINER,image=aws/codebuild/standard:7.0,computeType=BUILD_GENERAL1_MEDIUM,privilegedMode=true
# For more complex environment configurations, use JSON format aws codebuild update-project \ --name my-project \ --environment '{ "type": "LINUX_CONTAINER", "image": "aws/codebuild/standard:7.0", "computeType": "BUILD_GENERAL1_MEDIUM", "privilegedMode": true }' ```
You can also enable privileged mode through the AWS Console:
- 1.Navigate to AWS CodeBuild console
- 2.Select your project and click "Edit"
- 3.Go to "Environment" section
- 4.Check the box "Enable privileged mode"
- 5.Save changes
Step 3: Use a Docker-capable build environment image
Ensure your build environment image includes Docker:
```bash # Recommended standard images that include Docker # aws/codebuild/standard:7.0 - Amazon Linux 2023 with Docker # aws/codebuild/standard:6.0 - Amazon Linux 2 with Docker # aws/codebuild/standard:5.0 - Ubuntu with Docker (older)
# Update to a standard image if using custom image aws codebuild update-project \ --name my-project \ --environment type=LINUX_CONTAINER,image=aws/codebuild/standard:7.0,computeType=BUILD_GENERAL1_MEDIUM,privilegedMode=true ```
Step 4: Add Docker verification to your buildspec
Modify your buildspec.yml to verify Docker availability before build steps:
```yaml version: 0.2
phases: pre_build: commands: # Verify Docker is available before proceeding - echo "Checking Docker availability..." - docker version - docker info # Login to ECR - echo "Logging in to Amazon ECR..." - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com build: commands: - echo "Build started on $(date)" - echo "Building the Docker image..." - docker build -t $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my-repo:latest . - docker tag $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my-repo:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my-repo:$CODEBUILD_BUILD_NUMBER post_build: commands: - echo "Build completed on $(date)" - echo "Pushing the Docker image..." - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my-repo:latest - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my-repo:$CODEBUILD_BUILD_NUMBER ```
Step 5: Test with a minimal build
Run a test build to verify Docker works before complex operations:
# Minimal test buildspec
version: 0.2
phases:
build:
commands:
- echo "Testing Docker availability"
- docker run --rm hello-worldTrigger this test build:
aws codebuild start-build --project-name my-projectStep 6: Address organization policy restrictions
If privileged mode cannot be enabled due to organization policies:
```bash # Check for SCP restrictions aws organizations list-policies-for-target --target-id your-account-id --filter SERVICE_CONTROL_POLICY
# Request exception through your organization administrator # Or consider alternative approaches like: # - Using AWS CodePipeline with external build providers # - Self-hosted build agents with Docker access # - AWS ECR pull-through cache with upstream image building elsewhere ```
Verification
After enabling privileged mode, verify the configuration works:
```bash # Start a test build aws codebuild start-build --project-name my-project
# Monitor build logs aws codebuild batch-get-builds --ids build-id --query "builds[0].buildStatus"
# Check for Docker-specific output in build logs # Look for successful docker version output showing both client and server ```
Expected Docker version output showing daemon is running:
``` Client: Docker Engine - Community Version: 24.0.7 API version: 1.43
Server: Docker Engine - Community Engine: Version: 24.0.7 API version: 1.43 ```
Verify image push to ECR:
```bash # List images in ECR repository aws ecr list-images --repository-name my-repo
# Check image details aws ecr describe-images --repository-name my-repo --image-ids imageTag=latest ```
Prevention
To prevent CodeBuild Docker build failures in future projects:
- 1.Standardize project templates: Create CodeBuild project templates or CloudFormation templates that always include
privilegedMode: truefor Docker build projects. Store these in a shared repository for team use. - 2.Add buildspec validation: Include Docker availability checks at the start of every buildspec that uses Docker commands. Fail fast with clear error messages if Docker is unavailable.
pre_build:
commands:
- |
if ! docker version > /dev/null 2>&1; then
echo "ERROR: Docker daemon not available. Ensure privileged mode is enabled."
exit 1
fi- 1.Document infrastructure requirements: Maintain clear documentation that Docker builds require privileged mode. Include this in onboarding materials and project READMEs.
- 2.Use infrastructure as code: Define CodeBuild projects through CloudFormation or Terraform with privileged mode explicitly set:
# CloudFormation example
Resources:
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Environment:
Type: LINUX_CONTAINER
Image: aws/codebuild/standard:7.0
ComputeType: BUILD_GENERAL1_MEDIUM
PrivilegedMode: true- 1.Separate troubleshooting layers: Create runbooks that distinguish between daemon availability issues (privileged mode) and registry authentication issues (IAM/ECR permissions). Check daemon access first before debugging permissions.
- 2.Regular build environment audits: Periodically audit CodeBuild projects to ensure privileged mode is enabled where needed. Use scripts to check and report configuration drift.
- 3.Monitor build failures: Set up CloudWatch alarms for build failure rates and investigate patterns. A sudden increase in Docker-related failures may indicate configuration changes.
Related Articles
- [AWS troubleshooting: Fix IAM Permission Denied - Complete Tro](fix-iam-permission-denied)
- [AWS cloud troubleshooting: AWS ACM Certificate Pending Validation Because the](aws-acm-certificate-pending-validation-wrong-route53-zone)
- [AWS cloud troubleshooting: AWS ALB Returns 502 Because the Target Closed the ](aws-alb-502-target-closed-connection-keepalive-timeout-mismatch)
- [AWS cloud troubleshooting: Fix AWS ALB CreateListener TargetGroupNotFound Err](aws-alb-createlistener-targetgroupnotfound)
- [AWS cloud troubleshooting: Fix Aws Alb Lambda 502 Bad Gateway Issue in AWS](aws-alb-lambda-502-bad-gateway)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "AWS cloud troubleshooting: AWS CodeBuild Docker Build Failed Because Privileg", "description": "Professional guide to fix AWS CodeBuild Docker Build Failed Because Privileged Mode Was Disabled. AWS cloud troubleshooting with step-by-step solutions. Learn best practices and prevention strategies.", "url": "https://www.fixwikihub.com/aws-codebuild-docker-build-privileged-mode", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2026-01-23T22:12:58.926Z", "dateModified": "2026-01-23T22:12:58.926Z" } </script>