# Day 18: Docker for DevOps Engineers

### **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.
    
* Compose works in all environments: production, staging, development, testing, as well as CI workflows. It also has commands for managing the whole lifecycle of your application:
    
    * Start, stop, and rebuild services
        
    * View the status of running services
        
    * Stream the log output of running services
        
    * Run a one-off command on a service.
        

### **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 <mark>yet another markup language</mark> or YAML <mark>ain’t markup language</mark> (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.
    
    ### **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.
        
        ### **Task-1**
        
    
    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.
    
* **1\. Install Docker-Compose first on your local machine.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687756029870/32877007-6b15-4e0b-b994-836cec402afe.png align="center")
    
* **2\. Create the docker-compose file inside the project folder. By writing the docker-compose.yml file as below.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687756198478/704dc212-c417-45a5-8f7b-cc15b36f5485.png align="center")
    
* **version: "3.3"** denotes that we are using version 3.3 of Docker Compose.
    
* The **services** section defines all the different containers we will create.
    
* The **build** keyword specifies the location of our Dockerfile, and . represents the directory where the docker-compose.yml file is located.
    
* The **image** keyword is used to specify the image from the docker hub for MySQL containers
    
* For the database and web, we are using the **ports** keyword to mention the ports that need to be exposed.
    
* And then, we also specify the **environment variables** for MySQL which are required to run MySQL.
    
    **3.Run the docker-compose.yml file**
    
    **docker-compose up** This command does the work of the docker-compose build and docker-compose run commands.**\-d** open in detached mode.
    
    **docker ps** or **docker-compose ps** command list all the containers in the current docker-compose file.
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687756637307/679896d3-88ac-442e-8ab4-a75561e19f59.png align="center")
    

### **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 (Hint- Use** `usermod` **command to give the user permission to docker). Make sure you reboot the instance after giving permission to the user.**
    
* For running the container as a non-root user, the **usermod** command is used to give the user permission to docker
    
    **<mark>sudo usermod -a -G docker $USER</mark>**
    
    Then reboot the instance after giving permission to the user.
    
    **<mark>sudo reboot</mark>**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687757622882/784a1fdd-c984-4ff4-bfb7-3ac83750e4c0.png align="center")
    
    Copy the docker image command from dockerhub repository.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687757864885/d9af4723-9ed4-4c3c-8b14-12a38850845e.png align="center")
    
* **Inspect the container's running processes and exposed ports using the docker inspect command.**
    
    **docker inspect &lt;container\_name or ID&gt;**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687757985851/f022c449-ed3b-4c2d-ba98-ee06aeaf4bd4.png align="center")
    
* **Use the docker logs command to view the container's log output.**
    
    **docker logs &lt;container\_name or ID&gt;**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687758091038/15911324-18b3-4606-8bbb-0b34c18a7e25.png align="center")
    
* **Use the docker stop and docker start commands to stop and start the container.**
    
    **docker stop:** To stop one or more running Docker containers.
    
    **docker stop &lt;container-name or ID&gt;**
    
    **docker start:** Start one or more stopped containers
    
    **docker start &lt;container-name or ID&gt;**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687758209203/7f753308-16ab-4fd1-b747-20267364f144.png align="center")
    
* **Use the docker rm command to remove the container when you're done**.
    
    **docker rm:** Remove one or more containers.
    
    \--force, -f: Force the removal of a running container.
    
    **docker rm &lt;container\_name or ID&gt;**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687758355801/3665b3cd-57c0-48ce-ad64-6ea2c28df28f.png align="center")
    
    ***Thanks for reading this article !!***
    
    ***#90DaysOfDevOps Challange. Happy Learning :)***
