Skip to content

wulfland/container-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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/login-action@v1.10.0
            with:
              registry: ${{ env.REGISTRY }}
              username: ${{ github.actor }}
              password: ${{ secrets.GITHUB_TOKEN }}
    
          - name: Extract metadata (tags, labels) for Docker
            id: meta
            uses: docker/metadata-action@v3.5.0
            with:
              images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
    
          - name: Build and push Docker image
            uses: docker/build-push-action@v2.7.0
            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!