diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..204445d --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,42 @@ +name: Docker Image CI + +on: + push: + branches: [ "master" ] + workflow_dispatch: # run manually + +jobs: + + build: + + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + + steps: + - name: Replace underscores in tag name + run: NAME="$GITHUB.REPOSITORY"; echo DOCKER_TAG=${NAME//_//} >> $GITHUB_ENV + - uses: actions/checkout@v3 + - # Activate cache export feature to reduce build time of image + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Build the Docker image + run: make docker DOCKER_TAG=${{ env.DOCKER_TAG }} + - name: Login to Dockerhub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + - name: Push image to Dockerhub + run: docker push ${{ env.DOCKER_TAG }} + - name: Alias the Docker image for GHCR + run: docker tag ${{ env.DOCKER_TAG }} ghcr.io/${{ env.DOCKER_TAG }} + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Push image to Github Container Registry + run: docker push ghcr.io/${{ env.DOCKER_TAG }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f64d276 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +FROM ocrd/core-cuda +ARG VCS_REF +ARG BUILD_DATE +LABEL \ + maintainer="https://ocr-d.de/kontakt" \ + org.label-schema.vcs-ref=$VCS_REF \ + org.label-schema.vcs-url="https://github.com/bertsky/detectron2" \ + org.label-schema.build-date=$BUILD_DATE + +ENV DEBIAN_FRONTEND noninteractive +ENV PYTHONIOENCODING utf8 + +# avoid HOME/.local/share (hard to predict USER here) +# so let XDG_DATA_HOME coincide with fixed system location +# (can still be overridden by derived stages or at runtime) +# should be combined with a bind-mount at runtime +ENV XDG_DATA_HOME /usr/local/share + +WORKDIR /build-ocrd +COPY setup.py . +COPY ocrd_detectron2/ocrd-tool.json . +COPY README.md . +COPY requirements.txt . +COPY requirements-test.txt . +COPY ocrd_detectron2 ./ocrd_detectron2 +COPY Makefile . +RUN apt-get install -y --no-install-recommends g++ && \ + make deps && \ + make install && \ + rm -rf /build-ocrd && \ + apt-get -y remove --auto-remove g++ + +WORKDIR /data +VOLUME /data + +# override Nvidia entrypoint +ENTRYPOINT [] diff --git a/Makefile b/Makefile index 4672b4d..113ce13 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ PIP = pip3 PYTHONIOENCODING=utf8 SHELL = /bin/bash +# Docker container tag +DOCKER_TAG = 'bertsky/detectron2' + help: @echo @echo " Targets" @@ -12,10 +15,12 @@ help: @echo " deps-test Install Python dependencies for tests via pip and models via resmgr" @echo " test Run regression tests" @echo " clean Remove symlinks in test/assets" + @echo " docker Build Docker image" @echo @echo " Variables" @echo " PYTHON" @echo " CUDA_VERSION override detection of CUDA runtime version (e.g. '11.3' or 'CPU')" + @echo " DOCKER_TAG Docker image tag of result for the docker target" # Install Python deps via pip # There is no prebuilt for detectron2 on PyPI, and the wheels depend on CUDA and Torch version. @@ -81,6 +86,13 @@ test/assets: repo/assets clean: -$(RM) -r test/assets +# Build docker image +docker: + docker build \ + --build-arg VCS_REF=$$(git rev-parse --short HEAD) \ + --build-arg BUILD_DATE=$$(date -u +"%Y-%m-%dT%H:%M:%SZ") \ + -t $(DOCKER_TAG) . + #MODELDIR := $(or $(XDG_DATA_HOME),$(HOME)/.local/share)/ocrd-resources/ocrd-detectron2-segment TESTMODEL := TableBank_X152_Psarpei @@ -124,4 +136,4 @@ count-regions := python -c "import sys; from ocrd_models.ocrd_page import parse; # make cannot delete directories, so keep them .PRECIOUS .SECONDARY: %/OCR-D-BIN %/OCR-D-SEG-$(MODEL) -.PHONY: help deps install deps-test models-test test clean +.PHONY: help deps install deps-test models-test test clean docker