Managing Containers with Compose
Docker Compose provides a set of commands that allow you to manage the lifecycle of your multi-container applications. Let's explore some of the most common Compose commands.
Starting and Stopping Containers
To start your application, use the docker-compose up
command:
docker-compose up -d
The -d
flag runs the containers in detached mode, allowing you to continue using the terminal. To stop the application, use the docker-compose down
command:
docker-compose down
This will stop and remove all the containers, networks, and volumes defined in your Compose file.
Viewing Container Logs
To view the logs for your application, use the docker-compose logs
command:
docker-compose logs web
docker-compose logs -f
The first command will show the logs for the web
service, while the second command will follow the logs for all services.
Executing Commands in Containers
You can execute commands inside your running containers using the docker-compose exec
command:
docker-compose exec web bash
docker-compose exec db mysql -u root -p
These commands will open a bash shell in the web
container and connect to the MySQL database in the db
container, respectively.
Scaling Services
Docker Compose makes it easy to scale your services up or down. To scale a service, use the docker-compose scale
command:
docker-compose scale web=3 db=2
This will create three instances of the web
service and two instances of the db
service.
By using these Compose commands, you can easily manage the lifecycle of your multi-container applications, from starting and stopping them to viewing logs and executing commands inside the containers.