TekOnline

Drone CI with Docker Compose – Beginner’s Guide πŸ’

What is Drone CI?

Drone is a modern Continuous Integration/Continuous Deployment (CI/CD) platform that uses Docker containers to run your build and deployment steps. It’s lightweight, easy to set up, and works great with Docker Compose.

Prerequisites

  • Docker installed on your system
  • Git repository for your project
  • Basic understanding of Docker and Docker Compose

Step 1: Setting Up Your Project Structure

Create a basic project structure:

your-project/
β”œβ”€β”€ .drone.yml          # Drone configuration
β”œβ”€β”€ docker-compose.yml  # Your application services
β”œβ”€β”€ server/             # Backend code
└── ClientApp/          # Frontend code

Step 2: Creating docker-compose.yml

Here’s a simple example:

version: '3.8'
name: your-app

services:
  backend:
    build: 
      context: ./server
      dockerfile: Dockerfile
    volumes:
      - ./server/uploads:/app/uploads
    restart: always

  frontend:
    build: 
      context: ./ClientApp
      dockerfile: Dockerfile
    ports:
      - "80:80"
    restart: always

volumes:
  # Regular named volumes are managed by Docker
  app_data:

networks:
  default:
    name: your-app-network

Step 3: Creating .drone.yml

Here’s a basic Drone configuration:

kind: pipeline
type: docker
name: deploy

trigger:
  branch:
    - prod
  event:
    - push
    - manual

steps:
  - name: debug-info
    image: docker:dind
    volumes:
      - name: docker-socket
        path: /var/run/docker.sock
    commands:
      - pwd
      - ls -la
      - docker version
      - docker info
      - docker compose version

  - name: cleanup
    image: docker:dind
    volumes:
      - name: docker-socket
        path: /var/run/docker.sock
    commands:
      - echo "Stopping existing containers..."
      - docker ps -q --filter name=your-app_ | xargs -r docker stop
      - docker compose down
      - sleep 5

  - name: deploy
    image: docker:dind
    volumes:
      - name: docker-socket
        path: /var/run/docker.sock
    commands:
      - docker compose pull
      - docker compose down --timeout 30 --remove-orphans
      - docker compose up -d --build --force-recreate
      - docker compose ps

volumes:
  - name: docker-socket
    host:
      path: /var/run/docker.sock

Step 4: Important Notes

Volume Management

  • Use regular named volumes in docker-compose.yml
  • Don’t use external volumes unless absolutely necessary
  • Let Docker manage the volume lifecycle

Deployment Steps

  1. docker compose pullΒ – Gets latest images
  2. docker compose downΒ – Stops and removes containers
  3. docker compose upΒ – Starts containers with new images

Common Pitfalls to Avoid

  1. Don’t use external volumes unless you know what you’re doing
  2. Always include cleanup steps before deployment
  3. Use health checks for services that need them
  4. Keep your configuration simple and straightforward

Step 5: Testing Your Setup

  1. Push your changes to your repository
  2. Drone should automatically trigger a build
  3. Check the Drone dashboard for build status
  4. Verify your application is running correctly

Troubleshooting

Common Issues

  1. Volume not found: Make sure you’re using regular named volumes
  2. Build fails: Check your Dockerfile and docker-compose.yml
  3. Deployment fails: Verify your cleanup steps are working

Debugging Tips

  1. Use the debug-info step to check environment
  2. AddΒ ls -laΒ commands to see file structure
  3. Check Docker logs for container issues

Best Practices

  1. Keep your configuration simple
  2. Use health checks for critical services
  3. Implement proper cleanup steps
  4. Test your pipeline thoroughly
  5. Use secrets for sensitive information

Next Steps

  1. Add testing steps
  2. Implement staging environments
  3. Add deployment notifications
  4. Set up monitoring

Remember: Start simple and add complexity only when needed! πŸ’


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *