Docker Compose Quick Start & Usage

Docker Compose

Docker Compose is a tool that can centrally manage multiple Docker containers. We describe the installation method as a way to start it, and the main commands etc. as a usage.
The overview of Docker Compose will be described in another knowledge.
To use Docker Compose, you also need Docker itself. How to get started with Docker is described below.

Sponsored Links

Docker Compose installation

  • Access the Docker Compose repository page at the following URL on GitHub and check the version you want to install.
  • Change the version part of the following command (after “~download/”) to the version you want to install, and execute the command. Docker Compose is downloaded by this command.
    • In the following example, Docker Compose “1.22.0” is downloaded and saved under “/usr/local/bin/” with the file name “docker-compose”.
$ sudo curl -L`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  • The above is an executable binary, so just download it and the installation will be complete.
  • Give execute permission to this file.
$ sudo chmod +x /usr/local/bin/docker-compose
  • This completes the installation. Make sure Docker Compose is available.
$ docker-compose --version
docker-compose version 1.22.0, build f46880fe

Compose file

In order to use Docker Compose, you need to create a Compose file.
A Compose file is a YAML format file that instructs and manages the settings and startup method of the Docker container.
Before we explain the command, we describe the Compose file used to execute the command.
  • Create a Compose file with the name “docker-compose.yml” and copy and save the following.
    • Below is a Compose file that allows you to download the latest version images of Ubuntu and CentOS from Docker Hub, create a container, and start it.
      • The following “depends_on” setting is set to start Ubuntu before CentOS.
      • If “restart: always” is set, the container will be restarted automatically if each container is stopped due to an error or other reason.
      • However, “depends_on” and “restart: always” do not work together. When the container is started using Docker Compose, the start order is controlled by “depends_on”, but if the container is stopped due to a failure etc. and automatically restarted by “restart: always”, the start order is not considered.
version: '3'

    image: ubuntu:latest
    restart: always

      - ubuntu01
    image: centos:latest
    restart: always

Docker Compose main commands

Execute the following Docker Compose command in the directory where “docker-compose.yml” is saved.

  • Download the image, create and launch the container with Docker Compose.
    • With the “-d” option, the container goes into detach mode and starts in the background.
    • If the target image already exists, the image download is skipped and the container is created first.
    • If there is already a container, container creation is skipped and only container startup is performed.
$ docker-compose up -d
  • By default, the Compose file uses “docker-compose.yml” in the current directory, but if you want to specify the YAML file name to use, use the following command.
    • In the example below, “xxxxxxxxxxx.yml” is used.
$ docker-compose -f xxxxxxxxxxx.yml up -d
  • If you want to log in to the container after startup, use the Docker command.
  • Stop the containers.
$ docker-compose stop
  • Start the containers.
$ docker-compose start
  • Retart the containers.
$ docker-compose restart
  • Check the status of the containers.
$ docker-compose ps
  • Stop the containers and delete them.
$ docker-compose down
  • Stop the containers and delete the containers and images.
$ docker-compose down --rmi all
  • If you are using Docker Volume, check the Vloumes you are using.
$ docker volume ls
  • Delete the Docker Volume with the specified Volume Name.
    • In the following, the Volume whose Volume Name is xxxxxxxxxxxx is deleted.
$ docker volume rm xxxxxxxxxxxx
Sponsored Links