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.
To learn more about docker-compose visit here
๐ถ 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.
Read more about it here
๐ถ 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 the user permission to docker). Make sure you reboot the instance after permitting the user.
#To pull a pre-existing Docker images
docker pull chandreshpatle28/todo-app:tagname
- Inspect the container's running processes and exposed ports using the docker inspect command.
#To run docker
docker run chandreshpatle28/todo-app:latest
#To check docker process status
docker ps
docker images
docker inspect :
#To inspect docker container's running processes
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>
** Before Remove we have to kill or stop the container
๐ถ 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.
In conclusion, Docker Compose offers a streamlined and efficient way to manage multi-container applications. By encapsulating the configuration of multiple services, networks, and volumes into a single docker-compose.yml file, developers and DevOps engineers can orchestrate complex application stacks with ease.
In conclusion, Docker and Docker Hub together provide a powerful ecosystem for containerization and application deployment. The Dockerfile serves as the blueprint for creating Docker images, while Docker Hub acts as a repository for storing, sharing, and distributing those images.
Stay in the loop with my latest insights and articles on cloud โ๏ธ and DevOps โพ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).
DockerHub repo for above task ๐ณ https://hub.docker.com/repository/docker/chandreshpatle28/todo-app/general
Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.
#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker