Par's GitHub Pages

Visit my latest wiki at wiki.pacroy.com

View the Project on GitHub

Docker Compose Basics

Why

Template

version: '3.1'  # if no version is specificed then v1 is assumed. Recommend v2 minimum

services:  # containers. same as docker run
  servicename: # a friendly name. this is also DNS name inside network
    image: # Optional if you use build:
    command: # Optional, replace the default CMD specified by the image
    environment: # Optional, same as -e in docker run
    volumes: # Optional, same as -v in docker run
  servicename2:

volumes: # Optional, same as docker volume create

networks: # Optional, same as docker network create

Example 1

This create a Jekyll service.

version: '2'

# same as 
# docker run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve

services:
  jekyll:
    image: bretfisher/jekyll-serve
    volumes:
      - .:/site
    ports:
      - '80:4000'

Example 2

This creates a wordpress service.

version: '2'

services:

  wordpress:
    image: wordpress
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: example
      WORDPRESS_DB_PASSWORD: examplePW
    volumes:
      - ./wordpress-data:/var/www/html

  mysql:
    image: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: examplerootPW
      MYSQL_DATABASE: wordpress
      MYSQL_USER: example
      MYSQL_PASSWORD: examplePW
    volumes:
      - mysql-data:/var/lib/mysql

volumes:
  mysql-data:
	

Demo

Clone https://github.com/BretFisher/udemy-docker-mastery/tree/master/compose-sample-2 docker-compose up for running in the foreground docker-compose up -d for running in the background docker-compose logs to see the log docker-compose ps to see list of containers docker-compose top to see list of processes docker-compose down to stop and remove the service

Assignment: Writing A Compose File

Answer

version: '2'

services: 
  drupal:
    image: drupal
    ports: 
      - 8080:80
    volumes: 
      - drupal-modules:/var/www/html/modules
      - drupal-profiles:/var/www/html/profiles
      - drupal-sites:/var/www/html/sites
      - drupal-themes:/var/www/html/themes
  postgres:
    image: postgres
    environment: 
      - POSTGRES_PASSWORD=yourpasswd

volumes: 
  drupal-modules:
  drupal-profiles:
  drupal-sites:
  drupal-themes: