docker-compose
The docker-compose command is used to define and run multi-container Docker applications. With docker-compose, you can use a YAML file to configure your application's services, networks, and volumes, and then start all the services with a single command.
Basic Syntax
Key Commands and Options
up: Create and start containers.
docker-compose up [OPTIONS] [SERVICE...]Options:
-d, --detach: Run containers in the background.
docker-compose up -d--build: Build images before starting containers.
docker-compose up --build
down: Stop and remove containers, networks, images, and volumes.
docker-compose down [OPTIONS]Options:
-v, --volumes: Remove named volumes declared in the
volumessection of the Compose file and anonymous volumes attached to containers.docker-compose down -v
build: Build or rebuild services.
docker-compose build [OPTIONS] [SERVICE...]Options:
--no-cache: Do not use cache when building the image.
docker-compose build --no-cache
ps: List containers.
docker-compose ps [OPTIONS] [SERVICE...]logs: View output from containers.
docker-compose logs [OPTIONS] [SERVICE...]Options:
-f, --follow: Follow log output.
docker-compose logs -f
exec: Execute a command in a running container.
docker-compose exec [OPTIONS] SERVICE COMMAND [ARGS...]Options:
-d, --detach: Run command in the background.
docker-compose exec -d web bash
Examples
Starting Services
docker-compose upThis command starts all the services defined in the
docker-compose.ymlfile.Starting Services in Detached Mode
docker-compose up -dThis command starts all the services in the background.
Stopping and Removing Services
docker-compose downThis command stops and removes all the services, networks, and volumes defined in the
docker-compose.ymlfile.Building Services
docker-compose buildThis command builds the images for the services defined in the
docker-compose.ymlfile.Listing Running Containers
docker-compose psThis command lists all the running containers for the services defined in the
docker-compose.ymlfile.Viewing Logs
docker-compose logsThis command shows the logs for all the services defined in the
docker-compose.ymlfile.Executing a Command in a Running Container
docker-compose exec web bashThis command opens a bash shell in the running container for the
webservice.
Conclusion
The docker-compose command is essential for managing multi-container Docker applications. Understanding its commands and options allows you to effectively define, start, stop, and manage your Docker services.