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:
- Dockerize Your Application: Ensure each service has a Dockerfile.
- Set Up Docker Compose: A docker-compose.yml file to orchestrate the containers.
- 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
How It Works
- Trigger on Push: The workflow starts whenever you push to the main branch.
- Build Docker Images: Images for each container are built using docker-compose.yml.
- Push to Docker Hub: Built images are uploaded to Docker Hub (or another container registry).
- 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
- Automation: No manual SSH-ing or running commands — it’s all automated.
- Consistency: Every deployment uses the same images built in CI, ensuring consistency.
- 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)
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!