How do i run a command in a container in github actions?

I’m trying to do something that I thought would be very simple but it seems it is not. I want to run terraform fmt --check on my terraform code using Docker Hub

If I run it locally its fine:

docker run hashicorp/terraform:light fmt --check

However I’m trying to get this to run as a github action. I’ve tried this:

  terraform-fmt-check:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    container:
      image: hashicorp/terraform:light
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: terraform fmt check
        run: fmt --check path/to/terraform/config

which fails with:

OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: “bash”: executable file not found in $PATH: unknown

Also tried this:

  terraform-fmt-check:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    container:
      image: hashicorp/terraform:light
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: terraform fmt check
        run: terraform fmt --check path/to/terraform/config

which fails with the same error:

I know the command I’m trying to run is available in the container because I can run it locally. What’s the correct way to run a command inside a container?

The command that’s failing to run is bash, not terraform. As far as I can see hashicorp/terraform:light is based on Alpine, which doesn’t have Bash installed. If you remove the defaults section from your job that sets the shell to bash, the second version (terraform fmt ...) should work. :slightly_smiling_face:

2 Likes

Perfect. thank you :slight_smile:

1 Like