A Docker-in-Docker (DinD) setup for building and pushing Docker images to a container registry using Docker Compose. This tool is designed to be integrated into existing projects to provide containerized Docker builds.
Dockerize provides a containerized environment for building Docker images without requiring Docker to be installed on the host machine. It uses Docker-in-Docker (DinD) to create an isolated Docker daemon and a separate builder container to perform build and push operations.
This is not a standalone project - it's a build tool that should be integrated into your existing project to build and push your application's Docker images.
- dind: Docker-in-Docker daemon container that runs the Docker daemon
- builder: Docker CLI container that connects to the dind daemon to build and push images
This tool integrates with your project's existing Dockerfile and builds/pushes your application image to the specified registry.
- Docker and Docker Compose installed on your host machine
- An existing project with a Dockerfile
- Access to a Docker registry (e.g., git.mnco.dev, Docker Hub, etc.)
- Registry credentials (username and token/password)
-
Integrate into your existing project:
# Add dockerize files to your project root # Copy the following files to your project: # - dockerize.yml (rename from docker-compose.yaml) # - .env.example -> .env.dockerize # - docker-daemon.json (optional)
-
Create environment configuration:
cp .env.example .env.dockerize
-
Configure your environment variables in
.env.dockerize:USER=your-registry-username TOKEN=your-registry-token REGISTRY=your-registry-host # e.g., docker.io IMAGE=your-repo/image-name # e.g., organization/project TAG=your-tag # e.g., latest, v1.0.0
-
Ensure your project has a Dockerfile: Make sure your project has a
Dockerfilein the root directory. The dockerize tool will use this Dockerfile to build your application image. -
Configure Docker daemon (optional): The included
docker-daemon.jsonfile provides optimized settings for the Docker daemon.
To build and push your application's Docker image to the registry:
docker compose -f dockerize.yml upThis command will:
- Start the DinD daemon and wait for it to be ready
- Log in to your configured registry
- Build your application's Docker image using your project's Dockerfile
- Export the image as an OCI tar file
- Import and push the image to the registry using regctl
- Clean up temporary files
Start only the DinD daemon:
docker compose -f dockerize.yml up dindRun the builder separately:
docker compose -f dockerize.yml up builderStop all services:
docker compose -f dockerize.yml downRemove volumes (will clear build cache):
docker compose -f dockerize.yml down -v| Variable | Description | Example |
|---|---|---|
USER |
Registry username | myuser |
TOKEN |
Registry token/password | ghp_xxxxxxxxxxxx |
DOCKER_HOST |
Docker daemon endpoint | tcp://dind:2375 |
DOCKER_BUILDKIT |
Enable BuildKit | 1 |
REGISTRY |
Registry hostname | docker.io. |
IMAGE |
Image repository/name | tyou0/dockerize |
TAG |
Image tag | latest |
BLOB_CHUNK |
Chunk size for large images (default: 5MB) | 5242880 |
BLOB_MAX |
Max blob size before splitting (default: 100MB) | 104857600 |
The docker-daemon.json file allows you to configure the Docker daemon. Common configurations include:
{
"max-concurrent-uploads": 3,
"max-concurrent-downloads": 3,
"storage-driver": "overlay2",
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}For Docker registries hosted through Cloudflare Tunnel with size limitations (100MB limit), the dockerize tool automatically splits large images into chunks:
- BLOB_CHUNK: Size of each chunk when splitting large images (default: 5MB)
- BLOB_MAX: Maximum blob size before splitting (default: 100MB)
These settings are particularly useful when pushing images to a Docker registry hosted through Cloudflare Tunnel, which has a 100MB limit per blob. When an image layer exceeds this limit, it will be automatically split into 5MB chunks.
To configure these settings, add them to your .env.dockerize file:
# Cloudflare Tunnel registry settings (for images > 100MB)
BLOB_CHUNK=5242880 # 5MB chunks
BLOB_MAX=104857600 # 100MB max (Cloudflare Tunnel limit)If not specified, the default values will be used.
Your project's Dockerfile can be any valid Dockerfile. The dockerize tool will build whatever is defined in your Dockerfile.
Example Dockerfile structure:
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
FROM node:18-alpine AS runner
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Note: If you use multi-stage builds, the dockerize tool will build the final stage by default. You can specify a target stage by modifying the build command in dockerize.yml.
-
DinD daemon not starting:
- Check if Docker is running on the host
- Ensure the container has privileged access
- Check logs:
docker compose -f dockerize.yml logs dind
-
Registry login fails:
- Verify your credentials in
.env.dockerize - Check if the registry URL is correct
- Ensure your token has push permissions
- Verify your credentials in
-
Build fails:
- Check if your project has a valid Dockerfile
- Verify the build context (current directory)
- Check logs:
docker compose -f dockerize.yml logs builder
-
Push fails:
- Verify registry permissions
- Check network connectivity
- Ensure the image name follows registry naming conventions
To run with more verbose output:
docker compose -f dockerize.yml up --build --no-deps builder- The DinD container runs with privileged access
- Environment files contain sensitive credentials
- Ensure
.env.dockerizeis not committed to version control - Use secure tokens with minimal required permissions
- Consider using Docker secrets for production deployments
To integrate dockerize into your existing project:
-
Copy the dockerize files to your project root:
dockerize.yml(Docker Compose configuration).env.example(Environment template)docker-daemon.json(Docker daemon config)
-
Create your environment file:
cp .env.example .env.dockerize # Edit .env.dockerize with your registry credentials -
Build and push your application:
docker compose -f dockerize.yml up
For local development and testing:
- Use a test registry or local registry
- Set
TAG=devorTAG=testin your.env.dockerize - Use shorter-lived tokens when possible
This setup can be integrated into CI/CD pipelines:
# Example GitHub Actions workflow
- name: Build and Push Docker Image
env:
USER: ${{ secrets.REGISTRY_USER }}
TOKEN: ${{ secrets.REGISTRY_TOKEN }}
TAG: ${{ github.sha }}
run: |
echo "USER=$USER" > .env.dockerize
echo "TOKEN=$TOKEN" >> .env.dockerize
echo "TAG=$TAG" >> .env.dockerize
docker compose -f dockerize.yml up --build[Add your license information here]
[Add contribution guidelines here]