What is Docker-compose ?
docker-compose is a tool for defining and running multi-container Docker applications.
Docker is a great tool
The problem is with docker you can only run one CONTAINER at the time
Also most applications do not run on
A single container
Multiple containers
Let us take for example a shopping application made of 4 microservices
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
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
Docker-compose language
Docker-compose structure
version
service(s)
declaration(s)
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/
Docker-compose structure
VERSION
version
Remember the version number is always quoted
version: [“version_number”]
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
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
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
Docker-compose structure
Service: required elements
service(s)
Of. course we need an image to run a container
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
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
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
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
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
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
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
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
�
Docker-compose structure
Declaration
Declaration
Each time we have under service
networks
volumes
secrets
We have to declare them
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"
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
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
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
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
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
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
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
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
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
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
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).
Important commands
Stop Services
$ docker-compose down
Stop and remove all the containers, networks, volumes, and images created by up.
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.
Important commands
Start Specific Service
$ docker-compose start [service_name]
Start a specific stopped service.
$ docker-compose start redis
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
Important commands
Check Configuration
$ docker-compose config
Validate and view the Compose file.
Important commands
List Containers
$ docker-compose ps
List the running containers.
Docker vs docker-compose commands
$ docker -itd run [options] IMAGE [COMMAND] [ARG...]
$ docker-compose up -d
Docker vs docker-compose commands
$ docker build [options] PATH
$ docker-compose build
Docker vs docker-compose commands
$ docker stop [container name]
$ docker-compose stop
Docker vs docker-compose commands
$ docker rm [container name]
$ docker-compose down
Docker vs docker-compose commands
$ docker ps
$ docker-compose ps
Docker vs docker-compose commands
$ docker exec [options] CONTAINER COMMAND [ARG...]
$ docker-compose exec [options] SERVICE COMMAND [ARG...]
Docker vs docker-compose commands
$ docker stop $(docker ps –aq)
$ docker rm $(docker ps –aq)
$ docker-compose down
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.