docker-build-push/docs/advanced/export-docker.md
Andrei Baibaratsky 6a6e8c7c14
docs: build contexts and bake options
Signed-off-by: Andrei Baibaratsky <andrei@baibaratsky.com>
2022-10-07 18:21:37 +02:00

2.4 KiB

Export image to Docker

You may want your build result to be available in the Docker client through docker images to be able to use it in another step of your workflow:

name: ci

on:
  push:
    branches:
      - 'main'

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Build
        uses: docker/build-push-action@v3
        with:
          context: .
          load: true
          tags: myimage:latest
      -
        name: Inspect
        run: |
          docker image inspect myimage:latest          

Usage of the built image in other build steps

By default, docker/setup-buildx-action@v2 uses docker-container as a build driver, so the docker images are not available in the builder container. To use them, you may use build contexts:

name: ci

on:
  push:
    branches:
      - 'main'

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Build base image
        uses: docker/build-push-action@v3
        with:
          context: base
          load: true
          tags: my-base-image:latest
      -
        name: Build image from my-base-image:latest
        uses: docker/build-push-action@v3
        with:
          context: .
          build-contexts: |
            base-image=docker-image://my-base-image:latest            
          tags: myimage:latest

Where base-imageis the name of the base image (or stage name if specified) in your Dockerfile:

FROM base-image

Bake alternative

You may also want to use bake and build the base image and the target image in one build step:

# docker-bake.hcl
target "base" {
  dockerfile = "baseapp.Dockerfile"
}

target "app" {
  contexts = {
    baseapp = "target:base"
  }
}
      -
        name: Build
        uses: docker/bake-action@v2
        with:
          target: app