Table of contents
- 📚Docker Compose
- 📜What is YAML?
- ✔TASKS
- 1) Setting up the Environment:
- Configuring Services:
- Establishing Links between Containers:
- Using Environment Variables:
- ◼️Run the container as the new user:
- ◼️Inspect the container's running processes and exposed ports:
- ◼️View the container's log output:
- ◼️Stop and start the container:
- ◼️Remove the container:
- How to run Docker commands without sudo?
📚Docker Compose
Docker Compose is a tool provided by Docker that allows you to define and run multi-container Docker applications. It enables you to describe your application's infrastructure (including networks, volumes, and services) in a single YAML file called docker-compose.yml
.
With Docker Compose, you can define the services that make up your application, their dependencies, ports they expose, volumes they use, networks they are connected to, and other configurations. Once you have defined your application stack in the docker-compose.yml
file, you can use the docker-compose
command-line tool to start, stop, and manage your entire application with a single command.
Docker Compose simplifies the process of managing multi-container Docker applications, especially in development and testing environments, by providing a convenient way to define, manage, and scale complex applications. It abstracts away many of the complexities of managing individual containers and their interactions, allowing developers to focus on building and testing their applications.
📜What is YAML?
YAML, pronounced as "YAML" or sometimes "YAML" (rhymes with "camel"), stands for "YAML Ain't Markup Language." It's a human-readable data serialization language that's often used for configuration files, data exchange, and representing structured data in a way that's easy for both humans and computers to understand.
Imagine you have a recipe for making a sandwich. In a recipe book, the steps might be written in a format that's easy for humans to read and follow. YAML is like a format for writing down instructions or information in a clear and simple way that computers can also understand.
sandwich:
name: Grilled Cheese
ingredients:
- bread
- cheese
- butter
steps:
- Take two slices of bread.
- Place cheese between the slices.
- Spread butter on the outsides of the bread slices.
- Heat a skillet over medium heat.
- Cook the sandwich on the skillet until golden brown on both sides.
- Serve hot.
In this example:
We have a "sandwich" object with properties like "name," "ingredients," and "steps."
Under "ingredients," we have a list of items needed to make the sandwich.
Under "steps," we have a list of instructions to follow.
Just like in a recipe book, YAML organizes information in a structured and easy-to-read format, making it great for configuration files, storing data, and defining settings for software applications.
✔TASKS
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.
- Let's break down how to use a
docker-compose.yml
file.
1) Setting up the Environment:
Create a
docker-compose.yml
File: Begin by creating adocker-compose.yml
file in the root directory of your project.Define Version: Specify the version of Docker Compose syntax you're using. For example:
version: '3.8'
- Define Services: Declare the services (containers) that comprise your application under the
services
key.
- Define Services: Declare the services (containers) that comprise your application under the
Configuring Services:
Configure Each Service: Define each service with its specific configuration options. This includes specifying the Docker image, volumes, ports, networks, environment variables, and any other settings needed for that service.
services: web: image: nginx:latest ports: - "8080:80" volumes: - ./html:/usr/share/nginx/html
Establishing Links between Containers:
- Link Containers (Deprecated): Docker Compose provides service-to-service links, but this feature is deprecated. Instead, rely on Docker networks for inter-service communication.
Using Environment Variables:
Define Environment Variables: You can use environment variables in your
docker-compose.yml
file to inject dynamic values into your services. Define them under theenvironment
key within each service.services: web: image: nginx:latest environment: NGINX_PORT: 8080
Use Environment Variables in Configuration: You can reference these environment variables within the configuration settings for the service. For instance, in the
ports
configuration, you could use an environment variable instead of hardcoding the port.services: web: image: nginx:latest ports: - "${NGINX_PORT}:80" environment: NGINX_PORT: 8080
This way, you can have dynamic configurations based on the environment variables passed into your Docker Compose setup.
Example Summary:
Here's a simple example of a docker-compose.yml
file that sets up a web server using Nginx, with a dynamic port configuration using environment variables:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "${NGINX_PORT}:80"
environment:
NGINX_PORT: 8080
TASK-2 :
◼ Pull the Docker Image:
docker pull nginx
◼ Create a new user and give it permission to run Docker commands:
sudo useradd -m newuser
sudo usermod -aG docker newuser
sudo reboot
◼️Run the container as the new user:
sudo su - newuser #login new user
docker run --name mynginx -d -p 8080:80 nginx #run the container
◼️Inspect the container's running processes and exposed ports:
docker inspect mynginx
◼️View the container's log output:
docker logs mynginx
◼️Stop and start the container:
docker stop mynginx
docker start mynginx
◼️Remove the container:
docker rm mynginx
Note: Make sure to replace newuser
with the actual username you want to use, and adjust the Docker image and container names as needed.
How to run Docker commands without sudo?
To run Docker commands without using sudo
, you need to add your user to the Docker group. Here are the steps:
◼️Add your user to the docker group:
sudo usermod -aG docker $USER
◼️Log out and log back in:
su - $USER
◼️Verify Docker command without sudo:
docker ps
◼️Reboot the machine (optional): While not strictly necessary, rebooting the machine can ensure that all changes are applied correctly. But for safe side you can do that.
Happy Learning😊