The docker-compose is a useful tool for orchestrating multi-container Docker applications. It allows us to run applications based on a YAML file. This file is a list of instructions that define the parameters of the launched applications. For example, we can run two containers – database MySQL and base web application WordPress. The docker-compose files might look like this:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
To manage containers we can use a few basic docker-compose commands:
docker-compose build
docker-compose up
The containers have been run and logs printed on the console. To run in background use parameter -d.
docker-compose run <service> <bash_command>
ex: docker-compose run wordpress <service> <bash_command>
For example, print Debian version of a WordPress container.
docker ps
The above command presents the information about
docker-compose ps
docker logs <service>
docker-compose down
docker kill <container_id>
docker images
docker exec <service> <bash_command>
docker system prune
docker kill $(docker ps -q)
docker stop $(docker ps -q)
docker rmi $(docker images -q)
docker rm $(docker container ps -q)
Docker-compose is a useful and easy tool to manage multiple containers. You can decide how containers are separated, what resources they can use, and how they communicate with each other. You can also set what should happen if the status of one of the containers changes. This feature allows you to restore the state of the system automatically.
I invite you to explore the entire IoT analysis and management system.
If you have any question, leave it in the comment, I will be happy to answer 😉
[1] Official documentation https://docs.docker.com/compose/
[2] Quickstart: Compose and WordPress
https://docs.docker.com/compose/wordpress/
Leave a Reply