Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker build with examples

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 is Dockerfile in 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 .

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 .

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 .

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"

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 .

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

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

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"]

Build the image:

docker build -t my_app:latest .

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 build in 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=production to 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

CommandDescription
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.gitBuild from a remote Git repository
docker build --progress=plain .Show build progress in plain text
docker build -t my_app /path/to/contextBuild with a specific build context
DOCKER_BUILDKIT=1 docker build -t my_app .Enable BuildKit for faster builds

Best Practices for Using docker build:

  1. Use multi-stage builds to keep images small and secure.
  2. Leverage caching for faster builds, especially for dependency installation.
  3. Use .dockerignore to exclude unnecessary files and reduce build context size.
  4. Tag images properly with version numbers and environment-specific tags (dev, staging, prod).
  5. Automate builds in CI/CD pipelines for consistent image creation.
  6. Minimize layers in Dockerfiles to improve build performance and reduce image size.

Common Errors and Solutions

  1. “No such file or directory”
    → Ensure the Dockerfile and build context path are correct.
  2. “Failed to fetch base image”
    → Check your network connection and Docker registry access.
  3. “Cache not used”
    → Ensure the instructions in the Dockerfile are written in a way that maximizes caching (place frequently changed steps at the end).
  4. “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

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x