Day 18 Docker Compose Task: Step-by-Step Instructions
Hey there, tech explorers! ๐ Ready to dive deep into the world of containers? If youโve been playing around with Docker and are looking to take things to the next level, youโre in the right place! In todayโs blog, weโll be exploring Docker Compose and YAML ๐โtwo game-changing tools that can turn your container management from meh to magnificent! ๐ฅ
So, fasten your seatbelt, grab a cup of coffee โ, and letโs dive into it.
๐ Prerequisites ๐
Before we dive in, make sure you have the following in place:
๐ง Linux-based system (Ubuntu, CentOS, etc.) or Windows/Mac with Docker Desktop installed.
๐ณ Docker and Docker Compose are installed and running on your system.
Install Docker:
sudo apt-get update sudo apt-get install docker.io -y
Install Docker Compose:
sudo apt-get install docker-compose -y
Docker Compose Installed: Check with
docker-compose --version
๐จโ๐ป Basic understanding of terminal commands.
๐ Internet connection to pull images from Docker Hub.
What is Docker Compose? ๐ณ๐ค
Imagine you want to run a web app that needs three things:
A frontend to display stuff,
A backend to handle logic, and
A database to store data.
Now, setting up each service manually and linking them can be a pain, right? Thatโs where Docker Compose comes in to save the day! ๐
โจ One file, one command, and BAM! ๐ฅ Your entire environment is up and running in seconds! ๐
In simple terms:
Docker Compose is a tool that allows you to define and manage multiple containers in one single file called
docker-compose.yml
.
It automates everything, from building images to linking containers and managing their networks. ๐ฉโจ
What is YAML? ๐๐ค
YAML is like the secret language ๐คซ Docker Compose speaks!
YAML (Yet Another Markup Language or "YAML Ain't Markup Language") is a human-friendly data format that's easy to read and write. No more confusing syntax or complicated codeโjust simple, clean configurations. ๐งผ
YAML Syntax Basics:
Indentation Matters: Use spaces, not tabs.
Key-Value Pairs: Define data with simple
key: value
pairs.Lists: Use hyphens (
-
) for lists.
Why YAML?
๐ Readable: Easy to read and write.
๐ฏ Structured: Organizes data hierarchically.
โ Widely Used: Popular in DevOps for defining configurations like Docker Compose, Kubernetes manifests, and Ansible playbooks.
Think of YAML as a way to describe what you want Docker to do:
Which containers to start ๐ ๏ธ
How they should communicate ๐ก
What environment variables to use ๐
Hereโs what YAML looks like in action:
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
In this example:
web
anddb
are services.We specify images (like Nginx and MySQL).
We expose ports and set environment variables.
Easy, right? ๐
Letโs Get Hands-On! ๐ช
Task 1: Mastering Docker Compose
Now that you know what Docker Compose and YAML are, letโs get hands-on! ๐ช Hereโs how to set up your environment using a docker-compose.yml
file.
๐งโ๐ณ Step 1: Create a docker-compose.yml
File
Open your favorite code editor (VS Code, Nano, or even Notepad).
Create a file named
docker-compose.yml
.Add the following content to define two services: a web server and a database.
sudversion: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80" # Expose port 8080 on your machine to access the web service
environment:
ENV: production
volumes:
- .:/app
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: mypassword123 # Set environment variables
โ๏ธ Step 2: Run the Services
Open your terminal and navigate to the folder containing docker-compose.yml
. Run:
docker-compose up -d
This command spins up both the web and database services. ๐
๐Step 3: Check the running containers
docker ps
๐ Step 4: Check Everything is Running
Visit http://localhost:8080
in your browser. You should see the Nginx welcome page. If everything works, congrats! ๐ You just ran multiple containers with one command!
โน๏ธ Step 5: Stop the services
docker-compose down
Using Environment Variables ๐ฑ
You can manage environment variables in Docker Compose like this:
Option 1: Directly in the
docker-compose.yml
file (as shown above).Option 2: Use a
.env
file for better manageability:ENV=production MYSQL_ROOT_PASSWORD=mypassword123
Example:
Store sensitive data in a
.env
file:NODE_ENV=production PORT=3000
Update your
docker-compose.yml
to use it:environment: - NODE_ENV=${NODE_ENV} - PORT=${PORT}
Task 2: Running Docker Containers ๐
Letโs take it a step further and work with individual Docker commands.
๐ Step 1: Pull a Docker Image
Grab a pre-built image from Docker Hub:
sudo docker pull nginx:latest
๐จโ๐ป Step 2: Run the Container as a Non-Root User
To run Docker commands without using sudo
(admin rights), follow these steps:
Add your user to the
docker
group:sudo usermod -aG docker $USER
Reboot your machine for changes to take effect:
sudo reboot
After the reboot, check if you can run Docker without
sudo
:docker ps
If you can run the command without
sudo
, youโre all set! ๐
๐ Step 3: Inspect the Container
Run a container:
docker run -d -p 8080:80 --name mynginx nginx:latest
Now inspect its details:
docker inspect mynginx
This will display all the juicy details like exposed ports, IP address, and running processes. ๐ต๏ธโโ๏ธ
๐ Step 4: View Logs
Want to see whatโs happening inside the container? Run:
docker logs mynginx
๐ Step 5: Stop and Start the Container
Stop the container:
docker stop mynginx
Start it again:
docker start mynginx
๐๏ธ Step 6: Remove the Container
Once you're done, clean up by removing the container:
docker rm mynginx
Wrapping It Up with a Fun Twist! ๐
And thatโs a wrap, folks! ๐ฌ You've just unlocked another level in your Docker journey. ๐ Letโs take a quick recap of what we covered:
Docker Compose helps you manage multi-container apps with ease.
YAML is the language you use to configure services in Docker Compose.
You can pull, inspect, log, stop, start, and remove Docker containers like a pro. ๐
Bonus: Running Docker commands without
sudo
makes life much easier. ๐
With Docker Compose and YAML under your belt, youโre now ready to compose containerized symphonies! ๐ต Whether itโs running a simple web app ๐ธ๏ธ or building complex microservices architecture ๐๏ธ, you're in control. Keep experimenting, breaking things ๐ง, and building them back stronger! ๐ฅ
๐ โThe best way to predict the future is to create it.โ โ Abraham Lincoln (and probably every DevOps engineer ever ๐)
Happy Docking! โ See you in the next adventure! ๐ข