Two-service Compose file
services:
web:
image: nginx:alpine
ports:
- "8080:80"
cache:
image: redis:7-alpine- web is published to the host.
- cache is reachable by other services on the Compose network without a host port.
Define a multi-container application declaratively with services, networks, volumes, environment and lifecycle commands.
A Dockerfile describes image build instructions. A Compose file describes running services and how they connect to networks, volumes, environment and published ports. A Compose service may reference a prebuilt image or build from a Dockerfile.
docker compose up reads the desired service definition and creates or reconciles containers, networks and volumes. docker compose down removes the application containers and default network; adding -v also removes declared named volumes and therefore deserves caution.
Compose normally creates a default network for the project. Services attached to that network can discover one another using service names. This reduces dependence on container IP addresses.
services:
web:
image: nginx:alpine
ports:
- "8080:80"
cache:
image: redis:7-alpinedocker compose config
docker compose up -d
docker compose ps
docker compose logs
docker compose downGoal: Use Compose to start nginx and Redis, then prove both services are on one application network.
mkdir -p ~/docker-fundamentals/compose && cd ~/docker-fundamentals/compose
cat > compose.yaml <<'EOF'
services:
web:
image: nginx:alpine
ports:
- "8080:80"
cache:
image: redis:7-alpine
EOFdocker compose configdocker compose up -d
docker compose pscurl -I http://127.0.0.1:8080/
docker inspect "$(docker compose ps -q web)" --format '{{json .NetworkSettings.Networks}}'
docker inspect "$(docker compose ps -q cache)" --format '{{json .NetworkSettings.Networks}}'
docker compose exec cache redis-cli pingdocker compose downCompose provides a reproducible local integration environment and is also useful for small single-host deployments when its operational tradeoffs are acceptable.
Open each answer only after you have tried to answer the question yourself.
Dockerfile builds an image; Compose describes how one or more services run together.
By service name using Docker-provided DNS.
Because it removes named volumes declared/used by the Compose project, which can delete persistent data.
Progress is saved in this browser immediately. When you sign in to My Learning, it can also sync across devices.
Attempt the core lab and knowledge checks before marking the module complete.