Guide
Docker Compose vs Dockerfile: Which Configuration Does What?
A Dockerfile builds an image; Docker Compose defines how one or more containers run together, including their networks, volumes, ports, and environment.
What does a Dockerfile control?
A Dockerfile builds an image. It records the base image, packages, copied application files, working directory, default command, and other instructions needed to reproduce that image through Docker's build process.
The distinction matters. A Dockerfile is a simple text file that contains commands a user could otherwise call manually, but Docker executes those commands as ordered build steps and records the resulting filesystem layers. The common Dockerfile name has no extension.
A small example looks like this:
FROM python:3.13-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]Run docker build -t example-app . in that directory. Docker reads the instructions, sends the required build context, executes the build command, and creates an image named example-app.
Order has consequences. Copying the entire application before installing dependencies causes that dependency layer to be rebuilt whenever any source file changes, while copying requirements.txt first lets Docker reuse the package layer until the file changes. Docker's Dockerfile build documentation explains the instruction model and build context.
Keep runtime settings out. Ports published on the host, production passwords, bind-mounted source directories, and a database container's persistent volume describe a deployment environment, not the application image. Baking them into the image produces a less portable artifact and often exposes configuration that should have remained outside it.
Dockerfile vs Docker image: what is the difference?
The file is a recipe. The image is the immutable, built artifact produced from that recipe, containing filesystem layers and metadata from which Docker can create containers repeatedly.
That relationship is similar to source code and a compiled binary, although the analogy breaks down because an image may include an operating-system userspace, package manager files, configuration defaults, and the application itself. Changing a Dockerfile does nothing to an existing image until another build is performed.
Images are layered. Each filesystem-changing instruction generally contributes a layer, and Docker can reuse unchanged layers automatically across builds or related images. A container adds a writable layer above those read-only image layers; deleting the container deletes that writable layer unless important data was placed in a volume.
This catches people. Editing Dockerfile, then running an old image tag without rebuilding it, still launches the old application. Compose can hide that mistake because docker compose up does not promise to rebuild every image merely because local files changed. Use docker compose up --build when the service has a build definition and the image must reflect current source or dependency instructions.
The practical rule is blunt: put repeatable installation and filesystem changes in the Dockerfile, then put machine-specific runtime configuration elsewhere. Docker's explanation of image layers and containers shows why container edits do not rewrite the underlying image.
What does Docker Compose add?
Compose defines the running application. Its YAML file describes services, images or build directories, networks, volumes, ports, dependencies, environment values, health checks, and other settings for one or multiple containers.
Dockerfiles and Docker Compose YAML files have different functions: a Dockerfile provides instructions to build a container image, while Compose declares which containers should be created from images and how those containers connect. Compose can point at a Dockerfile, but it does not replace one.
Consider a web application and PostgreSQL database. The application service can be built from the current directory, while the database uses a published image and stores its files in a named volume:
services:
web:
build: .
ports:
- "8080:8000"
environment:
DATABASE_URL: postgresql://app:change-me@db/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: change-me
POSTGRES_DB: app
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 5s
timeout: 3s
retries: 10
volumes:
db-data:Run it with docker compose up --build. Compose builds the web image, creates a default project network, starts both services, and gives each service a DNS name matching its service key. Inside web, the database host is db, not localhost; localhost points back to the web container itself.
There is a trap in depends_on. Startup order alone does not prove that a database is ready to accept connections, so the example uses a health check and the service_healthy condition rather than trusting process creation. The Compose application model documents how a Compose file becomes services, networks, volumes, configurations, and secrets.
Do not commit that example password. Real projects should source sensitive values from an external secret mechanism or use the patterns covered in Docker Compose secrets, because environment variables can leak through inspection output, logs, and careless diagnostics.
Docker-compose vs docker run: what changes?
Both create containers. docker run expresses one container's runtime configuration on the command line, while Docker Compose stores related service configuration in a YAML file and manages the resulting application as a project.
A direct command might be:
docker run --name web -p 8080:8000 -e APP_ENV=development example-appThat is fine for a quick check. The trouble begins when the command acquires six environment flags, two volumes, a custom network, a restart policy, and a database that must be launched with another equally fragile command. Shell history is not deployment configuration.
Compose makes those choices reviewable. A teammate can run docker compose up from the same directory and receive the defined services, network attachments, named volumes, port mappings, and default settings without reconstructing a command from a wiki page.
The underlying mechanism remains similar. Compose calls Docker APIs to create networks, volumes, and containers; it is not a separate container runtime, and a container created by Compose is still visible through ordinary commands such as docker ps and docker inspect. Docker documents the flags and lifecycle behind running containers.
Use docker run for disposable experiments, one-off diagnostic commands, and a single container whose options fit comfortably in one line. Use Compose for multi-container applications or any repeatable local environment where losing one flag would change behavior. I would not maintain a hand-written startup script full of docker run commands once Compose can state the same configuration directly.
FAQ
- Docker compose yml example
- A useful Compose YAML example defines a service and names an image or build directory. Save this as
compose.yaml, then rundocker compose up. ```yaml services: app: build: . ports: - "3000:3000" ``` Compose builds the Dockerfile in the current directory and publishes container port 3000 on host port 3000. - Can Docker Compose work without a Dockerfile?
- Yes, Compose needs no Dockerfile when every service uses an existing image. A service containing
image: redis:8can be pulled and started directly; a Dockerfile becomes necessary only when you need to build a custom image or modify an existing one reproducibly. - Should production deployments use Docker Compose?
- Compose can run a production application on one Docker host, but it is not a multi-node scheduler. It works well for modest single-server deployments with controlled updates and backups; applications needing placement across hosts, automatic rescheduling after machine failure, or cluster-wide scaling need an orchestrator such as Docker Swarm or Kubernetes.
- Does Docker Compose rebuild an image after source changes?
- Not reliably unless requested. Run
docker compose up --buildto rebuild services with abuildconfiguration, and remember that bind-mounted development source can appear inside a running container without an image rebuild while files copied by the Dockerfile cannot.