Creating compose.yaml files

Intermediate Docker

Mike Metzger

Data Engineering Consultant

YAML

  • Yet Another Markup Language
    • YAML Ain't Markup Language
  • Text file, but spacing matters (like Python)
  • Used in many development scenarios for configuration
  • Rules can be tricky, mainly keep entries lined up as in examples
services:
  postgres:
    container_name: postgres
    image: postgres:latest
    ports:
      - "5432:5432"
    restart: always
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4:latest
    ports:
      - "5050:80"
    restart: always
Intermediate Docker

Main sections

  • Different sections handle different components
  • services: list the containers to load
  • networks: handles networking definitions
  • volumes: controls any volume mounting
  • configs: handles configuration options without custom images
  • secrets: Provides options to handle passwords, tokens, API keys, etc
  • Refer to the Docker Compose Documentation for more information
services:
  ... # Define containers

networks: ... # Define any networking details
volumes: ... # Define storage requirements
configs: ... # Define special config details
secrets: ... # Define passwords / etc
1 https://docs.docker.com/compose/compose-file/
Intermediate Docker

Services section

  • Defines all required resources for the application
  • Primarily specifies the containers and images to be used
  • Extensive options available, but only apply to the individual container(s)
  • Indention is applied as needed
  • First subsection is the name of each component, followed by the settings
Intermediate Docker

Services example

services:

# Resource name postgres:
# Container name, otherwise random container_name: postgres
# Container image to use image: postgres:latest
# Any port mapping required ports: # Network details - "5432:5432"
# Next resource pgadmin: ...
  • Resource name
  • container_name:, the assigned name of the container otherwise it's random
  • image:, which container image to use
  • ports:, contains a list of any port mapping required
  • Followed by next resources required
Intermediate Docker

Additional comments

  • config.yaml syntax is extensive
    • Covering very small portion of compose.yaml options
    • Review the documentation!
  • It's typically not required to build a compose.yaml file from scratch
1 https://docs.docker.com/compose/compose-file/
Intermediate Docker

Let's practice!

Intermediate Docker

Preparing Video For Download...