Till now we have created a Docker file and pushed it to the Repository. Let's move forward and dig more into other Docker concepts. Let's do some study today on 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.
Docker Compose is another best tool for docker to set up multi-container environments. Using this create a single compose file with defining all the containers with their environments. You can easily use a single command to build images and run all the containers.
There is a three-step process to work with Docker Compose.
Define the application environment with Dockerfile for all services.
Create a docker-compose.yml file defining all services under the application.
Run docker-compose up to run all services under applications.
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.
YAML syntax:
Indentation: Think of it like building blocks. When things are related, just indent them.
Key & Value: It's like saying "Here's what this is" and "This is what it means."
Lists: When you want to list things, use a dash
-
. Easy peasy.Words & Numbers: For regular words, just write them. Numbers are like they are, no extra fuss.
Quotes for Special: If you're talking about something special, like spaces in the text, put it in quotes.
Notes for You: Use
#
to write notes only for humans. Computers ignore them.
# Here's a simple YAML example
name: sriparthu
age: 25
isStudent: true
hobbies:
- Reading
- Bloger
- Writer
address:
city: Hyderbad , Telangana
zip: "500030"
Taskπ
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.
Setting Up Environment, Services, Links, and Variables ππ
Here's a step-by-step guide with the given example syntax explained using bullet points and emojis:
Environment Setup: Create a file named
docker-compose.yml
to orchestrate your containers.Version Definition: At the beginning, define the version you're using. In this case,
version: "3.3"
tells Docker Compose what features you're using.Services Section:
- Under
services:
, define each container you want to run as a "service."
- Under
Service Configuration:
For each service (container), set a name, like
web
anddb
.Define their images, like
nginx:latest
andmysql
.
Port Mapping:
Use
ports:
to map your computer's ports to container ports.For example,
"80:80"
means connect your computer's port 80 to the container's port 80.
Container Links (Optional):
Containers can talk by using their service names.
No need to worry about IP addresses.
Environment Variables:
Use
environment:
to set environment variables.For instance,
"MYSQL_ROOT_PASSWORD=test@123"
sets the MySQL root password.
Example Syntax and Explanation:
version: "3.3"
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123"
The
docker-compose.yml
version is set to3.3
.Two services are defined:
web
with Nginx anddb
with MySQL.Ports are mapped so that your computer's port 80 connects to Nginx's port 80, and port 3306 connects to MySQL's port 3306.
The MySQL container's root password is set to
test@123
.
Docker Compose takes this YAML script and creates a symphony of containers, setting up the environment, linking them together, and following your configuration. It's like instructing a team of chefs to cook up a feast together! π½οΈπΆ
Taskππ
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 giving permission to the userPull a Docker Image from Hub π’οΈπ
- Open your terminal and type:
- This fetches the latest version of the "nginx" image from Docker Hub.
π’οΈ Pulling the image... π
Run Container as Non-Root User π³π€
To give your user Docker powers, add them to the
docker
group:sudo usermod -aG docker $USER sudo reboot
Replace
$USER
with your real username.Afterwards, reboot your machine for changes to take effect.
π³π€ Adding user to Docker group... π
Run the Docker Container π’π
- To run the pulled image as a non-root user, use:
-u 1001
assigns UID 1001 to the container's user.-d
runs it in detached mode, and-p
maps ports.
π’π Running the container... π
Access Your Containerized App ππ₯οΈ
Open your browser and navigate to
http://localhost:8080
.You'll see the Nginx default page, indicating success.
π Accessing your app... π»
Cleanup: Stopping and Removing π§ΉποΈ
- To stop the container, use:
Replace
container_id
with the actual container ID.To remove it afterwards:
π§Ή Stopping and removing... ποΈ
Docker brings images to life and running them is like hosting a mini-container party on your machine! ππ°
How to run Docker commands without sudo?
Make sure docker is installed and the system is updated (This is already been completed as a part of previous tasks):
sudo usermod -a -G docker $USER
Reboot the machine. ( or )
sudo chown $USER /var/run/docker.sock
Happy Learning
Thanks For Reading! :)
-Sriparthuπ