Skip to main content

Docker Compose

====================================================================================

Installation

Install Docker Compose (and docker.io if not installed already)
apt install docker.io docker-compose

====================================================================================

YAML (Yet Another Markup Language)

YAML files are just text files that are used by Docker-Compose for definition of image/container setup. YAML files are a very useful feature of Docker as they all for deployment of containers on any system by just using the YAML file.

NOTE; YAML files are VERY picky about their syntax - this includes character positioning.

Basic annotated example;

version: '3.7' #defines# docker-composeSpecifies the Docker Compose version toused use(e.g., 3.7)

services:
  portainer:  # Defines a service named "portainer"

    container_name: container1  #define# the name to be assigned toNames the container instance "container1"

    image: portainer/portainer-ce  #define# Specifies the Docker image to beuse used for the container(portainer/portainer-ce)

    command: -H unix:///var/run/docker.sock  #command# Command to beexecute run onwhen the container once it starts -(grants variesaccess dependingto onDocker applicationsocket)

    restart: 'always'  #set# Automatically restarts the container toif startit oncrashes bootor exits

    ports:  #network# definitionDefines port mappings between the container and the host machine
      - target: '9000'  # Maps container port 9000
        published: '9000'  #define# to host port 9000 (can be accessed from the porthost for container to listen on externallymachine)
        protocol: tcp  #protocol# definitionusing TCP protocol

      - target: '8000'  # Maps container port 8000
        published: '8000'  #define# additionalto host port for8000 container(can tobe listenaccessed onfrom externallythe host machine)
        protocol: tcp  #protocol# definitionusing TCP protocol

    volumes:  #definition# ofDefines persistent storage volumesfor the container
      - type: bind  #container# storage will be bound toMounts a physicalhost filedirectory ononto thea underlyingcontainer hostdirectory
        source: /var/run/docker.sock  #define# docker.sockBinds the host's Docker socket file to allow container to use this
        target: /var/run/docker.sock  #define# to the docker.sockcontainer's Docker socket file for(grants access within the container. Essentially mapping the 2 files together.container)

      - type: bind  # Mounts another host directory onto a container directory
        source: /srv/portainer  # Binds the host's `/srv/portainer` directory
        target: /data/  # to the container's `/data/` directory (for persistent data storage)

Start the container based on YAML file:

docker-compose up --detatch

====================================================================================