Let's cover a useful list of docker-compose commands to get started with docker compose.
Starting containers
The most popular command is the docker-compose up
command. This command is used to run services defined in the docker compose file. When this command is cancelled, all running containers are stopped. When run without arguments, it creates containers for all services defined in the compose file and uses a file named docker-compose.yml
. This is the default file used by docker compose. There are many available options but we will cover only the most used in this article.
The up
command Options
1. -f
This option is used to specify the docker compose file to use. If your file is named docker-compose.yml, this option is not needed. See a full example below.
docker-compose up -f [file name]
Note: This option can be used multiple times to specify multiple compose file. WHen multiple compose files are used, their configurations are merged. When using multiple files, configuration of a file on the right overrides those of a file defined on it left. For more info see Multiple Compose files.
2. -d
By default, docker container output is printed on the current terminal session. Use this option to start containers without displaying output to the current terminal session. Full example
docker-compose up -d
3. Run a specific service: To run a specific service in the docker compose file add the service name after the command. When running a service, containers for all the services it depends on would also be started. Full example
docker-compose up [service name]
List running containers
The docker-compose ps
command lists all the running containers for the docker compose file in the current directory. Run docker-compose ps -q
to only display IDs. See the Full doc for more options.
Print service logs
The logs
command displays log output from services defined in the docker compose file. This command is very useful in cases where containers are started in detached mode with docker-compose up -d
command.
docker-compose logs --tail=[lines] -f [service name]
Stopping and starting containers
Containers can be started and stopped with three commands, stop
, start
and restart
.
1. stop
stops containers without removing them. The stopped containers can be restarted with start
or restart
.
docker-compose stop [options] [SERVICE...]
2. start
starts containers or a service.
docker-compose start [SERVICE]
3. restart
restarts a stopped container. Note that any docker compose configuration update does not take effect after running the command.
docker-compose stop [options] [SERVICE...]
Pausing and resuming containers
The pause
command as it's name describe, pauses container. Note that paused containers still take memory on the running host.
docker-compose pause [SERVICE...]
Paused containers can be resumed with the unpause
command.
docker-compose unpause [SERVICE...]
Stopping and removing containers
Using the down
stops containers and removes containers, networks, volumes, and images created using up
.
docker-compose down
Run a container, execute a command inside it then remove it
docker-compose run --rm SERVICE [COMMAND] [ARGS...]
Note: The --rm option Remove container after run. Ignored in detached mode.
docker-compose run --rm db printenv
In the example above, the service name is "db" and the command to execute is printenv
The config
command
The config command is used to validate the docker-compose file and view configurations. When the this command is used without arguments, it shows the configuration of the docker compose file(s). Knowing that the YAML language has reusable blocks, and also multiple compose files can be used when starting containers, this command is very useful to check the final configuration used to start containers. Full example
docker-compose config
Let's cover the options
1. --services
Print the service names in the docker compose file, one per line.
2. --volumes
Print the volume names in the docker compose file, one per line.
3. -q, --quiet
Used to validate the configuration, does not print.