Hi Readers, In this blog will learn about the basics of a docker compose file. We will see what it is, why do we need this and most importantly see how we can begin with writing docker compose file.
What is docker-compose file?
- It is a tool which is helpful in defining and running multi-container docker applications.
- We configure application services into a yaml file i.e docker-compose.yml
- We can start/stop services with simple commands:
- docker-compose up : To start the services.
- docker-compose down : To stop the services.
- In addition, it is easy to scale up or scale down specific services whenever required.
Why do we need this?
Docker compose is most suitable with micro-service application architecture:
- Earlier monolithic application architecture was used. The problem with monolithic applications is that if any single service of the application goes down, the whole application will not be accessible. And this creates huge impact on customer experience.
- Meanwhile, In micro-service architecture we have a separate application, separate database and separate server for different different services.
- So like in future if any one service goes down, it doesn’t affect any other service and user can use rest of the services seamlessly.
- Also in case suppose we want to scale up any specific service only due to heavy traffic or increased demand we can just scale up that specific service and this would be cost efficient as well.
For instance consider an example of online shopping application, we have multiple modules for multiple services.
Here we have 4 different micro-services for whole application,
- Accounts service
- Products Service
- Basket Service
- Billing Service
Writing a docker-compose file
Pre-requisite
Firstly, make sure to have docker-compose installed in you system. However If not already installed, install from here.
Create a file with name docker-compose.yml and use the below code snippet to begin,
version: '3'
services:
webserver:
image: nginx
ports:
- 9090:80
volumes:
- .:/code
- logVolume:/var/log
networks:
- webapp-network
database:
image: redis
networks:
- webapp-network
networks:
webapp-network:
driver: bridge
volumes:
logVolume: {}
In above code snippet, we are creating two different services, a volume and a network which uses default bridge driver,
- webserver: We have used nginx image and mapped it to port 9090:80 on a network webapp-network
- database: We have used redis image on a network webapp-network.
Starting the services
docker-compose up -d
You can visit to http://localhost:9090/ to see the nginx homepage.
Scale up or Scale down the services
Scaling up database service
Similarly, scaling down database services
Inspect the container to see container details
docker container inspect <containerID>
This will give a json response containing all the details related to container. We can find network and volume associated with the container.
Stopping the services
docker-compose down
That’s all for this blog.Thank you for following this blog till end. If you found this blog helpful do share this blog with your colleagues. In case of any feedback, suggestion or question reach out to me at nitin.mishra@knoldus.com.
References