Docker - Compose

Docker compose is one of Docker’s solutions for managing multiple containers.

In the lessons, I created 3 containers. CheckbookAPI, CheckbookSQL, and CheckbookWeb.

Generally, when I am running one, I am usually running all three.

To perform a build of these three containers I use the following commands.

docker build -f DockerFileSQL.txt -t wachdorfmark/checkbooksql .

docker build -f DockerFileAPI.txt -t wachdorfmark/checkbookapi .

docker build -f DockerFileWebsite.txt -t wachdorfmark/checkbookweb .

So to start I open a dos prompt and execute the following commands:

docker run --name CheckbookSql -p 1440:1433 -d --network=CheckbookNetwork wachdorfmark/checkbooksql 

docker run --name CheckbookApi -p 8000:80 -d --network=CheckbookNetwork wachdorfmark/checkbookapi 

docker run --name CheckbookWeb -d -p 8088:80 --network=CheckbookNetwork wachdorfmark/checkbookweb

Before I turn off or reboot my computer I execute the following commaNDS

docker stop CheckbookWeb

docker stop CheckbookApi

docker stop CheckbookSql

Of course, this is version one. When I start splitting and adding microservices, I’ll need to build/run/stop/start those as well.

This is where Docker compose comes in. With Docker Compose you setup a configuration file and then you execute commands based around those commands

to support my environment, I would setup a file like the following.


version: ‘3’

services:

checkbooksql:
    build:
        context: .
        dockerfile: DockerFileSQL.txt
    ports:
        - 1440:1433
    networks:
        - checkbook
    volumes:
    - ./sql:/var/opt/mssql/data/checkbook
    
checkbookapi:  
    build:
        context: .
    dockerfile: DockerFileAPI.txt
        ports:
            - 8000:80 
    networks:
        - checkbook       

checkbookweb:
    build: 
        context: .
        dockerfile: DockerFileWebsite.txt
    ports:
        - 8088:80
    networks:
        - checkbook

networks:
    checkbook:
        driver: bridge

If you study the sections, checkbooksql contains a number of sections, each of those sections match what you’d put on a command line. Checkbookapi and checkbookweb are the same.

Now you can use a command like

docker compose ps -d

which will launch all the containers at the same time.

More information

[Docker - Compose - File Syntax] [Docker - Compose - Command line Alphabetized]