Home > Software engineering >  Build Docker image using GitHub Actions: No such file or directory
Build Docker image using GitHub Actions: No such file or directory

Time:09-17

We intend to use Git Actions to build our Docker on every commit.

This is our current Git Actions yml:

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  push:
    branches: 
      - '**'
  pull_request:
    branches:
      - '**'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

  docker-build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      
      - name: Navigate to app folder
        run: cd app
      - name: Open Directory
        working-directory: app
        run: |
          ls -la
      - name: Build docker image
        run: docker build . -t app_name -f Dockerfile

The error I get is:

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/runner/work/git-root/app/Dockerfile: no such file or directory

But in my ls -la i see the Dockerfile is present:

total 48
drwxr-xr-x 4 runner docker 4096 Sep 15 13:03 .
drwxr-xr-x 6 runner docker 4096 Sep 15 13:03 ..
-rw-r--r-- 1 runner docker   93 Sep 15 13:03 .env-template
-rw-r--r-- 1 runner docker  655 Sep 15 13:03 Dockerfile

I have tried:

  • Using both actions/checkout@v1 and actions/checkout@v2
  • cd into the directory with the Dockerfile
  • setting Dockerfile directory to working-directory

Why does not the docker build find my Dockerfile?

CodePudding user response:

See the docs here: "Each run keyword represents a new process and shell in the runner environment. When you provide multi-line commands, each line runs in the same shell."

This means that the working directory isn't persisted after the cd step. Your ls step works because you explicitly set the working directory for it.

You have to cd in the same run step as the build command:

      - name: Build docker image
        run: |
          cd app
          docker build . -t app_name -f Dockerfile

Or you can give docker the path to your dockerfile:

      - name: Build docker image
        run: docker build app -t app_name

The default for -f is PATH/Dockerfile, where PATH is app above.

  • Related