Managing Docker Compose Services
Once you have configured your Docker Compose environment, you can use various commands to manage the running services. Here are some common tasks you can perform:
Listing Running Services
To list the currently running services, use the following command:
$ docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------
project_db_1 docker-entrypoint.sh mysql ... Up 3306/tcp
project_web_1 nginx -g daemon off
Up 0.0.0.0:80- > 80/tcp
This command will display the name, command, state, and exposed ports of each running service.
Viewing Service Logs
To view the logs of a specific service, use the following command:
$ docker-compose logs web
Attaching to project_web_1
project_web_1 | 172.18.0.1 - - [07/Apr/2023:12:34:56 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
Replace web
with the name of the service you want to view the logs for.
Scaling Services
To scale a specific service, use the following command:
$ docker-compose up --scale web=3 -d
Creating and starting project_web_2 ... done
Creating and starting project_web_3 ... done
This command will scale the web
service to 3 replicas.
Stopping and Restarting Services
To stop a specific service, use the following command:
$ docker-compose stop web
Stopping project_web_1 ... done
To restart a specific service, use the following command:
$ docker-compose restart web
Restarting project_web_1 ... done
By understanding these basic commands, you can effectively manage your Docker Compose services and ensure the smooth operation of your multi-container applications.