Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build from Dockerfile in subdirectory #169

Closed
digz6666 opened this issue Oct 8, 2020 · 22 comments · Fixed by #244
Closed

Build from Dockerfile in subdirectory #169

digz6666 opened this issue Oct 8, 2020 · 22 comments · Fixed by #244

Comments

@digz6666
Copy link

digz6666 commented Oct 8, 2020

I'd like to build the Dockerfile in following path apis/report-api/Dockerfile and push to amazon-ecr.
But COPY command in Dockerfile fails because it needs to run from the sub directory.
I've tried setting working-directory option but its saying: Unexpected value 'working-directory'.
Maven build is working fine, how can build docker image and push?

Here's files in my project structure:

pom.xml (root pom)
apis/report-api/pom.xml (sub pom)
apis/report-api/Dockerfile
apis/report-api/target/report-api-v1.war (war artifact is created after maven build)

Here's the apis/report-api/Dockerfile

FROM openjdk:8-jre-alpine

RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
RUN mkdir -p /home/spring
WORKDIR /home/spring
COPY target/*.war app.war

EXPOSE 8080
ENTRYPOINT java $JAVA_OPTS -Dfile.encoding=UTF-8 -jar app.war

Here's the action yml file:

jobs:
  path-context:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      - name: Maven clean & install
        run: mvn clean install

      - name: Login to ECR
        uses: docker/login-action@v1
        with:
          registry: xxx.dkr.ecr.ap-southeast-1.amazonaws.com/xxx
          username: ${{ secrets.AWS_ACCESS_KEY_ID }}
          password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

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

      - name: Build and push docker image
        uses: docker/build-push-action@v2
        working-directory: apis/report-api/
        with:
          context: .
          file: ./Dockerfile
          platforms: linux/amd64
          push: true
          tags: |
            repo:${{ github.sha }}
            repo:latest
@crazy-max
Copy link
Member

@digz6666

Unexpected value 'working-directory'.

This is not a valid syntax in a workflow.

You change the context to context: ./apis/report-api

@digz6666
Copy link
Author

digz6666 commented Oct 8, 2020

@digz6666

Unexpected value 'working-directory'.

This is not a valid syntax in a workflow.

You change the context to context: ./apis/report-api

I see, I modified my script according to #166 and its working.
I replaced docker/build-push-action@v2 with aws-actions/amazon-ecr-login@v1.

@digz6666
Copy link
Author

digz6666 commented Oct 8, 2020

@digz6666

Unexpected value 'working-directory'.

This is not a valid syntax in a workflow.

You change the context to context: ./apis/report-api

I'll try changing the context and close the issue soon.

@digz6666
Copy link
Author

digz6666 commented Oct 8, 2020

@digz6666

Unexpected value 'working-directory'.

This is not a valid syntax in a workflow.

You change the context to context: ./apis/report-api

I've changed context to ./apis/report-api but it fails to build, says following error:

Run docker/build-push-action@v2
📣 Buildx version: 0.4.2
🏃 Starting build...
/usr/bin/docker buildx build --tag repo:c4048e47f79445e4b5cb9da62fcb92ad9b491eb6 --tag repo:latest --platform linux/amd64 --iidfile /tmp/docker-build-push-1ubAVg/iidfile --cache-from type=local,src=/tmp/.buildx-cache --cache-to type=local,dest=/tmp/.buildx-cache --file ./Dockerfile --push ./apis/report-api
time="2020-10-08T15:03:04Z" level=warning msg="invalid non-bool value for BUILDX_NO_DEFAULT_LOAD: "
time="2020-10-08T15:03:04Z" level=warning msg="local cache import at /tmp/.buildx-cache not found due to err: could not read /tmp/.buildx-cache/index.json: open /tmp/.buildx-cache/index.json: no such file or directory"
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 2B 0.0s done
#1 DONE 0.0s

#2 [internal] load .dockerignore
#2 transferring context: 2B 0.0s done
#2 DONE 0.0s
failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /tmp/buildkit-mount161412942/Dockerfile: no such file or directory
Error: The process '/usr/bin/docker' failed with exit code 1

@crazy-max
Copy link
Member

crazy-max commented Oct 8, 2020

@digz6666

no such file or directory

So it looks like ./apis/report-api folder does not contain a Dockerfile in your repo.

@digz6666
Copy link
Author

digz6666 commented Oct 8, 2020

@digz6666

no such file or directory

So it looks like ./apis/report-api folder does not contain a Dockerfile in your repo.

It does.

@crazy-max
Copy link
Member

@digz6666 Do you have a link to your repo?

@digz6666
Copy link
Author

digz6666 commented Oct 8, 2020

@digz6666 Do you have a link to your repo?

Its private repo. I'll make a small public repo and tell you later :)

@yabab-dev
Copy link

yabab-dev commented Oct 15, 2020

@digz6666 did you find a solution ?

@digz6666
Copy link
Author

@digz6666 did you find a solution ?

I've replaced this:

      - name: Login to ECR
        uses: docker/login-action@v1
        with:
          registry: xxxxxxxx.dkr.ecr.xxxxxxxx.amazonaws.com/xxxxxxxx-report-api
          username: ${{ secrets.AWS_ACCESS_KEY_ID }}
          password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

      - name: Build and push docker image
        uses: docker/build-push-action@v2
        with:
          context: apis/report-api
          file: ./Dockerfile
          platforms: linux/amd64
          push: true
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache
          tags: |
            repo:${{ github.sha }}
            repo:latest

with following:

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: xxxxxxxx

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1

      - name: Build, tag, and push image to Amazon ECR
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: xxxxxxxx-report-api
          IMAGE_TAG: latest
        working-directory: apis/report-api
        run: |
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

@yabab-dev
Copy link

OK actually, on my side, I had to duplicate context for Dockerfile path:

- name: Build & push image
  uses: docker/build-push-action@v2
    with:
      context: ./build/front
      file: ./build/front/Dockerfile

@digz6666
Copy link
Author

OK actually, on my side, I had to duplicate context for Dockerfile path:

- name: Build & push image
  uses: docker/build-push-action@v2
    with:
      context: ./build/front
      file: ./build/front/Dockerfile

This works for me, thanks!

@jkulak
Copy link

jkulak commented Aug 21, 2022

Seems like I have a similar issue. I am using v3.

https://github.com/jkulak/spotify-grabtrack/actions/runs/2899683281

./app/Dockerfile is a valid file, but for some reason I get buildx failed with: error: unable to prepare context: path "./app" not found.

@jcnils
Copy link

jcnils commented Aug 31, 2022

I have the same problem with v3.1.1

context: .
file: ./docker/Dockerfile
/usr/bin/docker buildx build --file ./docker/Dockerfile --iidfile /tmp/docker-build-push-D7L3OP/iidfile --tag [repo]/[img]:[tag] --metadata-file /tmp/docker-build-push-D7L3OP/metadata-file --push .
ERROR: could not find docker: stat docker: no such file or directory
Error: buildx failed with: ERROR: could not find docker: stat docker: no such file or directory

I tried different paths as well:

  • /docker/Dockerfile
  • docker/Dockerfile
  • repo/docker/Dockerfile

@rbluethl
Copy link

@jcnils did you resolve this? I'm pulling out my hair trying to use a Dockerfile from a subdirectory...

@jkulak
Copy link

jkulak commented Oct 19, 2022

@rbluethl have a look here: https://github.com/jkulak/smartplaylist-backend/blob/main/.github/workflows/build-docker-images.yaml#L14-L36

I had to add

            - name: Checkout
              uses: actions/checkout@v3

and later I could use

            - name: Build and push "listener"
              uses: docker/build-push-action@v3
              with:
                  context: ./app

@rbluethl
Copy link

rbluethl commented Oct 20, 2022

Thanks for your help @jkulak! :)

Interestingly enough, I just tried without the checkout action and it now works even without it. For everyone struggling with this in the future, this is would I ended up with.

name: Build and push prod

on:
  push:
    tags: ['v*']
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Prepare
        id: prep
        run: |
          DOCKER_IMAGE=<image>
          VERSION=main
          SHA=${GITHUB_SHA::7}
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/}
            TAGS="${DOCKER_IMAGE}:main"
          elif [[ $GITHUB_REF == refs/heads/* ]]; then
            VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
          elif [[ $GITHUB_REF == refs/pull/* ]]; then
            VERSION=pr-${{ github.event.number }}
            SHA=$(git rev-parse --short ${{ github.event.pull_request.head.sha || github.sha }})
          fi
          TAGS="$TAGS,${DOCKER_IMAGE}:${VERSION}"
          if [ "${{ github.event_name }}" = "push" ]; then
            TAGS="$TAGS,${DOCKER_IMAGE}:sha-$SHA"
          fi
          echo ::set-output name=version::${VERSION}
          echo ::set-output name=tags::${TAGS}
          echo ::set-output name=sha::${SHA}
          echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and push backend
        uses: docker/build-push-action@v3
        with:
          context: ./backend
          push: true
          tags: ${{ steps.prep.outputs.tags }}
          build-args: VERSION_SUFFIX=${{ steps.prep.outputs.sha }}

@jkulak
Copy link

jkulak commented Oct 20, 2022

Just to be sure, does it work without the - uses: actions/checkout@v3? Because it is still present in the config in your comment above.

@rbluethl
Copy link

The funny thing is that this step had been there all the time, so I can't tell what the problem was before that. :(

@MichaelVoelkel
Copy link

For me it worked also only after adding this actions/checkout@v3 which is confusing because it seemed like normal checkout is anyways also done. But apparently not because the error seems to indicate that docker folder did not exist...

@JohannesBauer97
Copy link

For me it only worked after adding the checkout step at the beginning.

@Danferno
Copy link

Danferno commented May 3, 2024

Anyone running into this with similar issues using docker build rather than docker compose, note that the context does not affect the -f flag. Thus, if you have a app.Dockerfile in /app, you'd need to use docker build -f app/app.Dockerfile app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants