Day 18 : Docker for DevOps Engineers- Part 2

Day 18 : Docker for DevOps Engineers- Part 2

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.

  • Docker containers are created using the docker commands in the command line tool such as command prompt for Windows and terminal for Mac, Linux. Working in the command-line tool is easy when you beginning to learn Docker. However, if you are creating a complex Docker container such as creating multiple Docker containers and connecting them using the network. Managing the complex docker code/command is a tedious task. We have to use the backslash for multiple lines. Editing the commands in the terminal is also not an easy task. To address this problem, Docker proposed a new solution.

  • The solution is docker-compose. The docker-compose needs a YAML file. Put all the configuration code in the YAML file such as Image name, container name, host port and container, environment variables, etc. The YAML file format will be a little bit different from the Docker commands in the command line. I created a pictorial representation to understand it better.

TASK-1:

Learn how to use the docker-compose.yaml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yaml file.

  1. In project “react_django_demo_app”, make a new yaml file named “docker-compose.yaml

    1. Add details like ports, image, environment variables etc.
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674319711267/0380f132-d44a-4b10-89fc-202de87374e9.png align="center")


3\. Come out of yaml file and perform “**docker-compose up**” which aggregates the output of each container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674320457405/ca204e39-84f1-446a-aa02-384af8f45afa.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674320498757/ec6c6fcd-fd37-4b06-8942-055ae11a0f5d.png align="center")

4\. We need to specify environment for sql image and then perform “**docker-compose up**”.

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.

    • Inspect the container’s running processes and exposed ports using the docker inspect command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674321755071/c7344843-490e-4ffc-a5d7-13fa8f790586.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674321779164/e3c7738d-dfbf-4d92-b5fe-aa8e55862f8d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674321802986/7b14f3cd-bb31-4fc2-b733-43be1652fec9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674321821005/eef6bd12-9b2c-462b-b16e-7691b75e39ab.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674321838631/3b04d1ff-13e0-4781-a736-a15db2eca91b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674321854906/358be220-f341-4fc1-993f-203c3f3436e1.png align="center")

* Use the “**docker logs <container\_id>**”command to view the container’s log output.

    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674322001907/7f9c5e19-165d-47a4-ac21-bbc209295af4.png align="center")

    * Use the “**docker stop <container\_id>**” and “**docker start <container\_id>**” commands to stop and start the container.

        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674322138453/cd9dcbc4-b106-490d-b524-d94955872eaf.png align="center")

        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674322185873/629616cf-be77-470a-a1f0-4d505acda7c9.png align="center")

        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674322250793/0133febd-aaaf-4a0b-9e15-a0f15bec7637.png align="center")

        * Use the docker rm command to remove the container when you’re done.

            ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674322360873/473a27ea-402c-4827-8b66-97f235e0caf7.png align="center")

            Thank you for reading this blog. Hope it helps.

            — Safia Khatoon