Day 18 Task: Docker for DevOps Engineers
Aiding Multicontainer Orchestration: Understanding docker-compose
Docker Compose
Docker Compose is a tool that was developed to help define and share multi-container applications.
With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.
What is YAML?
YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ainโt markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.
YAML is a popular programming language because it is human-readable and easy to understand.
YAML files use a .yml or .yaml extension
๐ถ Task-1
Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.
docker-compose.yml
is a configuration file used by Docker Compose to define and manage multi-container Docker applications. This YAML file allows you to specify various components of your application, such as services, networks, and volumes, along with their configurations and relationships.
Here's a breakdown of what you can define in a docker-compose.yml
file:
Services: Each service represents a container in your application. You can specify the image to use, environment variables, ports, volumes, and other container-specific settings for each service.
Networks: You can define custom networks for your services to communicate with each other. This helps in isolating and securing communication between containers.
Volumes: Define persistent data storage locations (volumes) that can be mounted into containers. Volumes ensure that data is preserved even if containers are destroyed and recreated.
Environment Variables: Set environment variables for your services to configure their behavior.
Dependencies: You can define dependencies between services, ensuring that one service starts only after another service is up and running.
Scaling: You can specify the desired number of replicas for each service to scale horizontally based on demand.
Resource Limits: Configure resource limits for each service, such as CPU and memory limits.
By using a docker-compose.yml
file, you can describe your entire application stack in a single document, making it easier to manage complex applications and their interdependencies. Docker Compose reads this configuration file and handles the orchestration of containers, networks, and volumes according to the defined specifications. This simplifies the deployment and management of multi-container applications while promoting consistency and reproducibility across different environments.
Sample docker-compose.yaml file
๐ถTask-2
- Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user (Hint- Use
usermod
command to give user permission to docker). Make sure you reboot instance after giving permission to user.
docker pull nileshsahare/nilesh07-todo-app:node-app
- Inspect the container's running processes and exposed ports using the docker inspect command.
docker inspect <image_id>
- Use the docker logs command to view the container's log output.
docker logs <container_id>
- Use the docker stop and docker start commands to stop and start the container.
#To stop docker container
docker kill <container-id>
#To start docker container
docker start <container-id>
- Use the docker rm command to remove the container when you're done.
#To remove container
docker rm <container-id>
๐ถ How to run Docker commands without sudo?
Make sure docker is installed and the system is updated. To check docker install use
docker --version
sudo usermod -a -G docker $USER
Reboot the machine.
If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐ #devops #90daysofdevop #git&github #docker