Let's have a look on Docker compose today!
What is Docker compose?
Docker compose is a tool for defining and running multi-container applications.
It is used often to work with programs decomposed into multiple segments - like a web service may be broken down into a frontend, a backend and a database. We can use docker-compose to work with all the of them and provide a complete service.
Compose works in all environments - production, staging, development, testing, as well as CI workflows.
It can also be used to manage the whole lifecycle of an application:
start, stop and rebuild services
view status of running services
stream the log output of running services
run command(s) within a service
Benefits of using Docker Compose:
Easy to control and manage :
It allows you to define and manage multi-container applications in a single YAML file. This simplifies orchestrating and coordinating various services.
Efficient collaboration :
Docker compose configuration files can be easily shared among developers, operations team and other stakeholders.
Rapid application developement :
Docker-compose caches the configuration used to create a container. Next time when you start any cached service that has not changed, docker reuses the existing container.
Create an automated testing environment :
Configure isolated environments for your test-suite for end-to-end testing requirements.
docker compose up -d ./tests_file docker compose down
Installing docker-compose
Check if docker-compose is already installed on your system or not :
docker-compose --version
If it is not installed, install it using your package manager. The reason this needs to be install as a top-up and not comes in the docker package maybe becuase of migration between python to Go of the compose package. Besides, it is the UNIX philosophy to keep every entity separate yet functional. ๐
Also check this out for more : https://stackoverflow.com/questions/66514436/difference-between-docker-compose-and-docker-compose
Installing docker-compose in Debian family
sudo apt update; sudo apt install docker-compose
Task 1
Creating a Docker compose file
Learn YAML language.
Enlist your software requirements along with their version and other specifications.
Jot them all in the docker-compose file.
version: '3.1' # version of compose
services: # contains the services(containers) making up the applicaiton
mongo: # for container with mongoDB image
image: mongo # official mongoDB image from DockerHub
restart: always # if the image stops, restart automatically
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express: # for container with mongo-express image
image: mongo-express #official mongo-express image from DockerHub
restart: always
ports:
- 8081:8081 # port mapping from container port to localhost port
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
We can extend the above to use certain volume to store data i.e. the database with proper configurations.
Task 2
Already done in blog for day 17 (it is so because day 17 was a bit too comprehensive in itself)
Checkout the continuation to this blog in day19.