# Day - 18 : Docker for DevOps Engineers

## 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.

### **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**

**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.**

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.

Docker Compose is used for running multiple containers as a single service. Each of the containers here run in isolation but can interact with each other when required. Docker Compose files are very easy to write in a scripting language called YAML, which is an [**XML**](https://www.simplilearn.com/tutorials/programming-tutorial/what-is-xml)\-based language that stands for Yet Another Markup Language.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690802916416/676b433b-3ebc-4c51-8be6-df57c8aa4feb.png?auto=compress,format&format=webp align="left")

Now lets create a docker -compose.yml inside our project

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690803024414/07acc5b7-f4c9-4bd3-8495-904491470f50.png?auto=compress,format&format=webp align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690803043749/bf93defc-0a60-4e64-89da-5ca66a8ec390.png?auto=compress,format&format=webp align="left")

* **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 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.
    

Now lets run docker-compose.yml file

**docker-compose up:** This command does the work of the docker-compose build and docker-compose run commands.

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/v1690810610700/75814c4a-e253-46da-939f-1cd1218e1ae4.png?auto=compress,format&format=webp align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690810651955/ff870dea-92c6-442e-8141-d7effd13a4c4.png?auto=compress,format&format=webp align="left")

Now lets see our ngnix app is up or not

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690810895931/c9cb9424-49ec-4a03-9fc4-2b906ab1781d.png?auto=compress,format&format=webp align="left")

### **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. Run the container as a non-root user (Hint- Use** `usermod` **command to give user permission to docker). Make sure you reboot instance after giving permission to user.**

For running container as a non-root user, use command **usermod** to give user permission to docker

**sudo usermod -a -G docker $USER**

Then reboot instance after giving permission to user.

**sudo reboot**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690811786381/2320cb7a-f9de-49d4-b407-e6438d14c967.png?auto=compress,format&format=webp align="left")

1. **Inspect the container's running processes and exposed ports using the docker inspect command.**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690811958026/3e6896fa-4d84-4fe7-a471-a8852d73e390.png?auto=compress,format&format=webp align="left")

**Use the docker logs command to view the container's log output.**

**docker logs &lt;container\_name or ID&gt;**

1. **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;**
    

**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;**
