15 Basic Docker Compose Commands for Beginners

Docker Logo
15 Basic Docker Compose Commands for Beginners 1

Introduction

After writing the 25 Basic Docker Commands for Beginners article, it was only natural that I start exploring the functions of Docker Compose Commands. While running container using a docker run command is an easy way to get your containers up and running, using a docker-compose.yml file will ensure that your environmental variables and other parameters are saved making it easy to redeploy your containers across Docker host machines.

You can have multiple containers within a single Docker Compose file which will create a service that runs multiple containers. For example a container that runs a wiki, and a container that runs an SQL database for that wiki.

The docker compose commands in this article will be able to get you started with working with docker compose. This guide assumes that you already have Docker and Docker Compose installed on your system. If not, you can read the Quick and Easy Steps to Install Docker. Scroll to the bottom of the article and you will see the instructions to install Docker Compose.

Working with Docker Compose Services

Docker compose commands are available in the CLI that will allow you to start, stop and restart services, pause and resume services and pull images. Below are examples of those commands.

Create and Start all Containers, Volumes, Images, and Networks in a Docker Compose File

When you are ready to run your service, cd to the directory in which your docker-compose.yml file is located. Once you are in the directory with the compose file you can use the following command to run your service.

docker-compose up

When the above command is ran, docker-compose will search for a file named docker-compose.yml or docker-compose.yaml. Once it finds the file, it will read it and create the containers, volumes, images, and networks listed in the file and then run the containers.

Stop and Remove all Containers, Volumes, Images, and Networks in a Docker Compose File

In order to shut down your service, make sure you are in the directory of your docker-compose.yml file and run the following command.

docker-compose down

When this command is ran, docker-compose will search for a file named docker-compose.yml or docker-compose.yaml. Once the file is located, it will stop all of the containers in the service and remove the containers from your system.

If you want to remove internal volumes that were created, you can add the -v flag to the command. Below is an example of the command to remove internal volumes.

docker-compose down -v

Start Services in a Docker Compose File

In order to start services that are outlined in a Docker Compose File you can run the following command within the directory of your docker-compose file.

docker-compose start

This command will search for services listed in a docker-compose.yaml or docker-compose.yml file and start the containers listed in the service.

Stop Services in a Docker Compose File

If you need to stop the services that are outlined in your Docker Compose File you can run the below command within the directory of your docker-compose file.

docker-compose stop

This command will stop all services located in your docker-compose.yml file.

Pause Services in a Docker Compose File

In order to pause the containers in a service that is listed in a docker-compose file you can run the following command from within the directory of your docker-compose file.

docker-compose pause

Resume Services in a Docker Compose File

In order to resume paused containers in a service that is listed in a docker-compose file you can run the following command from within the directory of your docker-compose file.

docker-compose unpause

Restart Services in a Docker Compose File

To restart all of the containers in a service that is outlined in your docker-compose file, you can run the following command from inside the directory of your docker-compose file.

docker-compose restart

After this command is ran, all of your containers will restart.

Remove Stopped Containers in a Docker Compose File

This command will remove all containers that are listed in a docker-compose file that are stopped. You can run the following command from within the directory of our docker-compose.yml or docker-compose.yaml file.

docker-compose rm

Pull Images in a Docker Compose File

If you would like to pre-pull your container images prior to running your service or if you would like to pull updated images for your containers, you can run the following command.

docker-compose pull

This command will pull all of the container images that are outlined in your docker-compose file.

Monitoring Docker Compose Services

Docker Compose has commands that will allow you to view logs, resource usage and monitor events within the service outlined in your docker-compose.yml file.

List Containers Deployed from a Docker Compose File

If you need to view a list of containers that are deployed from your docker compose file, you can run the following command within the directory of your docker-compose file.

docker-compose ps

List images from a Docker Compose File

The below command will list all of the images that are on your system as part of your docker-compose file.

docker-compose images

View Logs from Running Containers

In order to view the logs from the containers you have running in a service you can run the following command within the directory of your docker-compose file.

docker-compose logs

This command will combine and output all of the logs from service containers that you have running.

View Resource Usage of Running Containers

This command is equivalent to running the top command. It will display resource usage of the containers that are deployed from a docker-compose file.

docker-compose top

Validate and View a docker-compose.yml File

In order to view and validate your docker-compose.yml file, you can run the below command from inside the directory in which your docker-compose file is stored.

docker-compose config

Additional Information

If you need additional information with any of these docker compose commands you can type append --help to the end of any of the above docker commands and type enter. If you need more information you can visit the Docker Documentation page.

If there are any other Docker Compose commands you would like to see added to this post, leave a comment below.

The post, 15 Easy Docker Compose Commands for Beginners, first appeared on Codeopolis.

Be the first to comment

Leave a Reply

Your email address will not be published.


*