How to Use Docker Compose Up: Simple Guide
Use
docker compose up to start all services defined in your docker-compose.yml file. This command builds, creates, and runs containers together, making it easy to launch multi-container apps with one command.Syntax
The basic syntax of docker compose up includes optional flags to control its behavior:
docker compose up: Starts containers as defined indocker-compose.yml.--build: Builds images before starting containers.-dor--detach: Runs containers in the background.--force-recreate: Recreates containers even if their configuration hasn't changed.
bash
docker compose up [options]
Example
This example shows how to use docker compose up to start a simple web service with Nginx. It demonstrates building and running the container in the foreground.
yaml
version: '3.8' services: web: image: nginx:alpine ports: - "8080:80"
Output
Creating network "default" with the default driver
Creating web_1 ... done
Attaching to web_1
web_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
web_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
web_1 | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
web_1 | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
web_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
Common Pitfalls
Common mistakes when using docker compose up include:
- Not running
docker compose upin the directory withdocker-compose.yml. - Forgetting to use
-dto run containers in the background, causing the terminal to be blocked. - Not rebuilding images after changes, which can be fixed by adding
--build. - Confusing
docker-compose(legacy) withdocker compose(modern CLI plugin).
Example of wrong and right usage:
bash
# Wrong: Running without -d and expecting terminal free $ docker compose up # Terminal is blocked showing logs # Right: Run in detached mode $ docker compose up -d # Terminal is free, containers run in background
Quick Reference
Here is a quick cheat sheet for common docker compose up options:
| Option | Description |
|---|---|
docker compose up | Start containers as defined in docker-compose.yml |
docker compose up -d | Run containers in background (detached mode) |
docker compose up --build | Build images before starting containers |
docker compose up --force-recreate | Recreate containers even if unchanged |
docker compose up --remove-orphans | Remove containers for services not defined in the compose file |
Key Takeaways
Run
docker compose up in the folder with your docker-compose.yml to start all services.Use
-d to run containers in the background and keep your terminal free.Add
--build to rebuild images if you changed Dockerfiles or configurations.Avoid confusion by using the modern
docker compose command instead of legacy docker-compose.Check your compose file and directory to prevent common errors like missing files or ports conflicts.