DEV Community

Cover image for Docker Compose: Deploy a Containerized Application
Israel .O. Ayanda
Israel .O. Ayanda

Posted on • Edited on

Docker Compose: Deploy a Containerized Application

In the previous project, 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

Let's Begin!!!

Deployment with Docker Compose

To follow along, clone the repo for the application here and change the directory to Tooling.

# Clone repo
git clone https://github.com/darey-devops/tooling.git

# change directory
cd tooling/
Enter fullscreen mode Exit fullscreen mode

clone repo

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

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:
Enter fullscreen mode Exit fullscreen mode

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

Give permission to docker, exit and login again

sudo usermod -aG docker ubuntu
Enter fullscreen mode Exit fullscreen mode

Run the command to start the containers

# The f flag specifies the Compose configuration files

docker compose -f tooling_app.yaml  up
Enter fullscreen mode Exit fullscreen mode

compose

Verify that the compose is in the running status:

docker compose ls
Enter fullscreen mode Exit fullscreen mode

running
Ensure port 5000 is opened in your security group.

security group
Verify from the browser

browser view

Congratulation!!! You have successfully deployed a containerized web application using Docker Compose.

Repo: darey.io

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

Top comments (0)