# Day 18 Task: Docker Compose

**Docker Compose** is used to run multiple containers as a single service. For example, suppose you had an application which required NGNIX and MySQL, you could create one file which would start both the containers as a service without the need to start each one separately.

* 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.
    
    ### 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 Compose ─ Installation**
    
* **Step 1** − Download the necessary files from **github**
    
* ```plaintext
    curl -L "https://github.com/docker/compose/releases/download/1.10.0-rc2/dockercompose
       -$(uname -s) -$(uname -m)" -o /home/demo/docker-compose
    ```
    
    **Step 2** − Next, we need to provide **execute privileges** to the downloaded Docker Compose file, using the following command −
    
    ```plaintext
    chmod +x /home/demo/docker-compose
    ```
    
    ```plaintext
    docker-compose version 
    ```
    
    ## **Creating Docker-Compose File**
    
    ```plaintext
    sudo vim docker-compose.yml 
    ```
    
    ```plaintext
    web:
     build: .
     links:
      - "database_default"
     ports:
      - "8000:80"
     volumes:
      - ".:/app:rw"
      - "./data:/data:rw"
     command: python manage.py runserver 0.0.0.0:80
     env_file: .env-local
    
    database_default:
     image: postgres:13.5-alpine
     environment:
       POSTGRES_DB: "db"
       POSTGRES_HOST_AUTH_METHOD: "trust"
       SERVICE_MANAGER: "fsm-postgres"
     volumes:
      - ".:/app:rw"
    
    Another File------
    vi docker-compose.yml
    
    version : "3.3"
    services:
      web:
        image: nginx:latest
        ports:
          - "80:80"
      db:
        image: mysql
        ports:
          - "3306:3306"
        environment:
          - "MYSQL_ROOT_PASSWORD=test@123"
    ```
    
    * 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 user permission to docker). Make sure you reboot instance after giving permission to user.
        
        ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQFLDCsAtSWxRA/article-inline_image-shrink_1500_2232/0/1678495631319?e=1689206400&v=beta&t=yb0J9aU4K2to4FQ4q2Vt4l018iM-wiZMnlxxmhuTiLI align="left")
        
    
    * Inspect the container's running processes and exposed ports using the docker inspect command.
        
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQH7oKMIpOe6Qw/article-inline_image-shrink_1500_2232/0/1678495648887?e=1689206400&v=beta&t=ywA81v2inCeqR7k9sbrX3swj7LKVCOMBAHdIoe4_pUo align="left")
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQFFpBX6PzqzcQ/article-inline_image-shrink_1500_2232/0/1678495662180?e=1689206400&v=beta&t=1NQ3IbRdvf1JmawuC5p8Znk42SVPataYJMF_6F6cGnA align="left")
    
    * Use the docker logs command to view the container's log output.
        
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQG99NoT_iTE_w/article-inline_image-shrink_1500_2232/0/1678495686369?e=1689206400&v=beta&t=qvggHmyFBovrEWkj1XYp9Fc_qcA9dBMfM-dhNa_U2zI align="left")
    
    * Use the docker stop and docker start commands to stop and start the container.
        
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQFcxXo_pIuOzQ/article-inline_image-shrink_1500_2232/0/1678495699372?e=1689206400&v=beta&t=x8EhpF7tcPGrCV-GQ9bUSHc4V1C1jGS2I3LVQCXtD4U align="left")
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQGUQiZpEfTrEQ/article-inline_image-shrink_1500_2232/0/1678495711435?e=1689206400&v=beta&t=pnEknLF0E-zjZXhHBja9IPCqw5gP1QPl_ol8A8WfSwQ align="left")
    
    * Use the docker rm command to remove the container when you're done.
        
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQHx_iWws6SZMg/article-inline_image-shrink_1500_2232/0/1678495731557?e=1689206400&v=beta&t=mG1wi-DlLpqxgSUrJuZfetDgQKyKZrqjmalwoqb8g3s align="left")
