2025-02-06
Docker Compose is a powerful tool for defining and running multi-container Docker applications. With a single `docker-compose.yml` file, you can configure your application’s services, networks, and volumes, making it easier to manage complex setups. Below are some practical examples and commands to help you get started with Docker Compose.
Basic Docker Compose Commands
1. Create a `docker-compose.yml` File
This file defines the services, networks, and volumes for your application. Here’s an example for a simple web application with a web server and a database:
version: '3' services: web: image: nginx ports: - "80:80" volumes: - ./html:/usr/share/nginx/html db: image: mysql environment: MYSQL_ROOT_PASSWORD: example
2. Start the Application
Use the following command to start all the services defined in your `docker-compose.yml` file:
docker-compose up
To run in detached mode (background), use:
docker-compose up -d
3. Stop the Application
To stop the running services, use:
docker-compose down
4. View Logs
To view the logs of a specific service, use:
docker-compose logs <service_name>
5. Rebuild and Restart
If you make changes to your `docker-compose.yml` file or Dockerfiles, you can rebuild and restart the services with:
docker-compose up --build
6. Scale Services
You can scale a specific service to multiple instances. For example, to scale the `web` service to 3 instances:
docker-compose up --scale web=3
7. Check Service Status
To see the status of your running services, use:
docker-compose ps
8. Execute Commands in a Running Container
To run a command inside a running container, use:
docker-compose exec <service_name> <command>
For example, to open a bash shell in the `web` service:
docker-compose exec web bash
9. Remove Unused Resources
To remove stopped containers, unused networks, and dangling images, use:
docker-compose down --volumes --rmi all
10. Environment Variables
You can use environment variables in your `docker-compose.yml` file for dynamic configurations. For example:
version: '3' services: web: image: nginx environment: - NGINX_HOST=example.com
Advanced Docker Compose Features
- Networks: Define custom networks for your services to communicate securely.
- Volumes: Use volumes to persist data and share files between containers.
- Health Checks: Implement health checks to ensure your services are running correctly.
- Dependencies: Define service dependencies to control the startup order.
What Undercode Say
Docker Compose is an essential tool for modern DevOps practices, enabling seamless management of multi-container applications. By leveraging its features, you can streamline your development, testing, and deployment workflows. Below are some additional Linux and Docker commands to enhance your understanding:
1. List All Docker Containers
docker ps -a
2. Remove All Stopped Containers
docker container prune
3. Inspect Docker Network
docker network inspect <network_name>
4. Backup Docker Volumes
docker run --rm -v <volume_name>:/volume -v $(pwd):/backup busybox tar cvf /backup/backup.tar /volume
5. Restore Docker Volumes
docker run --rm -v <volume_name>:/volume -v $(pwd):/backup busybox tar xvf /backup/backup.tar -C /volume
6. Monitor Docker Resource Usage
docker stats
7. Update Docker Images
docker-compose pull
8. Use Docker Swarm for Orchestration
docker swarm init
9. Deploy a Stack in Docker Swarm
docker stack deploy -c docker-compose.yml <stack_name>
10. Rolling Updates in Docker Swarm
docker service update --image <new_image> <service_name>
For further reading, check out the official Docker Compose documentation:
https://docs.docker.com/compose/
By mastering Docker Compose and related Linux commands, you can significantly improve your IT infrastructure management and ensure robust, scalable, and efficient application deployments.
References:
Hackers Feeds, Undercode AI