Back to Blog
Lesson 12 of the Docker: Containers & Your First Image course
DevOpsJuly 30, 20263 min read

Building Your First Image: Mastering the Docker Build Command

Learn how to transform a Dockerfile into a runnable image. We cover the docker build command, tagging best practices, and verifying your work in production.

dockercontainersdevopsdocker buildimage creation
Shipping containers and cranes at Hamburg port showcasing global trade.

Previously in this course, we explored the Anatomy of a Dockerfile: Building Custom Images from Scratch to define our environment's configuration. In this lesson, we move from text files to executable artifacts by using the docker build command to turn those instructions into a ready-to-run image.

From Dockerfile to Image

A Dockerfile is just a recipe. The docker build process is the "chef" that follows that recipe, step-by-step, to create a snapshot of an environment. When you run a build, Docker executes every directive in your file, creating an immutable layer for each step.

By the end of this process, you will have a local image that you can deploy anywhere the Docker engine is running.

Executing the Build Command

To build an image, navigate to the directory containing your Dockerfile and run the following command:

Bash
docker build -t my-app:v1 .

Let's break down this command:

  • docker build: The primary command for triggering image creation.
  • -t my-app:v1: This is the tagging flag. It names your image (my-app) and assigns it a version or label (v1). Without this, your image would be an anonymous hash, which is difficult to manage.
  • .: The trailing dot tells Docker to look for the Dockerfile in the current directory (the "build context").

Verifying Image Creation

Once the build process completes—you will see a stream of output showing each step—you need to confirm the image exists in your local registry. Use the list command:

Bash
docker images

You should see an output table similar to this:

REPOSITORYTAGIMAGE IDCREATEDSIZE
my-appv1a1b2c3d4e5f610 seconds ago120MB

If you see your image listed, the build was successful. You can now use this image name in a docker run command as we did in Running Hello World: Your First Docker Container Lifecycle.

Hands-on Exercise

Now it is your turn to build the foundation for our running project.

  1. Create a folder named web-service.
  2. Inside, create a Dockerfile with the following content:
    Dockerfile
    FROM nginx:alpine
    COPY index.html /usr/share/nginx/html/index.html
  3. Create a simple index.html file in the same folder with the text: <h1>Hello World</h1>.
  4. Run the build command: docker build -t web-app:latest .
  5. Verify the image exists by running docker images.

Common Pitfalls

  • Forgetting the Context: If you run docker build . from the wrong directory, Docker won't find your Dockerfile. Always ensure your terminal path matches the project folder.
  • The "Latest" Trap: While -t app:latest is convenient, it can cause confusion in production. Always version your images (e.g., :v1.0.1) so you know exactly which code is running.
  • Build Context Bloat: If you have large files (like .git folders or local logs) in the same directory, Docker will attempt to copy them into the build context, making your builds slow. Use a .dockerignore file to exclude unnecessary files.

FAQ

Q: What happens if I build with the same name twice? A: If you build my-app:v1 again, the old v1 image will be untagged (becoming a "dangling" image with an <none> tag) and the new build will take the v1 label.

Q: Can I build without a tag? A: Yes, but it is discouraged. You would get an image with no name, making it very hard to refer to when you want to start a container.

Q: Does the order of instructions in the Dockerfile matter? A: Yes, it matters for both build speed and image size. We will cover this in detail when we discuss Docker Image Optimization: Reduce Build Size and Speed Up Deployments.

Recap

You have now successfully transformed a text-based configuration into a functional, runnable image. By using the -t flag for naming and tagging, you have ensured your image is identifiable and ready for future iterations. You have verified your work using docker images and are now ready to understand how Docker caches these steps to make subsequent builds faster.

Up next: Understanding Image Layers

Similar Posts