Hereโs a complete tutorial on docker build, explaining what it does, examples, and use cases.
What is docker build?
docker build is a Docker command used to create a Docker image from a Dockerfile and context (the set of files required to build the image). Once the image is built, it can be used to run containers.
Key Features:
- Builds Docker images from a Dockerfile.
- Supports build-time variables and multi-stage builds for efficient image creation.
- Integrates with CI/CD pipelines for automated image building and deployment.
Basic Syntax
docker build [OPTIONS] PATH | URL | -
Common Options:
-t,--tag: Assign a name and tag to the built image (my_app:latest).-f,--file: Specify an alternate Dockerfile (default isDockerfilein the build context).--no-cache: Build the image without using cache.--build-arg: Pass build-time variables to the image.--progress: Control progress output (auto,plain,tty).--rm: Remove intermediate containers after a successful build (default behavior).
Examples of docker build
1. Build an Image from a Dockerfile
docker build -t my_app:latest .
Code language: CSS (css)
This builds an image from the Dockerfile in the current directory (.) and tags it as my_app:latest.
2. Use an Alternate Dockerfile
docker build -t my_app:dev -f Dockerfile.dev .
Code language: CSS (css)
This uses Dockerfile.dev to build the image and tags it as my_app:dev.
3. Build an Image Without Using Cache
docker build --no-cache -t my_app:latest .
Code language: CSS (css)
This forces Docker to ignore the cache and rebuild every layer.
4. Build an Image with Build-Time Variables
docker build --build-arg ENV=production -t my_app:latest .
Dockerfile example:
ARG ENV
RUN echo "Environment: $ENV"
Code language: PHP (php)
This passes ENV=production to the Dockerfile.
5. Tag an Image with Multiple Tags
docker build -t my_app:1.0 -t my_app:latest .
Code language: CSS (css)
This builds the image and assigns it two tags: 1.0 and latest.
6. Build from a Remote Git Repository
docker build -t my_app https://github.com/user/my_app.git
Code language: JavaScript (javascript)
This builds an image directly from a GitHub repository.
7. Build an Image with a Specific Build Context
docker build -t my_app /path/to/context
This uses /path/to/context as the build context for the image.
8. Use BuildKit for Faster Builds
DOCKER_BUILDKIT=1 docker build -t my_app:latest .
BuildKit is a modern builder that speeds up builds and supports advanced features like parallel builds and caching.
9. Monitor Build Progress in Plain Text
docker build --progress=plain -t my_app .
This displays the build progress in plain text, useful for debugging.
10. Save and Load Build Cache for CI/CD
Save cache:
docker build --build-arg BUILDKIT_INLINE_CACHE=1 -t my_app .
docker save my_app:latest > my_app.tar
Load cache:
docker load < my_app.tar
Code language: CSS (css)
11. Build with Multi-Stage Dockerfile
Multi-stage builds help reduce image size by separating build and runtime environments.
Dockerfile example:
# Stage 1: Build
FROM golang:1.17 as builder
WORKDIR /app
COPY . .
RUN go build -o my_app .
# Stage 2: Runtime
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/my_app .
CMD ["./my_app"]
Code language: PHP (php)
Build the image:
docker build -t my_app:latest .
Code language: CSS (css)
Use Cases for docker build
1. Creating Custom Application Images
- Build images with your application code, dependencies, and configurations.
- Example: Build a Node.js or Python application with required libraries.
2. Continuous Integration and Deployment (CI/CD)
- Use
docker buildin CI/CD pipelines to automate image creation and deployment. - Example: Build and push images to Docker Hub after every code commit.
3. Multi-Stage Builds for Optimized Images
- Reduce image size by using multi-stage builds.
- Example: Use a builder image for compiling code and a smaller image for the final runtime.
4. Environment-Specific Builds
- Use build-time arguments to create different versions of images for development, staging, and production.
- Example: Pass
ENV=productionto enable production-specific configurations.
5. Containerized Testing Environments
- Build images with test dependencies for automated testing.
- Example: Build a custom image with Cypress or Selenium for UI testing.
6. Building Static Websites
- Use Docker to build and deploy static websites with NGINX or Apache.
7. Packaging Microservices
- Package each microservice in its own Docker image for deployment in a microservices architecture.
List of Common docker build Commands
| Command | Description |
|---|---|
docker build -t my_app:latest . | Build an image from the current directory |
docker build -f Dockerfile.dev -t my_app:dev . | Build using an alternate Dockerfile |
docker build --no-cache -t my_app . | Build without using cache |
docker build --build-arg ENV=production . | Build with a build-time argument |
docker build -t my_app https://github.com/user/repo.git | Build from a remote Git repository |
docker build --progress=plain . | Show build progress in plain text |
docker build -t my_app /path/to/context | Build with a specific build context |
DOCKER_BUILDKIT=1 docker build -t my_app . | Enable BuildKit for faster builds |
Best Practices for Using docker build:
- Use multi-stage builds to keep images small and secure.
- Leverage caching for faster builds, especially for dependency installation.
- Use
.dockerignoreto exclude unnecessary files and reduce build context size. - Tag images properly with version numbers and environment-specific tags (
dev,staging,prod). - Automate builds in CI/CD pipelines for consistent image creation.
- Minimize layers in Dockerfiles to improve build performance and reduce image size.
Common Errors and Solutions
- “No such file or directory”
โ Ensure the Dockerfile and build context path are correct. - “Failed to fetch base image”
โ Check your network connection and Docker registry access. - “Cache not used”
โ Ensure the instructions in the Dockerfile are written in a way that maximizes caching (place frequently changed steps at the end). - “Image size too large”
โ Use multi-stage builds and a smaller base image (e.g.,alpine).
Combining docker build with Other Commands
Push Built Image to Docker Hub
docker build -t my_app:latest .
docker tag my_app:latest myusername/my_app:latest
docker push myusername/my_app:latest
Automate Builds in GitHub Actions
name: Docker Build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Build Docker Image
run: docker build -t my_app:latest .
- name: Push Docker Image
run: docker push myusername/my_app:latest
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals