Search code examples
dockerdockerfilegithub-actions

Docker --output in Github Actions


I have a Dockerfile that creates a file.

Dockerfile

FROM --platform=linux/amd64 amazonlinux:2 AS base

RUN mkdir /output && touch /output/hello.txt

FROM scratch AS export
COPY --from=base /output/hello.txt /

if I build the Dockerfile locally with

docker build -f Dockerfile --output . .

I have my hello.txt file in my folder as expected. But if I do the same through Github Actions as

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

  - name: Login to Docker Hub
    uses: docker/login-action@v1
    with:
      username: ${{ secrets.DOCKER_HUB_USERNAME }}
      password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

  - name: Set up Docker Buildx
    uses: docker/setup-buildx-action@v1

  - name: Run
    run: |
      docker build -f Dockerfile --output . .
 
  - name: Check
    run: |
      ls -la

the file is not in the folder. Do you know how can I retrieve the Dockerfile output through github actions?


Solution

  • The custom build output functionality you are using is available with the Buildkit backend. To use the Buildkit backend over the classic backend you either can:

    1. Specify DOCKER_BUILDKIT=1 when running the docker command
    2. Set it in the docker engine by default by adding "features": { "buildkit": true } to the root of the config json.
    3. Use docker buildx as you are attempting

    Docker buildx is being set up in the Github Actions so just have to replace docker build with docker buildx build.

    steps:
      - name: Checkout 
        uses: actions/checkout@v2
    
      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
    
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
    
      - name: Run Buildx
        run: |
          docker buildx build -f Dockerfile --output . .
     
      - name: Check
        run: |
          ls -la