0

I have long been struggling already with the automatic build of my Dockerfile (located in a subdirectory) using Github Actions.

My directory structure is as follows:

app
├── .github
│   ├── workflows
│   │   └── build.yml
├── encoding
│   ├── encoder.py
│   └── Dockerfile
├── database
│   └── Dockerfile
├── db
    └── <data>
  [...]
├── poetry.lock
├── pyproject.toml
├── LICENSE
└── README.md

My build.yml is as follows (for limitation purposes, only included the build of the encoding step):

name: Build
on:
  push:
    branches: [ main ]
defaults:
  run:
    shell: bash
jobs:
  encoding_build:
    runs-on: self-hosted
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2
    - name: Login to private Docker registry 
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_PASSWORD }}
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.8'
    - name: Build Docker image 
      uses: docker/build-push-action@v2
      with:
        context: ../../../encoding
        dockerfile: ./encoding/Dockerfile
        push: true
        tags: <username>/<project_name>:<tag>
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_PASSWORD }}

And my Dockerfile is the following:

FROM python:3.8

WORKDIR /app/

ADD ./pyproject.toml /tmp/ 
ADD ./poetry.lock /tmp/
RUN cp /tmp/pyproject.toml /tmp/poetry.lock /app/

RUN pip install poetry
RUN poetry config virtualenvs.create false && poetry install --no-root --no-dev -vvv

COPY encoder.py /app/

WORKDIR /app/

CMD ["python", "encoder.py", "--input_file", "/app/input.txt", "--output_file", "/app/output.txt", "--model_name_or_path", "bert-base-uncased"]

I have checked this SO question, and I created my context and dockerfile arguments based on the answers of vivekyad4v and Sal Borrelli there; yet the automated build doesn't seem to work. When pushing any changes to my repository, I am getting the following error:

Error: buildx failed with: ERROR: unable to prepare context: path "../../../encoding" not found

I'd highly appreciate if someone could help me out on how to resolve my error. Furthermore, any additional Docker or Github Actions related comments are welcome!

lazarea
  • 1,129
  • 14
  • 43

1 Answers1

1

Hey your context path is incorrect I believe.

You could try something like this:

with:
  context: ./encoding
  file: ./encoding/Dockerfile

Your current working directory should already be /app.

And the context in your build.yml like for the dockerfile will be resolved using the current working directory.

buddemat
  • 4,552
  • 14
  • 29
  • 49
leone
  • 123
  • 7
  • Thank you for your suggestion! Sadly, I got a different error message after changing the context to `./encoding`: ```Error: buildx failed with: ERROR: failed to solve: failed to read dockerfile: open /tmp/buildkit-mount3478190059/Dockerfile: no such file or directory``` Same issue is present after changing the dockerfile argument from `./encoding/Dockerfile` to `encoding/Dockerfile` – lazarea Feb 27 '23 at 16:58
  • I also tried changing dockerfile argument to `app/encoding/Dockerfile` but same issue as above. – lazarea Feb 27 '23 at 17:04
  • 1
    According to the [doc](https://github.com/docker/build-push-action) It's not `dockerfile` but `file` that have to be used, like this: ```yml with: context: ./encoding file: ./encoding/Dockerfile ``` – leone Feb 27 '23 at 17:17
  • 1
    Also i suggest you should use the latest actions available: ```docker/build-push-action@v4``` – leone Feb 27 '23 at 17:20
  • 1
    Thank you so much for your help. It worked. I accepted your response. :) – lazarea Feb 28 '23 at 12:39