Deploy Applications With Docker Compose

When deploying complex solutions and applications with Docker, there is a need to create structure and order in the deployment.

Docker-compose allows us to deploy applications that have dependencies or rely on other services that run inside other containers.

A good example is WordPress. Deploying WordPress with Docker-Compose is ideal because WordPress is an app that depends on a database service. It is probably a good idea to deploy both together using compose.

How to use Docker Compose

To use Docker compose, we need the following:

  • Docker Compose (Installed by default with Docker Desktop)
  • Create a directory that will act as the name of the application
  • Create a file called docker-compose.yml

docker-compose.yml

Let use a simple example for our deployment. Below I am deploying an application with two containers (webapp and backend ). The webapp container runs Nginx, and the backend run MySQL DB.

services:
  webapp:
    image: nginx
    ports:
      - "80:80"
  backend:
    image: mysql:5.7
    volumes:
      - data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: needed
      MYSQL_DATABASE: needed
      MYSQL_USER: needed
      MYSQL_PASSWORD: needed
 
volumes: 
  data: {}

I’m also attaching a volume for the MySQL DB to save data to the hosts.

To run the application, I will run the following command from the directory of the application.

docker compose up

Once the application is running, I can see it from VS Code

Since port 80 is open via the docker-compose.yml file, I can access the app from my browser.

To shut down the application and delete all the containers run.

docker compose down

2 comments

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.