# Day 18 Task: Docker for DevOps EngineersDocker 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.
    
* `ompose.yml`). This file specifies the services, networks, and volumes required for your application. Here is a basic example:
    
    ```bash
    yamlCopy codeversion: '3'
    services:
      web:
        image: nginx:latest
        ports:
          - "8080:80"
      db:
        image: postgres:latest
        environment:
          POSTGRES_DB: mydatabase
          POSTGRES_USER: user
          POSTGRES_PASSWORD: password
    ```
    
    In this example, two services (`web` and `db`) are defined. The `web` service uses the latest Nginx image and maps port 8080 on the host to port 80 on the container. The `db` service uses the latest PostgreSQL image and sets environment variables for database configuration.
    
    You can run this application with:
    
    ```bash
    bashCopy codedocker-compose up
    ```
    
    And stop it with:
    
    ```bash
    bashCopy codedocker-compose down
    ```
    
    ### **Task-2: Working with Docker Compose and Existing Images**
    
    1. **Pull a Docker Image:**
        
        You can pull an image from Docker Hub using `docker-compose.yml`. Add a `services` section for the image you want to use and define its configuration. Example:
        
        ```bash
        yamlCopy codeversion: '3'
        services:
          web:
            image: nginx:latest
            ports:
              - "8080:80"
        ```
        
        Run it with:
        
        ```bash
        bashCopy codedocker-compose up
        ```
        
    2. **Run as Non-Root User:**
        
        To run Docker commands without sudo, ensure your user is added to the `docker` group:
        
        ```bash
        bashCopy codesudo usermod -aG docker $USER
        ```
        
        After making this change, you need to reboot your machine for the changes to take effect.
        
    3. **Inspect Running Processes and Exposed Ports:**
        
        Use `docker ps` to see running containers and `docker inspect` for detailed information:
        
        ```bash
        bashCopy codedocker ps
        docker inspect <container_id>
        ```
        
    4. **View Container Logs:**
        
        You can view logs using `docker logs`:
        
        ```bash
        bashCopy codedocker logs <container_id>
        ```
        
    5. **Stop and Start Container:**
        
        ```bash
        bashCopy codedocker-compose stop
        docker-compose start
        ```
        
    6. **Remove Container:**
        
        ```bash
        bashCopy codedocker-compose down
        ```
        
        If you want to remove the image as well:
        
        ```bash
        bashCopy codedocker-compose down --volumes
        ```
        
    
    Remember to replace `<container_id>` with the actual container ID.
