Docker Compose
Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, you define a multi-container application in a single file, then spin up your application with a single command. This file, usually named docker-compose.yml
, describes the services, networks, and volumes required by your application.
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's human-readable and expressive nature makes it a popular choice for configuration files, especially in the DevOps and containerization space. YAML files use a .yml
or .yaml
extension.
Task-1: Employing Docker Compose to Establish the Environment
Setting up the environment: Create a docker-compose.yml
file in your directory.
Configuring services: Define the services in docker-compose.yml
file.
COPY
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
environment:
- NGINX_PORT=80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
api:
image: my-api:latest
ports:
- "5000:5000"
environment:
- API_KEY=abc123
depends_on:
- db
db:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=example
You can start this application by running docker-compose up
in the same directory as the docker-compose.yml
file.
Task-2:
◼️Pull the Docker Image:
COPY
docker pull nginx
◼️Create a new user and give it permission to run Docker commands:
COPY
sudo useradd -m myuser
sudo usermod -aG docker myuser
sudo reboot
◼️Run the container as the new user:
COPY
sudo su - myuser #login new user
docker run --name mynginx -d -p 8080:80 nginx #run the container
◼️Inspect the container's running processes and exposed ports:
COPY
docker inspect mynginx
◼️View the container's log output:
COPY
docker logs mynginx
◼️Stop and start the container:
COPY
docker stop mynginx
docker start mynginx
◼️Remove the container:
COPY
docker rm mynginx
Note: Make sure to replace myuser
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:
COPY
sudo usermod -aG docker $USER
◼️Log out and log back in:
COPY
su - $USER
◼️Verify Docker command without sudo:
COPY
docker ps
◼️Reboot the machine (optional): While not strictly necessary, rebooting the machine can ensure that all changes are applied correctly.
Conclusion
To summarize, Docker is a powerful tool for creating, deploying, and managing applications in containers. By using Docker, you can easily package your applications and their dependencies into a standardized unit that can run on any environment. With Docker, you can quickly set up and tear down isolated environments, making it ideal for development, testing, and deployment workflows.
I think this blog will be quite valuable, offering unique viewpoints and introducing new and engaging ideas. 🙏
😊 Happy learning!