Why Use Docker with CI/CD?
Docker ensures consistent environments across development, testing, and production. When combined with CI/CD, it creates a powerful pipeline for automated testing, building, and deployment.
1. Project Setup
# .dockerignore
.git
.env
node_modules
Dockerfile Example
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
2. GitHub Actions Pipeline
Automate testing and deployment with a GitHub Actions workflow.
.github/workflows/ci-cd.yml
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Build Docker image
run: docker build -t yourname/app:latest .
- name: Test Application
run: |
docker run --rm -e TEST=true yourname/app npm test
- name: Push to Docker Hub
run: |
docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASS }}
docker tag yourname/app yourname/app:latest
docker push yourname/app
3. Deploy with Docker
Use Docker Compose for easy deployment of your application.
docker-compose.yml
version: '3'
services:
app:
image: yourname/app:latest
ports:
- "3000:3000"
environment:
- NODE_ENV=production
Key Takeaways
- Use
.dockerignore
to exclude unnecessary files - Write multi-stage builds for optimized images
- Use environment variables for configuration
- Integrate security scanning in your pipeline
Ready to Level Up?
Try implementing this pipeline in your own project and share your experience in the comments below!
View Docker Docs