Search code examples
dockergithubdockerfilegithub-actions

Using automated build with Github Actions and Dockerfile


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!


Solution

  • 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.