DEV Community

aurangzaib
aurangzaib

Posted on

Run Docker with GitHub Actions

Docker is a popular platform for developing, deploying, and running applications in containers. GitHub Actions is a powerful platform for automating software workflows, including continuous integration and continuous delivery (CI/CD) processes.

  1. Start by creating a GitHub Actions workflow in your repository. You can do this by creating a new file in the ".github/workflows" directory
  2. Define the steps for your workflow. Here's an example of a basic workflow that builds and pushes a Docker image to a registry
name: Docker CI/CD

on:
  push:
    branches: [ main ]

env:
  DOCKER_REGISTRY: docker.io
  DOCKER_IMAGE: yourusername/yourimage

jobs:
  build:
    runs-on: ubuntu-latest

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

    - name: Build Docker Image
      uses: docker/build-push-action@v2
      with:
        context: .
        push: true
        tags: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE }}:${{ env.TAG_NAME }}

Enter fullscreen mode Exit fullscreen mode
  1. In this example, the workflow is triggered on pushes to the "main" branch of your repository. The environment variables "DOCKER_REGISTRY" and "DOCKER_IMAGE" are used to specify the Docker registry and image name, respectively.
  2. The "Checkout code" step uses the checkout action provided by GitHub Actions to check out the code in your repository.

  3. The "Build Docker Image" step uses the Docker CLI action to build and push the Docker image to the specified Docker registry. The "context" parameter specifies the build context, which is the current directory in this case. The "push" parameter is set to "true" to indicate that the image should be pushed to the registry.

  4. Test your workflow by pushing a change to your repository and check the Actions tab to see if your workflow is executed successfully.

Top comments (0)