DEV Community

Cover image for Docker Multi-Container Deployment with GitHub Actions in Laravel
Mahfuzur Rahman
Mahfuzur Rahman

Posted on

Docker Multi-Container Deployment with GitHub Actions in Laravel

In today’s tech world, applications often rely on multiple interconnected services (containers) to function seamlessly — think of a web app working alongside a database, a cache, and a load balancer. Deploying such a multi-container application can seem daunting, but with GitHub Actions, you can set up a smooth CI/CD pipeline to deploy your app to production. Here’s a casual walk-through of how it’s done.

What’s Multi-Container Deployment?

It’s the process of deploying multiple Docker containers that work together as a system. For instance, a Laravel app might use:

  • A container for the application code.
  • Another is for the MySQL database.
  • Nginx is used to serve static files and proxying requests.

Step 1: Prerequisites

Before jumping into deployment:

  1. Dockerize Your Application: Ensure each service has a Dockerfile.
  2. Set Up Docker Compose: A docker-compose.yml file to orchestrate the containers.
  3. Provision a Server: A VPS (like DigitalOcean or AWS Lightsail) with Docker and Docker Compose installed.

Step 2: Build Your GitHub Actions Workflow

GitHub Actions lets you automate testing, building, and deploying. Here’s a simple workflow for deploying a Dockerized app:

name: Multi-Container Deployment
on:
  push:
    branches: [main]
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      # Checkout code
      - name: Checkout Repository
        uses: actions/checkout@v2
      # Build Docker Images
      - name: Build Docker Images
        run: |
          docker-compose -f docker-compose.yml build
      # Push Images to Docker Hub
      - name: Push Images
        env:
          DOCKER_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
        run: |
          echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
          docker-compose -f docker-compose.yml push
      # Deploy to VPS
      - name: Deploy to VPS
        uses: appleboy/ssh-action@v0.1.4
        with:
          host: ${{ secrets.VPS_SERVER_IP }}
          username: ${{ secrets.VPS_USERNAME }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd /path/to/your/app
            docker-compose pull
            docker-compose down
            docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

How It Works

  1. Trigger on Push: The workflow starts whenever you push to the main branch.
  2. Build Docker Images: Images for each container are built using docker-compose.yml.
  3. Push to Docker Hub: Built images are uploaded to Docker Hub (or another container registry).
  4. Deploy to VPS: The workflow logs into your server via SSH, pulls the latest images, and restarts the containers using docker-compose.

Why This Workflow Rocks

  1. Automation: No manual SSH-ing or running commands — it’s all automated.
  2. Consistency: Every deployment uses the same images built in CI, ensuring consistency.
  3. Simplicity: Docker Compose simplifies managing multi-container apps.

And that’s it! With this workflow, you can confidently deploy your multi-container app without breaking a sweat. Happy deploying!

Top comments (1)

Collapse
 
ehtesham_ali_abc367f36a5b profile image
Ehtesham Ali

This post does a fantastic job explaining multi-container deployment with GitHub Actions in a simple and approachable way! 🚀 The steps are clear, and it's great to see the emphasis on automation, consistency, and the use of Docker Compose to manage everything. As a Laravel developer, I can definitely see the value of setting up CI/CD pipelines for smoother, more reliable deployments. The use of Docker for different services, like the app, database, and Nginx, is a great approach to scaling and maintaining applications. Thanks for sharing this — looking forward to implementing it in my projects!