Pass args to the Dockerfile from docker-compose
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In the world of containerization, Docker has become a staple technology. It's robust, efficient, and simplifies the deployment of applications across environments. One of the core components of this ecosystem is the Dockerfile, which contains the set of instructions to build a Docker image. On the other hand, `docker-compose` is a tool used to define and run multi-container Docker applications. It allows us to configure the services that make up our application using a `docker-compose.yml` file.
A common requirement is to pass arguments to the Dockerfile from a `docker-compose` configuration. Understanding how to achieve this can significantly enhance your ability to deploy flexible and dynamic containerized applications. This article delves into the intricacies of passing arguments to a Dockerfile using `docker-compose`.
Understanding the Basics
Dockerfile vs. Docker-Compose
- Dockerfile: A script containing a sequence of commands and arguments to build a Docker image. You can specify the base image, copy files, run commands, and set environment variables.
- docker-compose.yml: A YAML file used to define services, networks, and volumes for Docker containers. It provides orchestration of multi-container deployments.
Passing Arguments
Passing arguments (or build arguments) allows you to customize the Docker image at build-time. It is different from environment variables because it applies only during image building and cannot be accessed after the container starts.
Technical Implementation
Defining Arguments in Dockerfile
To use build arguments, define them in the Dockerfile with the `ARG` instruction. Here's an example:
- "5000:5000"
- APP_VERSION
- The Dockerfile contains logic to echo the app version.
- Set `build` properties to pass arguments.
- Ensure that the app version is passed and reflected during image creation.
- Use `docker-compose build` to construct the image with arguments.
- Run the application with `docker-compose up`.
- Toggle debug modes.
- Switch between API keys.
- Selectively install modules based on environment needs.
- Security: Avoid passing sensitive information as build arguments if possible. These values are included in image history.
- Caching: Changing build arguments forces a cache bust, leading to potential rebuilds of later layers in the Dockerfile.
Related reading
- Pass arguments to parent Dockerfile
- Pass AWS credentials IAM role credentials to code running in Docker container
- Pass environment variables from docker-compose to container at build stage
- Passing data between a fragment and its container activity
- Pass multiple variables in helm template
- Pass postgres parameter into Kubernetes deployment
- Passing JVM args to Docker image of Spring boot app on Kubernetes
- Passing long configuration file to Kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.