Day 18 Task: Docker for DevOps Engineers

Day 18 Task: Docker for DevOps Engineers

Mastering Docker Commands

Introduction
Docker Compose
YAML
Task-1
Task-2
run Docker commands without sudo
conclusion

──────────────────────────────────────────────────

Introduction📖🚀

🚀 Welcome back to our DevOps adventure! Today, we're diving into Docker – a superhero tool for DevOps engineers! 🐳 As we sail through the DevOps universe, Docker emerges as a mighty containerization platform, making software development, testing, and deployment a breeze. Let's unlock the power of Docker together! Docker simplifies application design, shipping, and deployment by encapsulating dependencies in isolated containers. Explore how Docker Compose streamlines the setup of multiple services, including a hands-on demonstration in this article✨✨

───────👉🏻👉🏻👉🏻👉🏻👉🏻Docker Compose👈🏻👈🏻👈🏻👈🏻👈🏻───────

Docker Compose will execute a YAML-based multi-container application. The YAML file consists of all configurations needed to deploy containers Docker Compose, which is integrated with Docker Swarm, and provides directions for building and deploying containers. With Docker Compose, each container is constructed to run on a single host.

Install Docker Compose

We can run Docker Compose on macOs, Widows, and 64-bit Linux.

For any significant activity, Docker Compose depends on Docker Engine. Depending on your arrangement, we must ensure that Docker Engine is installed either locally or remotely.

  • A desktop system such as Docker for Mac and Windows comes with Docker Compose preinstalled.

  • Install Docker first as instructed in Docker installation on the Linux system before beginning the installation of Docker Compose.

Steps to install Docker Compose

Step 1: The following scripts will install the most recent version of Docker Compose and update the package management.

sudo apt-get update
sudo apt-get install docker-compose-plugin

Here we are using the Ubuntu flavor in the Linux Operating system. So the package manager is “apt-get” If you want to install it in Redhat Linux then the package manager will be “yum”.

Step 2: Check the version to ensure that Docker Compose is installed successfully.

docker-compose version

Why Docker Compose?

As discussed earlier, a real-world application has a separate container for each of its services. And we know that each container needs to have a Dockerfile. It means we will have to write maybe hundreds of docker files and then manage everything about the containers individually, That’s cumbersome.

Hence we use docker-compose, which is a tool that helps in the definition and running of multi-container applications. With the help of Docker Compose you can start and stop the services by using its YAML file.

Docker-compose allows us to start and stop all of the services with just a few simple commands and a single YAML file for each configuration.

Now, let’s see how we can use docker-compose, using a simple project.

How to Use Docker Compose? With example

In this project, we will create a straightforward Restfull API that will return a list of fruits. We will use a flask for this purpose. And a PHP application will request this service and show it in the browser. Both services will run in their own containers.

  • First, Create a separate directory for our complete project. Use the following command.
mkdir dockerComposeProject
  • Move inside the directory.
cd dockerComposeProject

Create API

we will create a custom image that will use Python to serve our Restful API defined below. Then the service will be further configured using a Dockerfile.

  • Then create a subdirectory for the service we will name it product. and move into the same.
mkdir product
cd product

Build Python api.py

from flask import Flask
from flask_restful import Resource, Api

# create a flask object
app = Flask(__name__)
api = Api(app)

# creating a class for Fruits that will hold
# the accessors
class Fruits(Resource):
    def get(self):
      # returns a dictionary with fruits
        return {
            'fruits': ['Mango',
                        'Pomegranate',
                        'Orange',
                        'Litchi']
        }

# adds the resources at the root route
api.add_resource(Fruits, '/')

# if this file is being executed then run the service
if __name__ == '__main__':
      # run the service
    app.run(host='0.0.0.0', port=80, debug=True)
  • Create a Dockerfile to define the container in which the above API will run.

Create Dockerfile For Python API

  •   FROM python:3-onbuild
      COPY . /usr/src/app
      CMD ["python", "api.py"]
    

    FROM accepts an image name and a version that the docker will download from the docker hub. The current working directory’s contents can be copied to the location where the server expects the code to be by using the copy command. Moreover, the CMD command takes a list of commands to start the service once the container has been started.

    Create Php HTML Website

    Let’s create a simple website using PHP that will use our API.

    • Move to the parent directory and create another subdirectory for the website.
    cd ..
    mkdir website
    cd website

index.php

    <!DOCTYPE html>
    <html lang="en">
    <head>
       <title>Fruit Service</title>
    </head>
    <body>
       <h1>Welcome to India's Fruit Shop</h1>
       <ul>
           <?php
               $json = file_get_contents('http://fruit-service');
               $obj = json_decode($json);
               $fruits = $obj->fruits;
               foreach ($fruits as $fruit){
                   echo "<li> $fruit </li>";
               }
           ?>
       </ul>
    </body>
    </html>
  • Now create a compose file where we will define and configure the two services, API and the website.

  • Move out of the website subdirectory using the following code.

    cd ..

And then create the file name as . docker-compose.yaml

Create Docker-compose.yaml file

    version: "3"

    services:
     fruit-service:
       build: ./product
       volumes:
         - ./product:/usr/src/app
       ports:
         - 5001:80

     website:
       image: php:apache
       volumes:
         - ./website:/var/www/html
       ports:
         - 5000:80
       depends_on:
         - fruit-service

Docker-compose.yaml file Explanation

The first line is optional where we specify the version of the docker-compose tool. Next services define a list of services that our application is going to use. The first service is fruit service which is our API and the second one is our website. The fruit service has a property build that contains the dockerfile that is to be built and created as an image. Volumes define storage mapping between the host and the container so that we can make live changes. Finally, port property exposes the containers port 80 through the host’s 5001.

The website service does not use a custom image but we download the PHP image from the Docker hub and then map the websites folder that contains our index.php to /var/www/html (PHP expects the code to be at this location). Ports expose the container port. Finally, the depends_on specifies all the services on which the current service depends.

Run the application stack

Now that we have our docker-compose.yml file, we can run it.

  • To start the application, enter the following command.
docker-compose up -d

Now all the services will start and our website will be ready to be used at localhost:5000.

  • Open your browser and enter localhost:5000.

Output

Application from internet

  • To stop the application, either press CTRL + C or
docker-compose stop

Conclusion🔚🔚🔚

Day 18: Docker Compose Unleashed! 🚀

Today, we explored the game-changing Docker Compose, simplifying multi-container orchestration with its YAML wizardry. We sailed through installation, understood its importance, and built a delightful Fruit Shop app, showcasing the power of streamlined service coordination. Docker Compose, our DevOps ally, makes complex deployments a breeze. Ready for more DevOps magic? Stay tuned! 🐳✨ #DevOps #DockerComposeMagic