# Day 18 - Exploring Docker Compose and Docker Image Management (Dec 14, 2023)

## Understanding Docker Compose

### Docker Compose Overview

[Docker Compose](https://docs.docker.com/compose/) is a powerful tool designed to define and share multi-container applications. Utilizing a YAML file, developers can configure services, establish links between containers, and seamlessly manage the entire environment with a single command.

### What is YAML?

[YAML (YAML Ain't Markup Language)](https://yaml.org/) is a data serialization language commonly used for configuration files. Its human-readable syntax makes it an excellent choice for defining complex structures. YAML files use a `.yml` or `.yaml` extension, and they focus on data representation rather than document markup.

### Task-1: Docker Compose Configuration

To set up the environment and configure services, a `docker-compose.yml` file is created. This YAML file defines the services and links between different containers, allowing for efficient management of the entire application stack.

Sample `docker-compose.yaml` file:

```plaintext
version: '3'
services:
  web:
    image: nginx:alpine
  database:
    image: postgres:alpine
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702542782006/18040a55-63d7-49ba-b052-4e15134c20b1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702542789105/1f808c30-3b25-4d7f-b732-c83290966a80.png align="center")

Now, let's explore using environment variables in the `docker-compose.yml` file to enhance flexibility and customization.

---

## Task-2: Managing Docker Images and Containers

### Running a Pre-existing Docker Image

Pulling a pre-existing Docker image from a public repository like Docker Hub and running it locally is a common DevOps task. Ensure to run the container as a non-root user for security purposes.

```plaintext
# Pulling and running the Docker image as a non-root user
docker run --user <username> <image_name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702543011912/cc3e3766-1dde-486f-ba3e-8f9f5d12df15.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702543024655/07f0d5cb-bb91-4e05-9e61-50d9ce59849d.png align="left")

### Inspecting Container Details

Use the `docker inspect` command to gather detailed information about the container's running processes and exposed ports.

```plaintext
docker inspect <container_id>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702543117417/35e63c0a-084b-4c08-8a61-f9ad346785a4.png align="center")

### Viewing Container Logs

The `docker logs` command helps monitor the container's log output.

```plaintext
docker logs <container_id>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702543161812/fed98ec0-5b44-459f-a6f0-5670506eb5c0.png align="center")

### Stopping, Starting, and Removing Containers

Manage container lifecycle with these commands:

```plaintext
docker stop <container_id>
docker start <container_id>
docker rm <container_id>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702543295408/4178df08-762e-4afa-90d9-09fe94358eb6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702543303573/fb5fbe68-150d-4f2f-9e90-0e9ec274abae.png align="left")

### Running Docker Commands Without sudo

To execute Docker commands without `sudo`, follow these steps:

1. Make sure Docker is installed and your system is updated.
    
2. Add your user to the `docker` group:
    
3. ```plaintext
      sudo usermod -aG docker $USER
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702543011912/cc3e3766-1dde-486f-ba3e-8f9f5d12df15.png align="center")
    
4. Reboot your machine.
    

---
