Docker Compose: Deploy a Containerized Application
Tags: docker, php, containers, tutorial
Published: 2023-01-26 17:52:31

In the [previous project](https://dev.to/oayanda/deploy-a-containerized-application-4l55), we deployed a containerized application with docker, which involved some processes like creating containers (MySQL server, application and Apache Server) separately, use of a Docker file, creating a network and running couple of docker commands on the command line interface (CLI). This process might become too tedious.  Today, we will build on the previous project understanding and deploy our application containers in a more reliable, efficient and more simpler way using Docker Compose.


## Docker Composer
Compose is a tool from Docker that is used to build applications that consist of more than one Docker container. 
Containers in compose are called services. These services is defined with a YAML file which specify the configuration of your applications (For example, docker-compose.yml) and afterwards, create and start your multi-container with one command.

**Prerequisite**
- [Docker and compose](https://docs.docker.com/engine/install/ubuntu/) is installed on your ubuntu instance or [docker desktop](https://docs.docker.com/desktop/) for PC.
- Basic understanding of [docker and containers](https://dev.to/oayanda/getting-started-docker-container-docker-image-dockerfile-2oj9).
- Basic Linux understanding will be helpful.
- AWS free tier [here](https://aws.amazon.com/free/)

Let's Begin!!!

## Deployment with Docker Compose

To follow along, clone the repo for the application [here](https://github.com/darey-devops/tooling) and change the directory to Tooling.
```bash 
# Clone repo
git clone https://github.com/darey-devops/tooling.git

# change directory
cd tooling/
```
![clone repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ic3vl3dsoid7ue6naemd.png)

Create a file, name it _**tooling_app.yaml**_ and paste the following code snippet.

```bash
version: "3.9"

services:
  tooling_frontend:
    build: .
    ports:
      - "5000:80"
    volumes:
      - tooling_frontend:/var/www/html
    depends_on:
      - db
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: <The database name required by Tooling app >
      MYSQL_USER: <The user required by Tooling app >
      MYSQL_PASSWORD: <The password required by Tooling app >
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
      - ./html/tooling_db_schema.sql:/docker-entrypoint-initdb.d/tooling_db_schema.sql
volumes:
  tooling_frontend:
  db:
```

> _Update the MYSQL_DATABASE, MYSQL_USER and MYSQL_PASSWORD in the configuration file above._

- **_version:_** Specify the version of docker compose to use
- **_services:_** Containers to be create and run
- _**build:**_ specifies the build configuration(the Dockerfile we usee in the previous project) for creating container image from source.
- _**port:**_ specifies the mapping of the host port to the container port
- _**volumes:**_ Specify the storage or data
- _**depends_on:**_ Express dependency between services
- _**image:**_ Specifies the docker image to run from.
- _**restart:**_ restart the container

Under services, you have the frontend application and the MySQL database. The YAML file also use the Dockerfile configuration in the directory.The Dockerfile specifies the configuration for the frontend and Apache Server.

Make sure the your update the `html/db_conn.php`.

MYSQL_IP mysql ip address "db" from the compose file
MYSQL_USER mysql username for user export as environment variable
MYSQL_PASS mysql password for the user exported as environment varaible
MYSQL_DBNAME mysql databse name "toolingdb"

`html/db_conn.php`
![db](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rh9e0wtj3ez1rc5el48.png)

Give permission to docker, exit and login again
```
sudo usermod -aG docker ubuntu
```
Run the command to start the containers

```
# The f flag specifies the Compose configuration files

docker compose -f tooling_app.yaml  up
```
![compose](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bdc7o7o1trlea4k8v2n.png)

Verify that the compose is in the running status:

```
docker compose ls
```
![running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jrlbvtyxa08tj23q265.png)
Ensure port 5000 is opened in your security group.

![security group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sq4tadgtmkmn6fxzupqk.png)
Verify from the browser

![browser view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndc5df99967ff1ott9rq.png)

Congratulation!!! You have successfully deployed a containerized web application using [Docker Compose](https://docs.docker.com/compose/).

_Repo: darey.io_

As always, I look forward to getting your thoughts on this article. Please feel free to leave a comment!