How to Deploy an App in a Container Using Docker Compose

How to Deploy an App in a Container Using Docker Compose

·

3 min read

Deploying an application in a containerized environment can streamline development and deployment processes, making them more efficient and consistent. In this tutorial, we’ll walk through the steps to deploy a web application and a database using Docker Compose. We'll use a PHP web server and a MariaDB database. Follow these steps to set up and deploy the application on your server.

Prerequisites:

Before we begin, ensure you have the following:

  • Access to App Server.

  • Docker and Docker Compose installed on App Server.

  • Basic knowledge of Docker and Docker Compose

Step 1: SSH into the Host Server:

First, you need to SSH into App Server. Open your terminal and run the following command:

ssh user@<server-ip>

Replace <server-ip> with the actual IP address of App Server. You may need to replace user with your username.

Step 2: Create the Docker Compose File:

Navigate to the directory where you want to create the Docker Compose file. In this tutorial, we will create it in the /opt/security/ directory.

cd /opt/security

Now, create a new Docker Compose file named docker-compose.yml:

nano docker-compose.yml

Step 3: Define the Services in Docker Compose File:

Copy and paste the following configuration into the docker-compose.yml file:

version: '3'

services:
  php_web:
    container_name: php_web
    image: php:apache
    ports:
      - "3001:80"
    volumes:
      - /var/www/html:/var/www/html

  mysql_web:
    container_name: mysql_web
    image: mariadb:latest
    ports:
      - "3306:3306"
    volumes:
      - /var/lib/mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=P@ssw0rd
      - MYSQL_DATABASE=database_web
      - MYSQL_USER=user
      - MYSQL_PASSWORD=P@ssw0rd

This configuration file defines two services: php_web and mysql_web.

php_web:

  • Container name: php_web

  • Image: php:apache

  • Ports: Map port 80 of the container to port 3001 on the host

  • Volumes: Map /var/www/html directory of the container to /var/www/html on the host

mysql_web:

  • Container name: mysql_web

  • Image: mariadb:latest

  • Ports: Map port 3306 of the container to port 3306 on the host

  • Volumes: Map /var/lib/mysql directory of the container to /var/lib/mysql on the host

  • Environment variables:

    • MYSQL_ROOT_PASSWORD=P@ssw0rd

    • MYSQL_DATABASE=database_web

    • MYSQL_USER=user

    • MYSQL_PASSWORD=P@ssw0rd

Save and close the file (in nano, press CTRL + X, then Y, then ENTER).

Step 4: Deploy the Application:

Run the following command to deploy the application using Docker Compose:

docker-compose up -d

This command will download the necessary images and start the containers in detached mode.

Step 5: Verify the Deployment:

Once the deployment is complete, you can verify that the application is running by accessing it with the curl command:

curl <server-ip>:3001

Replace <server-ip> with the IP address of App Server 1. You should see the response from the PHP web server.

Conclusion

You've successfully deployed a web application and a database using Docker Compose. This setup allows you to run and manage your application in a containerized environment, making it easier to develop, test, and deploy consistently. For further customization, you can modify the docker-compose.yml file to suit your specific requirements.