runner icon indicating copy to clipboard operation
runner copied to clipboard

Docker container action does not execute from the repository root

Open discentem opened this issue 2 years ago • 6 comments

Describe the bug

I have a dockerfile defined in dockerfiles/compile/Dockerfile.

Github Actions executes Docker from the directory where the Dockerfile is stored:

/usr/bin/docker build -t 4cd98f:f7c0bfc3fa7c515f8b9a964cbb495763 -f "/home/runner/work/hello-world-docker-action/hello-world-docker-action/./.github/workflows/actions/compile/../../../../dockerfiles/compile/Dockerfile" "/home/runner/work/hello-world-docker-action/hello-world-docker-action/dockerfiles/compile"

This causes my go build instruction in my Dockerfile to fail in Github Actions because my source files (namely go.mod) are not found in dockerfiles/compile/Dockerfile.

See this gist for the full log: https://gist.github.com/discentem/4deff7191e527f0210e79fedfaca4026

To Reproduce

Fork https://github.com/discentem/hello-world-docker-action and push this commit to a new branch to trigger CI.

Expected behavior

I would expect Docker to be executed from the root of my repository and not specifically from the directory where the Dockerfile is sourced from. Something like this:

/usr/bin/docker build -t 4cd98f:f7c0bfc3fa7c515f8b9a964cbb495763 -f "/home/runner/work/hello-world-docker-action/hello-world-docker-action/./.github/workflows/actions/compile/../../../../dockerfiles/compile/Dockerfile" "."

Or perhaps there should be a param to tell the runner what directory to run docker in

Workaround

If I copy my Dockerfile to the root of my repository on the fly and point the action to this copy, the action succeeds. This is because the Dockerfile is now being executed in a directory where go.mod and the go source files can be found.

Runner Version and Platform

Hosted, Ubuntu 20.04

OS of the machine running the runner? Linux

What's not working?

Please include error messages and screenshots.

Job Log Output

Failed job output

Runner and Worker's Diagnostic Logs

N/A

discentem avatar Jul 23 '22 02:07 discentem

Hi @discentem,

Thank you for submitting this issue. I will mark this as an enhancement, since the issue here is that you don't have a way to control the context passed to the docker build for example.

nikola-jokic avatar Aug 01 '22 10:08 nikola-jokic

Hey @nikola-jokic, thanks for the reply!

I am curious though, what is the expected way to use this feature? Is the workaround I settled on common?

discentem avatar Aug 01 '22 17:08 discentem

Hi again @nikola-jokic, any updates on providing a param for context?

I'm also wondering whether there are alternative strategies that I should consider. Is there an env variable or symlink I could utilize to access the root of the Github repo from whatever arbitrary directory I'm in?

discentem avatar May 31 '23 00:05 discentem

Hey @discentem,

I can't think of one to leverage docker like this, without changing the runner and possibly the workflow.yaml definition. One idea might be to get around this issue might be to have a Makefile. It is common for go projects to have makefiles, so you can leverage that. Other than that, until we change the workings of the docker actions in the runner, the only thing I can think of is to use runner container hooks.

nikola-jokic avatar May 31 '23 08:05 nikola-jokic

Hi again, this documentation actually implies the working directory should already be set to $GITHUB_WORKSPACE (the root of the code repository): https://docs.github.com/en/actions/creating-actions/dockerfile-support-for-github-actions#workdir

In my experience that was not the case. However, should I simply be able to do cd $GITHUB_WORKSPACE to solve my issue (get back to the root of my project code) instead of copying the Dockerfile around?

discentem avatar Mar 04 '24 16:03 discentem

Hello, I'm also having this issue.

AndresPinerosZen avatar Apr 20 '24 09:04 AndresPinerosZen