docker-compose
In Continuation with the previous blog ….
What is docker-compose means?
As the word compose means to form something by putting together, yes correct! to put something inside a bag so that transportation of things becomes easier.
In a similar way, docker-compose is a way to put containers of multi-tier architecture applications in a single place such that their management becomes easier.
Multi-tier architecture: has many applications distributed at different layers mainly Frontend, Backend, and Database.
The Docker Compose File
It is a simple YAML configuration file that we use to deploy applications. After creating the file it's just a single command to deploy the applications.
Install docker-compose:
For windows & Linux you need to explicitly install the same but for mac, it comes with a docker installation.
docker-compose file instructions are version specific so need to use them thoroughly.
check the installed version:
docker-compose version
Let’s start with file creation, docker-compose.yaml
Get DevKishan Joshi’s stories in your inbox
Join Medium for free to get updates from this writer.
Here We are creating a multi-tier architecture to deploy a WordPress with a MySQL database.
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
networks:
- myapp
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
depends_on:
- db
volumes:
- wordpress:/var/www/html
deploy:
resources:
limits:
cpus: '0.50'
memory: 50M
reservations:
memory: 20M
db:
image: mysql
restart: always
networks:
- myapp
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
deploy:
mode: replicated
replicas: 2
networks:
myapp:
name: myapp
volumes:
wordpress:
db:Create the infra using the following command:
docker-compose up -d
This compose file will create:
- The version of compose file used is 3.1
- The following YAML file will first create a network “myapp”.
- Creates MySQL container & attaches volume named “db” to it.
- The MySQL database container will be created first as in the WordPress container we have a depends_on flag.
- there will be 2 replicas of MySQL containers will be created.
- The containers are created with specified resource constraints.
The docker-compose file is very much self-explanatory, I have used almost keywords that we generally use in a compose file.
version: Specify the docker-compose file versionservices: we define all the applications in the services block in a list fashion.image: Specifies the image to use that will be used to deploy.restart: this specifies restarting the container whenever it failsports: Exposes a specific port or ports to be used by the container.networks: Creates a network as specified & put containers in that network created.environment: Using this we put environment variables in the container.depends_on: this allows the container to wait until the subsequent container starts up.volumes: This will attach the volume createddeploy: this keyword specifies resource limits & minimum requirement for resources, number of replicas to create
There is a volumes & networks keyword we have used at the bottom of the file that will create the specified infra first & later the same keyword used in the services block binds the same created things to the specified container.
Commands summary:
- docker-compose — version
Check the version of docker-compose installed
2. docker-compose up -d
Create all infra as mentioned in compose file
3. docker-compose down
delete all infra as mentioned in compose file
4. docker-compose -f my-compose.yaml up -d
by default, the docker-compose command looks for the docker-compose.yaml file & if the name of the file is different then we have to specify it explicitly.
5. docker-compose up -d db
There will be many services in compose file & we want a specific service to be up at a time, so only db service will be created.
As you might have gone through both blogs I have a task:
Find out & demonstrate what the “build” keyword does inside the services block & let me know in the comment section.
Thanks for your Time! Follow & Clap 👏 to keep me Motivated.
Happy Learning!










