# Docker-Compose

## **Setup Docker Compose**

**Docker compose** is a tool that simplifies the management of multi-container applications by allowing you to define and run them using a single YAML file. It automates the process of managing several Docker containers simultaneously, making it quicker, easier, and more repeatable compared to manual methods. Docker compose enables you to encapsulate application requirements like storage volumes, environment variables, and port bindings in a YAML file improving developer ergonimics and supporting the reuse of configuration in different environments. It helps in orchestrating and coordinating various services, streamlining development deployment, and management processes. &#x20;

#### Understanding Docker Compose

**Docker Compose** is an essential tool for developers working with Docker, particularly when dealing with multi-container applications. At its core, it uses a single YAML `.yml` file to define multiple containers and their relationships, so that they can be managed as a single service.

Key Features:

* **Simplification of Multi-container Management**: With Docker Compose, you can run and manage multiple containers as a single service, which significantly streamlines development and testing workflows.
* **YAML File Configuration**: All the services, networks, and volumes are defined in a YAML file, making the setup and configuration of your application’s environment straightforward and versionable.
* **Automation**: It automates the deployment of your application by allowing the definition of all your service dependencies in a single file.
* **Environment Standardization**: Docker Compose ensures that your application runs in the same way in every environment, eliminating the "it works on my machine" problem.

## **Prerequisites:**

Make sure you have:

* Installed the latest version of Docker Compose.
* A basic understanding of Docker concepts and how Docker works

### Update && Upgrade the system

```
sudo apt-get update -y && sudo apt-get upgrade -y
```

### Install docker compose

```
sudo apt-get install docker-compose -y
```

Check the version

```
docker-compose version
```

### **Configuration and example of docker-compose**&#x20;

To set up Docker Compose with a service and a database example, we can create a simple setup with a web application service and a MySQL database service. Here's how you can do it:

Create a project directory and navigate into it:

```
mkdir docker_project
cd docker_project
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FZR0KDdNEHc1M8DcHZ0k5%2Fimage.png?alt=media&#x26;token=0f043a07-b4e6-4ed3-80b0-e8b3fae69b7f" alt=""><figcaption></figcaption></figure>

### Create docker-compose file

Create a Docker Compose file named `docker-compose.yml` in your project directory. Here's an example of a Docker Compose file that defines a web service using Nginx and a MySQL database service:

```
vi docker-compose.yml
```

```
version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
    depends_on:
      - db
  db:
    image: mysql
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: mydb
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypassword
```

Paste the file inside the docker-compose.yaml and save **wq**!  Enter.

### Build and run your app with docker-compose.

```
sudo docker-compose up -d
```

The `-d` flag runs the services in detached mode.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FS73ZmkQJaqYPsvQgTqcI%2Fimage.png?alt=media&#x26;token=bb8535fc-3cb2-4ed4-b932-96a3dc737dd5" alt=""><figcaption></figcaption></figure>

Verify the running services and their status:

```
sudo docker-compose ps
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FRn2udrw4nn2WeYp2F2xg%2Fimage.png?alt=media&#x26;token=3175d2f2-3fa3-45d3-a95c-b63ad94fcc6d" alt=""><figcaption></figcaption></figure>

Access the web service: You can access the Nginx web server running on port 80 by opening **<http://server\\_ip\\_address:80>** in your web browser.

**Access the MySQL database: You can access the MySQL database service by connecting to it using a MySQL client.**

Now you can check that mysql\_server is running on port 3306 by opening  **<http://server\\_ip\\_address:3306> i**n your browser.

### Check the running container

Also if you want to log in to your MySQL database navigate to the terminal and run these commands

```
docker ps -a
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FsjO5kSa97bUH7WthX7Zb%2Fimage.png?alt=media&#x26;token=6ca5dc93-b1b8-43d5-a7f3-2b22cfe6d0f1" alt=""><figcaption></figcaption></figure>

Login MySQL-server

```
docker exec -it container_ID /bin/bash
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FJ4b1o8x3SXlj0cREaJrq%2Fimage.png?alt=media&#x26;token=755b926e-076b-42c2-a6ea-ea82561859fa" alt=""><figcaption></figcaption></figure>

Now run these command

```
mysql -u username -p
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FOmxZTFEILRmiVnHJh3NW%2Fimage.png?alt=media&#x26;token=c0249333-0b76-425e-b0db-addaf3555a24" alt=""><figcaption></figcaption></figure>

## How to Push an Image to Docker Hub

Docker Images Pushed in the Docker Hub&#x20;

First, sign up on Docker Hub.

### **Go to the docker** [**https://hub.docker.com/**](https://hub.docker.com/)

Follow these steps to push an image to Docker Hub:

### **Log In to Docker Hub:**

```sh
docker login
```

Enter your Docker Hub credentials when prompted.

### **Tag Your Image:**

```sh
docker tag <local-image> <username>/<repo>:<tag>
```

### **Push the Image:**

```sh
docker push <username>/<repo>:<tag>
```

### **Verify the Image on Docker Hub:** Go to Docker Hub and ensure the image appears in your repository.

```
sudo docker login 
```

Give the GitHub  username and Password&#x20;

check docker images&#x20;

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FNdOSoc5JKfjTnDuZNCsb%2Fimage.png?alt=media&#x26;token=c52b9bd2-7947-441c-8d5b-02fbe707775e" alt=""><figcaption></figcaption></figure>

Push the image with the tag name in the docker\_hub

```
sudo docker tag your_image_name:latest docker_hub_username/new_name_image
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FkSTICaX9mx1Pw96L82Dr%2Fimage.png?alt=media&#x26;token=42224fef-f2d2-4974-842d-ffac01e06d50" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FY1EfBjwFYvPsv8gcCXWi%2Fimage.png?alt=media&#x26;token=2496a320-cade-4bba-baad-e188e332cdd0" alt=""><figcaption></figcaption></figure>
