Docker for DevOps Engineers

Docker for DevOps Engineers

#90 Days of DevOps Challenge - Day 18

Docker Compose

Docker Compose is a tool that assists in defining and sharing multi-container applications. By using Compose, we can define the services in a YAML file, as well as spin them up and tear them down with one single command.

For example:

If you have an application that requires an NGINX server and Redis database, you can create a Docker Compose file that can run both containers as a service without the need to start each one separately.

Benefits of Docker Compose

  • Single host deployment - This means you can run everything on a single piece of hardware

  • Quick and easy configuration - Due to YAML scripts

  • High productivity - Docker Compose reduces the time it takes to perform tasks

  • Security - All the containers are isolated from each other, reducing the threat landscape

Basic Commands in Docker Compose

CommandExplanation
Docker Compose upStart all services
Docker Compose downStop all services
pip install -U Docker-composeInstall Docker Compose using pip
Docker-compose-vCheck the version of Docker Compose
Docker-compose up -dRun Docker Compose file
Docker psList the entire process
Docker Compose up -d -scaleScale a service
Docker Compose.ymlUse YAML files to configure application services

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.

Task-1:- Running Multiple Containers using Docker Compose

Step 1:- First we need to clone the docker image from the docker hub by using the below command

 git clone https://github.com/Saikat55/react_django_demo_app.git

Step 2:- Now we need to use docker-compose in the Linux machine, By the below command

sudo apt-get install docker-compose -y

Step 3:- Using the Vim editor we need to create a docker-compose file

version: '3.9'

services:
  web:
    image: saikat55mukherjee/react_django_app:latest
    ports:
      - "8001:8001"

Step 4:- Now we need to start the container using the below command

sudo docker-compose up -d

Step 5:- Now we need to search in the web browser along with the ipv4 address and port number

Step 6:- If we need to down or close the application then need to use below command

sudo docker-compose down

Task 2:-

  • 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
docker pull saikat55mukherjee/apache:latest

  • Inspect the container's running processes and exposed ports using the docker inspect command.
docker inspect <container_id>

  • Use the docker logs command to view the container's log output.
docker logs <container_id>

  • Use the docker stop and docker start commands to stop and start the container.
docker stop <Container_id>
docker start <conatiner_id>

  • Use the docker rm command to remove the container when you're done.
docker rm <conatiner_id>

How to run Docker commands without sudo?

Step 1:- Make sure docker is installed and the system is updated

Step 2:- Need to add the below user by below command

sudo usermod -a -G docker $USER

Step 3:- Need to Reboot the machine. After reboot, we can run any command without using sudo

sudo reboot

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Did you find this article valuable?

Support Saikat Mukherjee's blog by becoming a sponsor. Any amount is appreciated!