Here's a complete tutorial that will guide you through setting up an automated deployment pipeline using GitHub Actions, Docker Compose, and Docker Hub. This will deploy any code pushed to the main
branch to a Docker Compose server. Let’s break down the process step-by-step.
- Docker & Docker Compose installed on your server.
- Docker Hub account or access to another Docker registry.
- GitHub repository containing your application code.
- SSH access to your Docker server.
In the root of your project repository, create a Dockerfile
. This file will contain instructions to build a Docker image for your application. Below is an example for a simple Node.js app:
# Use the official Node.js image as a base image
FROM node:14
# Set the working directory in the container
WORKDIR /app
# Copy package.json and package-lock.json to install dependencies
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code to the working directory
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Start the application
CMD ["npm", "start"]
This Dockerfile installs the dependencies, copies the application code, and exposes port 3000
for your app.
In the same directory as your Dockerfile, create a docker-compose.yml
file. This file will define the services and images to deploy. Here’s an example configuration:
version: '3.8'
services:
app:
image: myusername/myapp:latest # Replace with your Docker image name
ports:
- "3000:3000" # Map host port 3000 to container port 3000
restart: always
Replace myusername/myapp
with your Docker Hub repository name (or another registry if you're using it).
Push your Dockerfile and docker-compose.yml
file to your GitHub repository if you haven’t already.
To allow GitHub Actions to access your Docker registry and server, add the following secrets in your GitHub repository:
- Go to Settings > Secrets and variables > Actions.
- Click New repository secret for each secret.
Add the following secrets:
- DOCKER_USERNAME: Your Docker registry username.
- DOCKER_PASSWORD: Your Docker registry password.
- SSH_HOST: The IP or hostname of your Docker Compose server.
- SSH_USERNAME: SSH username for your server.
- SSH_PRIVATE_KEY: Your private SSH key for logging into the server.
- DOCKER_IMAGE: Your Docker image name (e.g.,
myusername/myapp
).
Inside your repository, create a new directory called .github/workflows
. Inside this directory, create a file called deploy.yml
. This file will contain the configuration for GitHub Actions to build and deploy your Docker container.
Here’s an example deploy.yml
file:
name: Deploy to Docker Compose Server
on:
push:
branches:
- main # Trigger the workflow on push to the main branch
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and Push Docker Image
run: |
docker build -t ${{ secrets.DOCKER_IMAGE }}:latest .
docker push ${{ secrets.DOCKER_IMAGE }}:latest
- name: Deploy to Docker Compose Server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /path/to/your/docker-compose # Replace with your actual path
docker-compose pull
docker-compose up -d
- Checkout Code: This step pulls the latest code from the repository.
- Log in to Docker Hub: This logs in to Docker Hub using your credentials, allowing GitHub Actions to push the image.
- Build and Push Docker Image: This builds the Docker image and pushes it to the Docker Hub repository.
- Deploy to Docker Compose Server:
- SSH into your Docker Compose server.
- Navigate to the directory where
docker-compose.yml
is located (replace/path/to/your/docker-compose
). - Pull the latest image and restart the Docker Compose services.
Ensure that your server's SSH configuration allows GitHub Actions to connect using the provided private key (SSH_PRIVATE_KEY
).
- Add the corresponding public key (of
SSH_PRIVATE_KEY
) to the server’s~/.ssh/authorized_keys
file for the user specified inSSH_USERNAME
. - Confirm SSH access by running a test connection from your local machine.
With everything configured:
- Push any changes to the
main
branch of your repository. - The GitHub Actions workflow will be triggered, building, pushing, and deploying your Docker image to the server.
- Docker Image Tagging: This example uses the
latest
tag, but you can use different tags if you prefer versioned deployments. - Deployment Branch: Modify the
branches
setting in theon.push
section if you want to deploy from a branch other thanmain
.
After completing these steps, you’ll have a continuous deployment setup that automatically builds and deploys your app whenever you push changes to your GitHub repository!