Docker Compose
Estimated reading: 4 minutes 66 views

🐳 Using Build, Image, and Container Name in Docker Compose: A Step-by-Step Guide

🧩 Introduction

Docker Compose is an essential tool for managing multi-container Docker applications. It enables developers to define and orchestrate services using a simple YAML file (docker-compose.yml).

In this guide, we’ll focus on three vital directives:

  • πŸ”§ build – Defines how to build a container image.
  • πŸ“¦ image – Points to a pre-built or custom image.
  • 🏷️ container_name – Assigns a custom name to the container.

These components ensure your containers are built, named, and deployed precisely as needed. Let’s break them down with practical examples and usage tips.


πŸ”§ build: How to Define the Build Context

The build directive is used to create an image from source files. It defines the context (directory where the Dockerfile and app files live) and optionally specifies a custom Dockerfile.

βœ… Example:

services:
  web:
    build:
      context: ./app
      dockerfile: Dockerfile.dev
πŸ”Ή PropertyπŸ“ Description
contextPath to the directory containing the Dockerfile and source code
dockerfile(Optional) Specify a custom Dockerfile name

πŸ’‘ Pro Tip: Use different Dockerfiles for development (Dockerfile.dev) and production (Dockerfile.prod) to customize each environment.

πŸ“ Note: You can combine build with args, target, and cache_from for more control over the image build process.


πŸ“¦ image: Using Pre-Built or Custom Images

The image directive defines which Docker image to use. It can reference:

  • Images pulled from a remote registry (like Docker Hub)
  • Locally built images with custom tags

βœ… Example:

services:
  web:
    image: my-custom-image:v1
πŸ”Ή ElementπŸ“ Description
imageThe image name (with optional tag) to use for the container
latestRefers to the most recent version (not recommended for production use)

πŸ’‘ Pro Tip: Use version-specific tags (my-app:v1.2.0) in production for consistency.

⚠️ Gotcha: If you specify both build and image, Docker Compose builds the image first, then tags it with the name in image.


🏷️ container_name: Assigning Custom Names to Containers

Docker Compose usually creates automatic container names like project_service_1. Using container_name, you can set a human-friendly name for easier reference in logs or commands.

βœ… Example:

services:
  web:
    container_name: my-web-container
πŸ”Ή FieldπŸ“ Description
container_nameManually assigns a name to the container
Default behaviorWithout this, names are auto-generated as folder_service_index

πŸ’‘ Pro Tip: This is especially useful for shell commands, like:

docker exec -it my-web-container bash

πŸ“ Note: Once set, Docker Compose always uses the specified name, even if the service or project name changes.


πŸ“Š Quick Comparison Table – build vs image vs container_name

πŸ”§ DirectiveπŸ” Purposeβœ… Best Use Case
buildBuilds a new image from a Dockerfile and contextUse when building custom images
imageSpecifies which image to use (prebuilt or custom tag)Use for stable, reusable image references
container_nameAssigns a fixed name to the containerUse for convenience and shell access

✨ Visual Elements Used in This Guide

To enhance readability, we’ve used:

βœ… Checklists – For best practices
πŸ“¦ Code blocks – For real YAML usage
⚠️ Callouts – For warnings and gotchas
πŸ“Š Comparison tables – For quick decision-making

These help both beginners and professionals absorb the content quickly and effectively.


🧩 Final Thoughts

Using build, image, and container_name in Docker Compose gives you full control over how your containers are created, labeled, and managed. These directives are foundational to building robust Docker applicationsβ€”whether you’re crafting a small dev setup or a complex multi-service deployment.

🎯 By following best practices:

  • You’ll make your app builds consistent and reproducible.
  • Your containers will have meaningful names.
  • You’ll avoid common misconfigurations and bugs.

πŸ› οΈ Ready to apply your knowledge? Try building your first multi-service Compose file today!


❓ Frequently Asked Questions (FAQs)

❓ What is the difference between build and image in Docker Compose?

βœ… build is used to create a new image from source code, while image refers to an existing image pulled from a registry or built locally.


❓ Can I use both build and image in the same service?

βœ… Yes. If both are present, Docker Compose will build the image and tag it using the image name.


❓ How do I assign a custom name to a container?

βœ… Use the container_name directive under the service. This sets the container name directly.


❓ How do services communicate when using different images?

βœ… Docker Compose creates a shared network. Services can reference each other by service name, regardless of whether they use build or image.


❓ Is it safe to use latest tag in production?

⚠️ No. Avoid using latest in production. Always use pinned version tags to ensure consistency.


❓ How can I push a custom-built image to a registry?

βœ… After using build, tag and push the image manually or via CI/CD:

docker tag my-image:v1 my-registry.com/my-image:v1
docker push my-registry.com/my-image:v1

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Using Build, Image, and Container Name in Docker Compose

Or Copy Link

CONTENTS
Scroll to Top