Docker for DevOps Engineers (Day16)
Hey Everyone!
In our previous discussion, we covered Docker projects for DevOps Engineers. If you haven’t read it yet, you can check it out [here].
Today, we will delve into a Docker for DevOps Engineers(Day16). Previously we deployed a simple Todo Application made using Django. Today we will be doing that with the help of docker compose.
What is docker-compose?
Docker Compose is a tool for running multi-container Docker applications. It enables the configuration of an application, including services, networks, and volumes, using a YAML file. This file simplifies the listing of services for complex applications in a single file, which is particularly useful when working with complex applications with multiple containers running simultaneously.
Task-1: Using docker-compose.yml
The docker-compose.yml
file is used to define and run multi-container Docker applications. Here’s a breakdown of how to use it to set up environments, configure services, link containers, and use environment variables.
Understanding a simple docker-compose.yml file. Consider the following example.
Sample docker-compose.yml
File
version: "3.3"
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql:latest
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=test@123
Key Elements
- Version: Specifies the version of the Compose file format.
- Services: Defines the services that make up your app.
- Service Configuration:
- web: Uses the
nginx:latest
image and maps port 80 on the host to port 80 in the container. - db: Uses the
mysql:latest
image, maps port 3306, and sets the MySQL root password via an environment variable.
Running the Compose File
- Create a
docker-compose.yml
file with the content provided above. - Navigate to the directory containing the
docker-compose.yml
file. - Run
docker-compose up
to start the services defined in the file.
Install Docker and Docker Compose (if not already installed):
- For Docker:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
- For Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Create a
docker-compose.yml
file:
- Open a terminal and create a directory for your project:
mkdir my_project
cd my_project
- Create the
docker-compose.yml
file using a text editor:
nano docker-compose.yml
- Copy and paste the following content into the file:
version: "3.3"
services:
web:
image: nginx:latest
ports:
- "81:81"
db:
image: mysql:latest
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=test@123
- Save and exit the text editor (Ctrl+X, Y, Enter in nano).
- Note the change for the
db
service port mapping (3307:3306
instead of3307:3307
), assuming MySQL default port is 3306.
Run the Docker Compose File:
- In the same directory where the
docker-compose.yml
file is located, run:
docker-compose up
- This command will pull the necessary images and start the containers.
Access the Services:
- Open a web browser and go to
http://localhost
to see the Nginx welcome page. - The MySQL database will be running, accessible on port 3306.
- Check if your
docker-compose.yml
has the correct ports and try to accesshttp://localhost:81/
Task-2: Pulling and Running a Docker Image
Install Docker (if not already installed):
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
Add User to the Docker Group:
- Add your user to the Docker group to run Docker commands without
sudo
:
sudo usermod -aG docker $USER sudo reboot
- After the machine reboots, you can run Docker commands without
sudo
.
Pull an Image from Docker Hub:
- Open a terminal and pull the Nginx image:
docker pull nginx:latest
Run the Container:
- Start the container:
docker run -d --name my_nginx nginx:latest
- This will run the Nginx container in detached mode.
Inspect the Container:
- Get detailed information about the container:
docker inspect my_nginx
View Container Logs:
- Check the logs of the running container:
docker logs my_nginx
Stop and Start the Container:
- Stop the container:
docker stop my_nginx
- Start the container again:
docker start my_nginx
Remove the Container:
- Once you’re done with the container, remove it:
docker rm -f my_nginx
By following these steps, you will successfully complete both tasks. If you encounter any issues, feel free to ask for further assistance.
Running Docker Commands without sudo
Add Your User to the Docker Group:
sudo usermod -aG docker $USER
Reboot Your Machine:
sudo reboot
After rebooting, you should be able to run Docker commands without sudo
.
This is how we deploy multi-containerized applications on a web server using docker-compose.
By completing these tasks, you’ll gain a practical understanding of setting up multi-container applications using Docker Compose and managing Docker containers effectively.
Happy Learning :)