1 of 51

2 of 51

What is Docker-compose ?

docker-compose is a tool for defining and running multi-container Docker applications.

3 of 51

Docker is a great tool

The problem is with docker you can only run one CONTAINER at the time

4 of 51

Also most applications do not run on

A single container

Multiple containers

5 of 51

Let us take for example a shopping application made of 4 microservices

6 of 51

In docker this is how we are going to deploy it

As you noticed it took us 4 docker run command to get it done

What if we have 150 microservices to deploy

7 of 51

But using docker-compose we can get everything deployed in one command

As you noticed it took us 1 docker-compose command to get it done

8 of 51

Docker-compose language

9 of 51

Docker-compose structure

version

service(s)

declaration(s)

10 of 51

Docker-compose structure

VERSION

version

The version of the compose file and the version number is controlled by docker

https://docs.docker.com/compose/compose-file/compose-versioning/

11 of 51

Docker-compose structure

VERSION

version

Remember the version number is always quoted

version: [“version_number”]

12 of 51

Docker-compose structure

Service

service(s)

version: '3.8'

services:

app:

image: redis�

db:

image: postgres

​

front:

image: nginx�

api:

image: node:alpine

​

check:

image: golang:latest

13 of 51

Docker-compose structure

Service

service(s)

version: '3.8'

services:

app:

image: redis

ports:

- 80:80

networks:

- appnetwork

db:

image: postgres

ports:

- 80:80

networks:

- appnetwork

front:

image: nginx

ports:

- 80:80

networks:

- appnetwork

api:

image: node:alpine

ports:

- 80:80

networks:

- appnetwork

check:

image: golang:latest

ports:

- 80:80

networks:

- appnetwork

version: '3.8'

services:

app:

image: redis

ports:

- 80:80

version: '3.8'

services:

app:

image: redis

ports:

- 80:80

Same as

14 of 51

Docker-compose structure

Service

service(s)

version: '3.8'

services:

app:

image: redis

ports:

- 80:80

networks:

- appnetwork

db:

image: postgres

ports:

- 80:80

networks:

- appnetwork

front:

image: nginx

ports:

- 80:80

networks:

- appnetwork

api:

image: node:alpine

ports:

- 80:80

networks:

- appnetwork

check:

image: golang:latest

ports:

- 80:80

networks:

- appnetwork

version: '3.8'

services:

app:

image: redis

ports:

- 80:80

version: '3.8'

services:

app:

image: redis

ports:

- 80:80

Same as

15 of 51

Docker-compose structure

Service: required elements

service(s)

Of. course we need an image to run a container

16 of 51

Docker-compose structure

Service: optional elements

version: '3.3'

services:

db:

image: postgres:latest

restart: always

hostname: database

volumes:

- dbdata:/var/lib/postgresql/data

environment:

POSTGRES_DB: exampledb

POSTGRES_USER: exampleuser

POSTGRES_PASSWORD: examplepass

expose:

- "5432"

� web:

image: nginx:alpine

restart: always

hostname: webserver

volumes:

- weblogs:/var/log/nginx

ports:

- "88:80"

depends_on:

- db

� redis:

image: redis:alpine

restart: always

hostname: redis-cache

volumes:

- redisdata:/data

expose:

- "6379"

� rabbitmq:

image: rabbitmq:3-management

restart: always

hostname: rabbitmq-broker

volumes:

- rabbitmqdata:/var/lib/rabbitmq

expose:

- "5672"

- "15672"

Note that this docker-compose file is not complete yet because the declaration portion is still missing

17 of 51

Docker-compose structure

Service: optional elements

version: '3.8'

services:

app:

image: nginx:latest

working_dir: /app

entrypoint: ["./start.sh"]

volumes:

- appdata:/app

ports:

- "3000:3000"

- "4000:3000"

environment:

- APP_ENV=production

secrets:

- app_secret

networks:

- appnetwork

Note that this docker-compose file is not complete yet because the declaration portion is still missing

18 of 51

Docker-compose structure

Service: NOTES

version: '3.8'

services:

app:

image: nginx:latest

working_dir: /app

entrypoint: ["./start.sh"]

volumes:

- appdata:/app

ports:

- "3000:3000"

- "4000:3000"

environment:

- APP_ENV=production

secrets:

- app_secret

networks:

- appnetwork

A docker-compose file required at least a service

The number of services represent the number of container that will be deployed

19 of 51

Let us compare Docker-compose service options with Dockerfile

version: '3.8'

services:

app:

image: nginx:latest

working_dir: /app

volumes:

- appdata:/vol

expose:

- "3000"

environment:

- APP_ENV=production

entrypoint: ["./entrypoint.sh"]

​

# Use nginx latest as the base image

FROM nginx:latest

​

# Set the working directory inside the container

WORKDIR /app

��# Set volume

VOLUME /vol

​

# Expose port 3000

EXPOSE 3000

�# Set environment variables

ENV APP_ENV=production

​

�# Set the entrypoint

ENTRYPOINT ["/app/entrypoint.sh"]

��

docker-compose

Dockerfile

20 of 51

Docker-compose structure

Service: optional elements

version: '3.8'

services:

db:

image: mysql:5.7

command: --default-authentication-plugin=mysql_native_password

working_dir: /var/lib/mysql

volumes:

- dbdata:/var/lib/mysql

environment:

MYSQL_ROOT_PASSWORD: rootpassword

MYSQL_DATABASE: appdb

MYSQL_USER: appuser

MYSQL_PASSWORD: apppassword

secrets:

- db_secret

networks:

appnetwork:

aliases:

- database_server

Note that this docker-compose file is not complete yet because the declaration portion is still missing

21 of 51

Docker-compose structure

Service: optional elements

Here are few elements we can use under the service

command

restart

volumes

networks

environment

expose

ports

depends_on

hostname

container_name

working_dir

entrypoint

22 of 51

Docker-compose structure

Declaration

Declaration

version: '3.8'

services:

app:

image: node:latest

volumes:

- app_data:/data

- app_modules:/usr/

networks:

- webnet

secrets:

- my_secret

�volumes:

app_data:

app_modules:

�networks:

webnet:

�secrets:

my_secret:

file: ./my_secret.txt

23 of 51

Docker-compose structure

Declaration

Declaration

version: '3.8'

services:

app:

image: node:latest

volumes:

- app_data:/data

- app_modules:/usr/

networks:

- webnet

secrets:

- my_secret

�volumes:

app_modules:

app_data:

�networks:

webnet:

�secrets:

my_secret:

file: ./my_secret.txt

�

24 of 51

Docker-compose structure

Declaration

Declaration

Each time we have under service

networks

volumes

secrets

We have to declare them

25 of 51

Important facts to know

Unique Port Mappings

version: '3.8'

services:

frontend:

image: nginx

ports:

- "8080:80"

api:

image: apache

ports:

- "8081:80"

version: '3.8'

services:

frontend:

image: nginx

ports:

- "8080:80”

​

api:

image: apache

ports:

- "8080:80"

26 of 51

Important facts to know

Unique Container Names

version: '3.8'

services:

frontend:

image: nginx

container_name: frontend

​

api:

image: apache

container_name: api

version: '3.8'

services:

frontend:

image: nginx

container_name: frontend

​

api:

image: apache

container_name: frontend

27 of 51

Important facts to know

Unique Service Names

version: '3.8'

services:

webserver:

image: nginx

​

database:

image: mysql

version: '3.8'

services:

webserver:

image: nginx

​

webserver:

image: mysql

28 of 51

Important facts to know

Environment Variable Isolation

version: '3.8'

services:

app1:

environment:

- APP_ENV=development

​

app2:

environment:

- APP_ENV=production

version: '3.8'

services:

app1:

environment:

- APP_ENV=development

​

app2:

environment:

- APP_ENV=production

29 of 51

Important facts to know

Volume Naming and Paths

version: '3.8'

services:

service:

volumes:

- data_volume:/var/lib/data

volumes:

data_volume:

version: '3.8'

services:

service:

volumes:

- data_volume:/var/lib/data

30 of 51

Important facts to know

Networks Configuration

version: '3.8'

services:

service1:

networks:

- my_network

​

service2:

networks:

- my_network

​

networks:

my_network:

version: '3.8'

services:

service1:

networks:

- my_network

​

service2:

networks:

- my_network

​

31 of 51

Important facts to know

Dependency Management

version: '3.8'

services:

web:

depends_on:

- db

db:

image: mysql

version: '3.8'

services:

web:

depends_on:

- frontend

db:

image: mysql

32 of 51

Important facts to know

Dependency Management

version: '3.8'

services:

web:

depends_on:

- db

db:

image: mysql

version: '3.8'

services:

web:

depends_on:

- frontend

db:

image: mysql

33 of 51

Docker-compose file

By default the docker-compose file is called

docker-compose.yml

docker-compose.yaml

docker-compose up –d

When you run the command above docker will use by default the docker-compose file located on the current directory

34 of 51

Docker-compose file

What if the docker-compose file is not located in current directory ?

docker-compose –f [LOCATION OF THE COMPOSE FILE] up –d

docker-compose –f /home/devops/docker-compose.yml up –d

35 of 51

Docker-compose file

What if the docker-compose file is located in our current directory and has different name

docker-compose –f [COMPOSO FILE NAME] up –d

docker-compose –f devops-compose.yml up –d

36 of 51

Important commands

Start Services

$ docker-compose up

Create and start containers for all services defined in docker-compose.yml. Add -d to run them in the background (detached mode).

$ docker-compose up –d

run them in the background (detached mode).

37 of 51

Important commands

Stop Services

$ docker-compose down

Stop and remove all the containers, networks, volumes, and images created by up.

38 of 51

Important commands

Stop/Start/restart Containers

$ docker-compose stop

Stop running containers without removing them.

​

$ docker-compose start

They can be started again with docker-compose start.

$ docker-compose restart

They can be restarted again with docker-compose restart.

​

​

​

39 of 51

Important commands

Start Specific Service

$ docker-compose start [service_name]

Start a specific stopped service.

​

$ docker-compose start redis

40 of 51

Important commands

Execute a Command in a Running Container

$ docker-compose exec [service_name] [command]

Execute a command in a running container.

​

$ docker-compose exec redis sh

41 of 51

Important commands

Check Configuration

$ docker-compose config

Validate and view the Compose file.

42 of 51

Important commands

List Containers

$ docker-compose ps

List the running containers.

43 of 51

Docker vs docker-compose commands

$ docker -itd run [options] IMAGE [COMMAND] [ARG...]

$ docker-compose up -d

44 of 51

Docker vs docker-compose commands

$ docker build [options] PATH

$ docker-compose build

45 of 51

Docker vs docker-compose commands

$ docker stop [container name]

$ docker-compose stop

46 of 51

Docker vs docker-compose commands

$ docker rm [container name]

$ docker-compose down

47 of 51

Docker vs docker-compose commands

$ docker ps

$ docker-compose ps

48 of 51

Docker vs docker-compose commands

$ docker exec [options] CONTAINER COMMAND [ARG...]

$ docker-compose exec [options] SERVICE COMMAND [ARG...]

49 of 51

Docker vs docker-compose commands

$ docker stop $(docker ps –aq)

$ docker rm $(docker ps –aq)

$ docker-compose down

50 of 51

Docker vs docker-compose commands

docker commands generally operate on individual containers, images, or volumes.

docker-compose commands are higher-level commands that work with the entire architecture as defined in the Compose file, which can consist of many containers working together.

51 of 51