Best Practices and Real-World Scenarios
Now that you understand the docker-compose down
command and its options, let's explore some best practices and real-world scenarios for using this command effectively.
Creating a More Complex Docker Compose Environment
To better demonstrate real-world usage, let's create a more complex Docker Compose environment. We'll set up a simple web application with a frontend, backend, and database.
cd ~/project/docker-compose-demo
nano docker-compose.yml
Replace the content with:
version: "3"
services:
frontend:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- frontend_data:/usr/share/nginx/html
networks:
- app_network
backend:
image: node:14-alpine
command: sh -c "echo 'Backend service running' && sleep infinity"
volumes:
- backend_data:/app
networks:
- app_network
- db_network
database:
image: postgres:13-alpine
environment:
POSTGRES_PASSWORD: example
POSTGRES_USER: user
POSTGRES_DB: appdb
volumes:
- db_data:/var/lib/postgresql/data
networks:
- db_network
networks:
app_network:
db_network:
volumes:
frontend_data:
backend_data:
db_data:
Save and exit the editor.
Let's start this more complex environment:
docker-compose up -d
You should see output showing the creation of networks, volumes, and containers for all three services.
Best Practice: Using Environments
In real-world scenarios, you might have different environments like development, testing, and production. Docker Compose allows you to use different configuration files for different environments.
Create a file for development environment:
nano docker-compose.dev.yml
Add the following content:
version: "3"
services:
frontend:
ports:
- "8081:80"
environment:
NODE_ENV: development
backend:
environment:
NODE_ENV: development
DEBUG: "true"
database:
ports:
- "5432:5432"
Save and exit the editor.
To use this file in combination with the base file, you can use the -f
flag:
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
This will merge the configurations, applying the development-specific settings.
To bring down this environment, you would use:
docker-compose -f docker-compose.yml -f docker-compose.dev.yml down
Best Practice: Using a Cleanup Script
In continuous integration/deployment (CI/CD) pipelines or development workflows, it's often useful to have a cleanup script that removes all Docker resources. Let's create a simple cleanup script:
nano cleanup.sh
Add the following content:
#!/bin/bash
echo "Cleaning up Docker environment..."
## Stop and remove containers, networks, volumes, and images
docker-compose down --volumes --rmi all --remove-orphans
## Remove dangling volumes
echo "Removing dangling volumes..."
docker volume prune -f
## Remove dangling images
echo "Removing dangling images..."
docker image prune -f
echo "Cleanup complete!"
Save and exit the editor.
Make the script executable:
chmod +x cleanup.sh
Now you can run this script whenever you need to do a complete cleanup:
./cleanup.sh
Best Practice: Selective Resource Removal
Sometimes you may want to remove only specific resources. For example, you might want to keep your volumes (to preserve data) but remove containers, networks, and images.
Here's how you would approach different scenarios:
-
Remove only containers and networks (preserve volumes and images):
docker-compose down
-
Remove containers, networks, and images (preserve volumes):
docker-compose down --rmi all
-
Remove containers, networks, and volumes (preserve images):
docker-compose down --volumes
-
Remove only local images (not pulled from a registry):
docker-compose down --rmi local
By selectively removing resources, you can optimize your workflow based on your specific needs.
Best Practice: Monitoring Resource Usage
Before and after running docker-compose down
, it's often helpful to monitor your Docker resource usage. This can help you identify any resources that aren't being properly cleaned up.
Here are some useful commands:
-
List all containers (including stopped ones):
docker ps -a
-
List all networks:
docker network ls
-
List all volumes:
docker volume ls
-
List all images:
docker image ls
-
Get system-wide information:
docker system df
Let's try the last command to see our current resource usage:
docker system df
You'll see a summary of your Docker resource usage, including the number of containers, images, volumes, and the total amount of space used.
Bringing Down Our Complex Environment
Now, let's bring down our complex environment and remove all associated resources:
docker-compose down --volumes --rmi all --remove-orphans
This will stop and remove all containers, networks, volumes, and images associated with our Docker Compose project.
By following these best practices, you can effectively manage your Docker environments and ensure optimal resource usage.