Docker Compose 🚢
📌Introduction to docker-compose
🐳 Docker Compose is like a magic wand for creating and managing multi-container Docker apps with a user-friendly YAML file. It simplifies the task of running complex apps by letting you define all the services, networks, and storage you need in one neat config.
🚀 This makes setting up, deploying, and expanding your apps super easy across different settings. With Docker Compose, you can separate your app components (like databases, web servers, and more) into individual containers that talk to each other and collaborate.
🤖 Docker Compose acts like the conductor, making sure these containers start, stop, and work together harmoniously.
📌What is docker Compose?
Docker Compose is an open-source tool that allows you to define and manage multi-container Docker applications.
It simplifies the process of orchestrating and running multiple Docker containers as a cohesive application stack.
Docker applications use a simple YAML file.
Declarative Configuration: 📝
Docker Compose makes things simple with a YAML file (usually named docker-compose.yml) to define the structure of your application. It defines Docker images, variables, ports, volumes, and relationships for each part.
Service Definition: 🚀
Each component of your application is defined as a "service" within the Docker Compose file. A service could represent a web server, database, backend API, cache, or any other part of your application.
Networking: 🌐
Docker Compose sets up a default network for your app, so services can communicate using their service names as hostnames. You can even create custom/special networks to isolate and control communication between specific services.
Volumes: 📂
Docker Compose lets you define data storage that outlives of you container. This is essential for maintaining data integrity and stateful applications.
Scaling: 🔢
You can easily multiply your containers by telling Docker Compose how many copies of each one you want. It takes care of the distribution and load balancing of requests among the replicated containers.
Environment Variables and Secrets: 🔐
Docker Compose helps you handle secret missions like passwords and environment settings separately. It's like a secret agent for your app's security and settings.
Command-line Interface: 💻
Docker Compose offers a bunch of commands to boss your app around, from starting to stopping and everything in between.
It's like having a magical script that describes your app's every detail. This makes setting up, launching, and growing your app the same dance, no matter where it's performed.
With Docker Compose, you cast your app as a cast of characters, like databases, web servers, and more, all in their own containers
To harness the power of Docker Compose, begin by crafting a docker-compose.yml file, in which you outline the services, networks, and volumes needed for your application. Next, employ the docker-compose command-line tool to oversee the complete life cycle of your application stack.
YAML representation of a basic configuration
server:
name: my_aws_server
port: 8080
enabled: true
database:
host: localhost
username: type_user_name
password: type_secret_password
In this example, we have a configuration with two main sections, server and database, each containing key-value pairs representing various configuration settings.
YAML is widely used in many domains, including configuration management tools like Ansible and Kubernetes, as well as in various software projects for defining settings, configurations, and data structures in a human-readable format.
📌Docker Compose Installation
Before going to work on task install docker compose on UBUNTU System:
sudo curl -SL https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
Apply executable permissions to the standalone binary in the target path for the installation
sudo chmod +x /usr/local/bin/docker-compose
Verify that Docker Compose is installed correctly by checking the version :
docker-compose --version
👉Practical Approach to learn Docker-Compose
We will learn by performing some tasks:
1) 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.
Create a Directory Structure:
Start by creating a directory for your project. Inside this directory, create a docker-compose.yml.
Write the docker-compose.yml File
version : "3.3" services: web: image: nginx:latest ports: - "80:80" db: image: mysql ports: - "3306:3306" environment: - "MYSQL_ROOT_PASSWORD=test@123"
version: "3.3": Specifies the version of the Docker Compose file format being used.
services: This section defines the different services (containers) in your application.
web: This service is defined with the nginx:latest image, which pulls the latest version of the Nginx image from Docker Hub. It maps port 80 on the host machine to port 80 in the container, allowing you to access the Nginx web server.
db: This service uses the mysql image, which is the official MySQL image from Docker Hub. It maps port 3306 on the host to port 3306 in the container. Additionally, it sets the MYSQL_ROOT_PASSWORD environment variable to "test@123", which will be used as the root password for the MySQL database.
Start the Services:
Open a terminal, navigate to the project directory containing the docker-compose.yml file, and run the following command to start the services:
sudo docker-compose up -d
Docker Compose will build images if needed and start the containers based on your configuration.
Access the Application:
You can access the frontend service by opening a web browser and going to http://localhost. Depending on your backend code, the front end might be configured to make requests to the backend service using its service name (backend) as the hostname.
2) Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine:
Open a terminal and pull an image from Docker Hub. For this example, we'll use the official httpd image (Apache HTTP Server).
docker pull httpd
Give User Permission to Docker:
Add your user to the docker group to allow running Docker commands without using sudo.
sudo usermod -aG docker ubuntu
Reboot the Instance:
After adding your user to the docker group, reboot the system to apply the group changes
sudo reboot
Run the Container as a Non-Root User:
docker run -d -p 8080:80 --name my-httpd-container httpd
Inspect Container:
Use the docker inspect command to inspect the container's running processes and exposed ports:
docker inspect my-httpd-container
View Container Log Output:
View the container's log output using the docker logs command:docker logs my-httpd-container
Stop and Start the Container:
Stop the container:
docker stop my-httpd-container
Start the container:
docker start my-httpd-container
Remove the Container:
When you're done, remove the container:
docker rm my-httpd-container
This walkthrough demonstrates pulling an image, running it as a non-root user, inspecting the container, viewing logs, stopping and starting the container, and finally removing it. You can adapt these steps for other images and scenarios.
How to run Docker commands without sudo?
Add User to Docker Group:
Open a terminal and run the following command to add your user to the Docker group:
sudo usermod -aG docker ubuntu
Log Out and Log In (or Reboot):
After adding your user to the docker group, you need to either log out and log back in, or you can reboot your system to apply the group changes. This step is necessary to ensure that the group changes take effect.
sudo reboot
Verify Permissions:
After logging back in or rebooting, you can verify that your user has the necessary permissions to run Docker commands without sudo by running a Docker command without sudo:
docker info
If you see information about your Docker setup without any permission errors, it means you now can run Docker commands without using sudo.
🎉 Congratulations! You've successfully completed the practical exercises outlined in this blog post. Hands-on experience valuable and that it has enhanced your understanding of Docker Compose . Working through practical examples is a powerful way to solidify your knowledge, and you've done just that.
I hope you enjoy the blog post!
If you do, please show your support by giving it a like ❤, leaving a comment 💬, and spreading the word 📢 to your friends and colleagues 😊
Subscribe to my newsletter
Read articles from Vyankateshwar Taikar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vyankateshwar Taikar
Vyankateshwar Taikar
Hi i am Vyankateshwar , I have a strong history of spearheading transformative projects that have a direct impact on an organization's bottom line as a DevOps Engineer with AWS DevOps tools implementations.