0
0
DockerHow-ToBeginner · 4 min read

How to Deploy Using Docker in CI: Simple Steps

To deploy using Docker in a CI pipeline, write a pipeline script that builds a Docker image, pushes it to a registry, and then deploys it to your environment. Use commands like docker build, docker push, and deployment steps in your CI tool configuration.
📐

Syntax

Here is the basic syntax pattern for deploying with Docker in CI pipelines:

  • docker build -t <image-name> .: Builds a Docker image from your Dockerfile.
  • docker push <image-name>: Pushes the built image to a Docker registry.
  • Deployment step: Use your CI tool or scripts to deploy the pushed image to your server or cloud.
bash
docker build -t yourusername/yourapp:tag .
docker push yourusername/yourapp:tag
# Deployment depends on your environment, e.g., kubectl, ssh, or cloud CLI
💻

Example

This example shows a simple GitHub Actions workflow that builds a Docker image, pushes it to Docker Hub, and deploys it by SSH to a server.

yaml
name: Docker CI Deploy

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Log in to Docker Hub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build Docker image
      run: |
        docker build -t yourusername/yourapp:latest .

    - name: Push Docker image
      run: |
        docker push yourusername/yourapp:latest

    - name: Deploy to server
      uses: appleboy/ssh-action@v0.1.7
      with:
        host: ${{ secrets.SERVER_IP }}
        username: ${{ secrets.SERVER_USER }}
        key: ${{ secrets.SERVER_SSH_KEY }}
        script: |
          docker pull yourusername/yourapp:latest
          docker stop yourapp || true
          docker rm yourapp || true
          docker run -d --name yourapp -p 80:80 yourusername/yourapp:latest
Output
Run workflow on push to main branch Build Docker image Push Docker image to Docker Hub SSH deploy pulls and runs the new container
⚠️

Common Pitfalls

Common mistakes when deploying Docker in CI include:

  • Not logging into the Docker registry before pushing images.
  • Forgetting to tag images properly, causing overwrites or confusion.
  • Not handling existing containers during deployment, leading to conflicts.
  • Exposing sensitive credentials in the pipeline instead of using secrets.
bash
Wrong:
docker push yourapp:latest  # No login, will fail

Right:
docker login -u $DOCKER_USER -p $DOCKER_PASS
docker push yourapp:latest
📊

Quick Reference

Remember these key steps for Docker deployment in CI:

  • Build your image with docker build.
  • Tag and push your image to a registry with docker push.
  • Use secure secrets for credentials.
  • Deploy by pulling the image and running containers on your target environment.

Key Takeaways

Always log in to your Docker registry before pushing images in CI.
Use clear image tags to manage versions and avoid confusion.
Securely store credentials using CI secrets or environment variables.
Handle existing containers properly during deployment to avoid conflicts.
Automate build, push, and deploy steps in your CI pipeline for smooth delivery.