Skip to content

Latest commit

 

History

History
85 lines (63 loc) · 2.94 KB

README.md

File metadata and controls

85 lines (63 loc) · 2.94 KB

Creating and publishing a docker image to ghcr.io

  1. Create a new repsitory container-demo and add a new file namend Dockerfile (without extension). Add the following content:

    FROM alpine
    CMD ["echo", "Hello World!"]

    If you want to test this locally, clone your repository and change directory to it. Build and run the image. The output is Hello World!:

    $ docker build -t container-demo .
    $ docker run --rm container-demo
    > Hello World!
  2. Create a workflow file .github/workflows/release-container.yml with the following content and commit/push it to your repository:

    name: Publish Docker image
    
    on:
      release:
        types: [published]
    
    env:
      REGISTRY: ghcr.io
      IMAGE_NAME: ${{ github.repository }}
    
    jobs:
      build-and-push-image:
        runs-on: ubuntu-latest
        permissions:
          contents: read
          packages: write
    
        steps:
          - name: Checkout repository
            uses: actions/checkout@v2
    
          - name: Log in to the Container registry
            uses: docker/[email protected]
            with:
              registry: ${{ env.REGISTRY }}
              username: ${{ github.actor }}
              password: ${{ secrets.GITHUB_TOKEN }}
    
          - name: Extract metadata (tags, labels) for Docker
            id: meta
            uses: docker/[email protected]
            with:
              images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
    
          - name: Build and push Docker image
            uses: docker/[email protected]
            with:
              context: .
              push: true
              tags: ${{ steps.meta.outputs.tags }}
              labels: ${{ steps.meta.outputs.labels }}
  3. The workflow gets triggered if you publish a new release. Navigate to Source and click on Create a new release:

    006_new-release
  4. Enter v1.0.0 as a tag and hit enter:

    007_create-tag
  5. Enter a name for the release and publish it:

    008_publish-release
  6. If the workflow has completed you can see the container-demo package under Code | Packages. Click on it to see details:

    009_package-info
  7. To test the image, pull it from the library and run it:

    $ docker pull ghcr.io/<github-user>/container-demo:latest
    $ docker run  --rm ghcr.io/<github-user>/container-demo:latest
    > Hello World!