Quick startup with Docker Compose

Photo by weston m on Unsplash

Quick startup with Docker Compose

Overview:

Docker Compose - is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services.

Installation:

  1. Download the Docker Compose version 1.23.2:
    sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    
  2. Apply executable permission:
    sudo chmod +x /usr/local/bin/docker-compose
    
  3. Test the installation:
    docker-compose --version
    

Basic commands:

  1. To create a compose service and startup in the background (before that create docker-compose.yml file):
    docker-compose up -d
    
  2. To list the containers created by Docker compose use:
    docker-compose  ps
    
  3. To specify compose file use -f flag:
    docker-compose -f [filepath] up -d
    
  4. To stop a service:
    docker-compose stop
    
  5. To start service:
    docker-compose start
    
  6. To restart a service:
    docker-compose restart
    
  7. To delete a service:
    docker-compose down
    

Docker Compose file:

  1. By default the docker-compose.yml is used to create compose services. You can specify any other file with -f flag
  2. There are four types of top level keys in Docker compose file:
    • version
    • services
    • networks
    • volumes
  3. Docker compose file format (version) is dependent on which version of Docker you are using
  4. Example of docker-compose.yml file which utilizes the Dockerfile. To specify usage of Dockerfile use: build: context::
    version: '3'
    services:
    my-service: # --> Name of the service
     build: # --> Specifies build context
       context: . # --> Path to the Dockerfile (both files in the same directory)
       args:
         - VERSION=v4.1
     ports:
       - "8080:4000"
     environment:
       - ENV=development
    
  5. If changes were made into Dockerfile, before starting up a compose service you need to build it again:
    docker-compose build
    
  6. To bypass using the cache layers from previous built images use --no-cache flag:
    docker-compose build --no-cache
    

Reference:

  1. Compose file version 3 reference
  2. Example of installing Drupal with MySQL via Docker Compose
  3. How to use volumes in Docker Compose