A Dockerfile describes an environment precisely enough that it builds identically on any machine: the same PHP version, the same extensions, the same system packages, every time.

FROM php:7.1-apache
COPY . /var/www/html
RUN docker-php-ext-install mysqli

docker-compose for multi-service projects

Most real projects aren't one container - they're an app server, a database, maybe a cache, all needing to talk to each other. docker-compose defines the whole set in one file and brings them up together with one command:

services:
  web: { build: ., ports: ["8080:80"] }
  db: { image: mysql:5.7, environment: { MYSQL_ROOT_PASSWORD: secret } }

The onboarding instruction for a new developer joining a project has gone from a page of setup steps to docker-compose up.

The problem it actually solves

"Works on my machine" is almost always a difference in Node version, a missing environment variable, or a system dependency installed globally rather than declared anywhere. A Dockerfile makes the entire runtime environment explicit and reproducible, so the container that runs on a laptop is the identical one that runs in staging and production.

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
CMD ["node", "server.js"]

docker-compose for the "it needs a database too" problem

Most real apps aren't a single container - they need a database, maybe Redis, maybe a background worker. docker-compose defines that whole stack in one file so a new team member runs one command and has a fully working local environment, database included, without a page of manual setup instructions that inevitably goes stale.