# 🔧 Simplify Multi-Container Management: Learn Docker Compose with Nginx and MySQL!

## **Introduction to Docker Compose**

Docker Compose is a powerful tool that makes managing multi-container Docker applications simple and efficient. Whether you're setting up a web app with a database or managing a more complex application, Docker Compose allows you to define all the containers your application needs in a single file and manage them easily.

**What is Docker Compose?**

Docker Compose is a tool that enables you to define and run multi-container Docker applications by defining all the services in a single YAML file.

**What it does:**

Instead of running each container separately, Docker Compose allows you to configure all your containers (services) in one YAML file and start them with just one command, saving time and reducing complexity.

**Use case:**

Docker Compose is ideal for applications that require multiple containers, such as a web app that might need an Nginx server, a MySQL database, and Redis caching. Docker Compose allows you to spin up all these services together as one cohesive unit.

## **Why Docker Compose?**

### **1\. Simplifies Multi-Container Setup**

Instead of running separate `docker run` commands for each service, you can define them all in a single YAML file. This reduces the complexity of managing multiple containers.

### **2\. Configuration Management**

You can centrally store environment variables, network settings, and other configurations, making your setup consistent and easy to manage across different environments (development, testing, production).

### **3\. Easy to Scale**

With Docker Compose, you can scale your services easily by specifying the number of container instances for each service. For example, you can scale your web server to handle more traffic without touching the database configuration.

## **Key Features of Docker Compose**

* **Single YAML File:** All services, networks, and volumes are defined in one file, making it easy to manage.
    
* **Multi-Container Management:** Start, stop, and manage all containers with a single command (`docker-compose up` or `docker-compose down`).
    
* **Environment Variables:** You can use environment variables to configure different services easily without hardcoding sensitive information like passwords.
    

## **Common Docker Compose Commands**

* `docker-compose up`: This command starts all the services defined in the `docker-compose.yml` file, creating and running the containers.
    
* `docker-compose down`: This command stops and removes all the containers and networks created by `docker-compose up`.
    

![The subject of my next guide will be precisely on Docker-Compose. Here is a  s... - DEV Community](https://res.cloudinary.com/practicaldev/image/fetch/s--ATjvxEqa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/n3ej9o6kzs9kjjgir5yk.png align="left")

### **Installing Docker Compose on Ubuntu**

Here’s a step-by-step guide to installing the latest version of Docker Compose on an Ubuntu machine:

1. **Download Docker Compose binary:**
    

```bash
sudo curl -L "https://github.com/docker/compose/releases/download/v2.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
```

2. **Set the executable permissions:**
    

```bash
sudo chmod +x /usr/local/bin/docker-compose
```

3. **Verify the installation:**
    

Check if Docker Compose was successfully installed.

```bash
docker-compose --version
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725525367760/244d23fe-0617-42f8-9d78-b7a9e6421b48.png align="center")

### Practical Task: Setting up a Basic Web Server and MySQL Database

### **Objective:**

In this task, we will learn how to set up an Nginx web server and a MySQL database using Docker Compose to manage multi-container applications.

**Step-by-Step Instructions:**

1. **Create** `docker-compose.yml`: Create a file named `docker-compose.yml` with the following content:
    

```bash
version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: mydb
    ports:
      - "3306:3306"
```

* The **web** service uses the Nginx image and maps port 80 in the container to port 8080 on your machine.
    
* The **db** service uses the MySQL 5.7 image, sets a root password, and creates a database. It maps port 3306 for database access.
    

2. **Save the file** as `docker-compose.yml`.
    
3. **Run Docker Compose**:
    

Navigate to the directory containing your `docker-compose.yml` file and start the containers:

```plaintext
docker-compose up
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725553141760/66ad87f8-f6ee-4230-84b7-f00cf0c3235b.png align="left")

When you run `docker-compose up`, the command keeps running, and the terminal stays open until you press `Ctrl + C` to stop it. But if you need to access the shell of the `db` container, you have to exit this terminal. When you do that, the containers will stop running.

So, to access the `db` container's shell, make sure to restart the containers after exiting.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726341975156/11e7b16e-dc5c-4095-90d1-92a412546cba.png align="center")

4. **Access the Web Server**:
    

Open your browser and navigate to [http://localhost:8080](http://localhost:8080). If you’re using an EC2 instance, replace [localhost](http://localhost) with your instance’s public IP address, e.g.,

http://35.174.155.9:8080

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725553258160/72c02c5e-5fde-4d6a-a958-fd801ed9b10b.png align="center")

**Interact with MySQL**: Access the MySQL container shell:

`The MySQL database is available on port 3306, accessible with the root password rootpassword.`

![Kubernetes Vs Docker Compose: An Overview | K21Academy](https://k21academy.com/wp-content/uploads/2022/10/docker-compose.png align="left")

To access the MySQL CLI, first, get into the shell of the MySQL container.

```bash
docker exec -it <db-container-name> bash
```

Replace `<db-container-name>` with the name of your MySQL container, which you can find using: docker ps.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726341946159/49b8dd39-25a6-47c2-a5d3-91fbc7b639fe.png align="center")

Inside the container, run the following command to access the MySQL CLI:

```bash
mysql -u root -p
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726342116484/e41e4895-fb0a-4558-930b-a3c17f83d67e.png align="center")

You will be prompted for the MySQL root password. Enter the password you set in the `docker-compose.yml` file (e.g., `rootpassword`).

Once inside the MySQL CLI, list the databases:

```sql
SHOW DATABASES;
```

**Expected Outcome**

You should see a list of databases, including the `mydb` database you specified in your `docker-compose.yml` file. It should look something like this:

```diff
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725554429210/ebbb30d5-0d24-4057-af22-4a0bb6cee7fe.png align="center")

If you need to interact with the `mydb` database, you can run:

```sql
USE mydb;
SHOW TABLES;
#This will let you see any tables within the mydb database (if any have been created).
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725554457763/181786f0-7054-4b8d-a9a8-ec4453e1e6fd.png align="left")

6. **Exit MySQL**:
    

* Exit the MySQL CLI by typing `EXIT;`.
    

## **Expected Outcome**

* **Web Service:** You will be able to access the Nginx default web page by going to [`http://localhost:8080`](http://localhost:8080).
    
* **MySQL Service:** You will have a MySQL database running inside a container, with access to it through port `3306`. If everything works as expected, you'll see the list of databases and can interact with MySQL from the command line.
    

## **Conclusion**

Docker Compose simplifies the process of managing multiple containers by defining all your services in a single YAML file. In this blog, you learned how to install Docker Compose, create a `docker-compose.yml` file, and set up a basic web server and MySQL database. With Docker Compose, multi-container management becomes easier and more scalable.

Stay tuned for more advanced Docker Compose topics in future posts!!!
