Docker-compose is a powerful tool for creating and managing Docker containers. This tool allows you to create, run, and manage multiple containers that interact with each other. In this article, we will look at several commands that can be useful when working with Docker-compose.
So, let's get started.
Commands for managing containers:
docker-compose up: to start all containers defined in the docker-compose.yml file.
For example, the command "docker-compose up" will start all containers from the docker-compose.yml file in the current directory.
- docker-compose down: to stop and remove all containers defined in the docker-compose.yml file.
For example, the command "docker-compose down" will stop and remove all containers that were started using the docker-compose.yml file.
Commands for managing services:
docker-compose ps: to display a list of running containers and their statuses.
For example, the command "docker-compose ps" will show all running containers from the docker-compose.yml file.
- docker-compose up -d [SERVICE]: to start a specific service from the docker-compose.yml file.
For example, the command "docker-compose up nextcloud" will start only the nextcloud container from the docker-compose.yml file that was stopped.
Commands for managing images:
docker-compose pull: to download all images defined in the docker-compose.yml file. For example, the command "docker-compose pull" will download all images specified in the docker-compose.yml file.
- docker-compose build: to build images defined in the docker-compose.yml file. For example, the command "docker-compose build" will build all images specified in the docker-compose.yml file.
Commands for managing logs:
docker-compose logs: to view the logs of all containers defined in the docker-compose.yml file. For example, the command "docker-compose logs" will output the logs of all containers from the docker-compose.yml file.
- docker-compose logs [SERVICE]: to view the logs of a specific service from the docker-compose.yml file. For example, the command "docker-compose logs nextcloud_db" will output the logs of only the database container from the docker-compose.yml file.
Commands for managing running containers:
docker-compose stop: to stop all containers defined in the docker-compose.yml file without removing them. For example, the command "docker-compose stop" will stop all containers from the docker-compose.yml file.
- docker-compose start: to start all stopped containers defined in the docker-compose.yml file. For example, the command "docker-compose start" will start all stopped containers from the docker-compose.yml file.
- docker-compose up --force-recreate forces Docker Compose to recreate containers even if their configuration or image has not changed. This means that Compose will stop and remove existing containers for the services specified in the docker-compose.yml file, and then create new containers from scratch using the latest version of the images defined in the file. Using the --force-recreate option is useful in situations where you have made changes to the docker-compose.yml file that affect the container configuration, such as environment variables or port mappings.
Commands for managing networks:
- docker-compose network ls: to display a list of networks defined in the docker-compose.yml file if any. You can also use the command docker network ls.
- docker-compose network create [NETWORK]: to create a new network in the docker-compose.yml file. For example, the command "docker-compose network create frontend" will create a new network named frontend in the docker-compose.yml file.
- docker-compose network rm [NETWORK]: to remove a network defined in the docker-compose.yml file. For example, the command "docker-compose network rm frontend" will remove the network named frontend from the docker-compose.yml file.
Commands for managing environment variables:
docker-compose config: to check the correctness of the docker-compose.yml file and display all environment variables defined in it. For example, the command "docker-compose config" will check the correctness of the docker-compose.yml file and show all environment variables defined in it.
- docker-compose exec [SERVICE] [COMMAND]: to execute a command inside a container defined in the docker-compose.yml file. For example, the command "docker-compose exec nextcloud ls" will execute the command "ls" inside the nextcloud container defined in the docker-compose.yml file.
Commands for managing volumes:
docker-compose volume ls: to display a list of all volumes defined in the docker-compose.yml file. For example, the command "docker-compose volume ls" or "docker volume ls" will show all volumes defined in the docker-compose.yml file.
- docker-compose volume create [VOLUME]: to create a new volume defined in the docker-compose.yml file. For example, the command "docker-compose volume create db_data" will create a new volume named db_data in the docker-compose.yml file.
- docker-compose volume rm [VOLUME]: to remove a volume defined in the docker-compose.yml file. For example, the command "docker-compose volume rm db_data" will remove the volume named db_data from the docker-compose.yml file.
In this article, we reviewed the basic commands for working with Docker Compose, such as starting and stopping containers, creating networks and volumes, as well as managing volumes. Familiarity with these commands will help you use Docker Compose more effectively when developing and deploying applications in containers.
We also suggest looking at other useful articles:
- How to deploy a Python application to Docker-Compose
- How to deploy any version of PHP in Docker-Compose
- How to install Redis in Docker and Docker Compose