Build a Homelab CI Pipeline for Docker Services with Rollback Support
A practical setup for a Continuous Integration (CI) pipeline in your homelab that automatically deploys Docker services from GitHub repositories and includes rollback capabilities.
Expected Outcome
A working CI pipeline that builds, tests, and deploys Docker containers from GitHub, with the ability to roll back to previous versions if needed.
Assumptions
- Basic knowledge of Docker
- Familiarity with Git and GitHub
- A homelab server with Docker installed
- Access to a terminal/command line interface
- A GitHub account
Bill of Materials
- Homelab server (Linux-based preferred)
- Docker installed
- Git installed
- A text editor (e.g., VSCode, Nano)
- GitHub repository for your Docker project
Build Steps
- Set Up GitHub Repository
Create a GitHub repository for your Docker project if you haven't already. Manual action: Go to GitHub and create a new repository.
Safe to run: read-only
Clone the repository to your local machine using: git clone <repository-url>
- Create Dockerfile
In your repository, create a Dockerfile that defines your application environment.
Example pattern only. Adjust for your environment before running.
touch Dockerfile nano Dockerfile
- Set Up CI/CD Tool
Choose a CI/CD tool (e.g., GitHub Actions, Jenkins) and configure it to build and deploy your Docker image.
Example pattern only. Adjust for your environment before running.
For GitHub Actions, create a .github/workflows/ci.yml file. nano .github/workflows/ci.yml
- Configure CI/CD Pipeline
Add the necessary steps in your CI/CD configuration to build, test, and deploy the Docker image.
Changes system state: review before running
Add build step: 'docker build -t <image-name> .' Add test step: 'docker run --rm <image-name> pytest' Add deploy step: 'docker run -d --name <service-name> <image-name>'
- Implement Rollback Mechanism
Set up a rollback mechanism in your CI/CD tool to revert to the previous version in case of failure.
Changes system state: review before running
Add a rollback step in your CI/CD configuration using: 'docker stop <service-name>' Use 'docker run -d --name <service-name> <previous-image-name>' to deploy the previous version.
- Test the Pipeline
Push a change to your GitHub repository to trigger the CI/CD pipeline and verify that it works as expected.
Safe to run: read-only
git add . git commit -m 'Test CI/CD pipeline' git push origin main
- Monitor and Validate Deployment
Check the status of your deployed service and ensure that it is running correctly.
Safe to run: read-only
docker ps docker logs <service-name>
Validation
- Ensure the Docker container is running and accessible.
- Verify the rollback functionality by simulating a failure and checking if the previous version is deployed.
Troubleshooting
- If a step fails, capture the exact command, exit code, and log line before retrying or changing the design.
- Use `docker compose ps` and `docker compose logs <service>` to separate image, environment, port, and volume problems.
Cleanup or Rollback
- Keep a copy of working configuration, compose files, scripts, and service credentials before removing containers, packages, or data directories.
- Stop test containers with `docker compose down` only after confirming which volumes contain persistent data.
- Rollback by redeploying the previous compose file or image tag and restoring the saved environment file.
Next Improvements
- Explore additional CI/CD tools for more advanced features.
- Integrate automated testing for your Docker services.
- Set up notifications for deployment status.