We can tell by running the previous docker-compose –help command that there are many subcommands that can be used with the main docker-compose command.
Up and PS
The first one is docker-compose up, but this time, we will be adding a flag. In your chosen application folder, run the following:
docker-compose up -d
This will start your application back up, this time in detached mode
Once control of your Terminal is returned, you should be able to check that the containers are running using the following command
When running these commands, Docker Compose will only be aware of the containers defined in the service section of your docker-compose.yml file; all other containers will be ignored as they don’t belong to our service stack.
Running the following command will validate our docker-compose.yml file
If there are no issues, it will print a rendered copy of your Docker Compose YAML file to screen; this is how Docker Compose will interpret your file. If you don’t want to see this output and just want to check for errors, then you can run the following command.
docker-compose config -q
Pull, build, and create
The next two commands will help you prepare to launch your Docker Compose application. The following command will read your Docker Compose YAML file and pull any of the images it finds
The following command will execute any build instructions it finds in your file.
The pull and build command only generate/pull the images needed for our application; they do not configure the containers themselves. For this, we need to use the following command
This will create but not launch the containers.
Start, stop, restart, pause, and unpause
The following commands work exactly in the same way as their docker container counterparts, the only difference being that they effect change on all of the containers
docker-compose start docker-compose stop docker-compose restart docker-compose pause docker-compose unpause
It is possible to target a single service by passing its name; for example, to pause and unpause the db service, we would run the following.
docker-compose pause mysql docker-compose unpause mysql
Top, logs, and events
The following command, displays information on the processes running within each of our Docker Compose-launched containers
If you would like to see just one of the services, you simply have to pass its name when running the command.
docker-compose top mysql
The next command streams the logs from each of the running containers to screen
The events command streams events, such as the ones triggered by the other commands we have been discussing, in real time.
The scale command will take the service you pass to the command and scale it to the number you define; for example, to add more mysql containers, I just need to run the following:
docker-compose up -d --scale mysql=3
Kill, rm, and down
The first command stops our running containers by immediately stopping running container processes. This is the kill command
Be careful when running this as it does not wait for containers to gracefully stop, such as when running docker-compose stop, meaning that using the docker-compose kill command may result in data loss.
Next up is the rm command; this removes any containers with the state of exited:
Finally, we have the down command. This, as you might have already guessed, has the opposite effect of running docker-compose up
That will remove the containers and the networks created when running docker-compose up. If you want to remove everything, you can do so by running the following
docker-compose down --rmi all --volumes
This will remove all of the containers, networks, volumes, and images (both pulled and built) when you ran the docker-compose up command.