Docker Compose Commands

Docker Compose Commands

In this tutorial we will discuss about most common Docker Compose commands that we can use with docker compose files.

Docker compose
1. docker-compose

Every docker compose command starts with this command. You can also use docker-compose <command> --help to provide additional information about arguments and implementation details.

$ docker-compose --help
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [--] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  -c, --context NAME          Specify a context name
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert keys
                              in v3 files to their non-Swarm equivalent (DEPRECATED)
  --env-file PATH             Specify an alternate environment file

Commands:
  build              Build or rebuild services
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show version information and quit
2. docker-compose build

This command builds images in the docker-compose.yml file. The job of the build command is to get the images ready to create containers, so if a service is using the prebuilt image, it will skip this service.

$ docker-compose build
database uses an image, skipping
Building web
Step 1/11 : FROM python:3.9-rc-buster
 ---> 2e0edf7d3a8a
Step 2/11 : RUN apt-get update && apt-get install -y docker.io
 ---> 9a03df6dl63a
Step 3/11 : COPY app.config /opt/app
 ---> 43805ed30c71
3. docker-compose images

This command is used to list the images you have built using your current docker-compose file.

$ docker-compose images
   Container          Repository           Tag       Image Id       Size  
--------------------------------------------------------------------------------------
docker_database_1     mysql/mysql-server    5.8      2a6c84ecfcb2   333.9 MB
docker_web_1          <none>                <none>   d986d824dae4   730 MB
4. docker-compose stop

This command is used to stop the running containers of specified services.

$ docker-compose stop
Stopping docker_web_1      ... done
Stopping docker_database_1 ... done
5. docker-compose run

This command is similar to the docker run command. It will create containers from images built for the services mentioned in the compose file.

$ docker-compose run web
Starting 7001788f31a9_docker_database_1 ... done
 * Serving Flask app "app.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
6. docker-compose up

This command does the work of the docker-compose build and docker-compose run commands.

It builds the images if they are not located locally and starts the containers. If images are already built, it will fork the container directly.

$ docker-compose up
Creating docker_database_1 ... done
Creating docker_web_1      ... done
Attaching to docker_database_1, docker_web_1
7. docker-compose ps

This command is used to list all the containers in the current docker-compose file. They can then either be running or stopped.

$ docker-compose ps
      Name                 Command             State               Ports         
---------------------------------------------------------------------------------
docker_database_1   /entrypoint.sh mysqld   Up (healthy)   3306/tcp, 33060/tcp   
docker_web_1        flask run               Up             0.0.0.0:8080->8080/tcp
8. docker-compose down

This command is similar to the docker system prune command. However, in Compose, it stops all the services and cleans up the containers, networks, and images.

$ docker-compose down
Removing docker_web_1      ... done
Removing docker_database_1 ... done
Removing network docker_default

$ docker-compose images
Container   Repository   Tag   Image Id   Size
----------------------------------------------

$ docker-compose ps
Name   Command   State   Ports
------------------------------
9. docker-compose pause

This command is used to pause the running containers of a service. They can be unpaused with docker-compose unpause

$ docker-compose pause
10. docker-compose unpause

This command is used to unpause paused containers of a service.

$ docker-compose unpause
11. docker-compose logs

This command is used to displays log output from services.

$ docker-compose logs <name-of-service>
12. docker-compose start

This command is used to start the containers, but it doesn’t build images or create containers. So it only starts containers if they have been created before.

$ docker-compose start

For more information about docker compose commands you can refer docker official website.

Docker Compose Commands
Scroll to top