docker_image: add custom docker_image macro building and pushing docker images
diff --git a/apps/face_detection/BUILD b/apps/face_detection/BUILD
index d1ed253..c966548 100644
--- a/apps/face_detection/BUILD
+++ b/apps/face_detection/BUILD
@@ -1,22 +1,14 @@
-load("@rules_pkg//:pkg.bzl", "pkg_tar")
+load("//:bazel_tools/docker.bzl", "docker_image")
# TODO(lekva): figure out how to build py_binary with pip dependencies and
-# migrate off of shell script running docker build
-sh_binary(
+# migrate off docker_image rule
+docker_image(
name = "push_to_dev",
- srcs = ["push_to_dev.sh"],
- data = glob(["*.py"]) + [
- "Dockerfile",
+ registry = "localhost:30500",
+ image = "giolekva/face-detector",
+ tag = "latest",
+ dockerfile = "Dockerfile",
+ srcs = glob(["*.py"]) + [
"haarcascade_frontalface_default.xml",
],
- deps = [
- "@bazel_tools//tools/bash/runfiles",
- ],
-)
-
-pkg_tar(
- name = "chart",
- srcs = glob(["chart/**"]),
- extension = "tar.gz",
- strip_prefix = "./chart",
)
diff --git a/apps/face_detection/Dockerfile b/apps/face_detection/Dockerfile
index befcba5..8c0996c 100644
--- a/apps/face_detection/Dockerfile
+++ b/apps/face_detection/Dockerfile
@@ -15,4 +15,5 @@
python3-opencv
WORKDIR /app
-COPY . /app
\ No newline at end of file
+COPY *.py /app/
+COPY *.xml /app/
diff --git a/bazel_tools/docker.bzl b/bazel_tools/docker.bzl
new file mode 100644
index 0000000..1149497
--- /dev/null
+++ b/bazel_tools/docker.bzl
@@ -0,0 +1,55 @@
+__PUSH_TO_DEV_TMPL = """
+#!/bin/sh
+set -eE -o functrace
+# --- begin runfiles.bash initialization ---
+# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
+set -euo pipefail
+if [[ ! -d "\$${{RUNFILES_DIR:-/dev/null}}" && ! -f "\$${{RUNFILES_MANIFEST_FILE:-/dev/null}}" ]]; then
+ if [[ -f "\$$0.runfiles_manifest" ]]; then
+ export RUNFILES_MANIFEST_FILE="\$$0.runfiles_manifest"
+ elif [[ -f "\$$0.runfiles/MANIFEST" ]]; then
+ export RUNFILES_MANIFEST_FILE="\$$0.runfiles/MANIFEST"
+ elif [[ -f "\$$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+ export RUNFILES_DIR="\$$0.runfiles"
+ fi
+fi
+if [[ -f "\$${{RUNFILES_DIR:-/dev/null}}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+ source "\$${{RUNFILES_DIR}}/bazel_tools/tools/bash/runfiles/runfiles.bash"
+elif [[ -f "\$${{RUNFILES_MANIFEST_FILE:-/dev/null}}" ]]; then
+ source "\$$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
+ "\$$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
+else
+ echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
+ exit 1
+fi
+# --- end runfiles.bash initialization ---
+IMAGE="{registry}/{image}:{tag}"
+DOCKERFILE=\$$(rlocation __main__/{package}/{dockerfile})
+docker build \
+ --tag=\$$IMAGE \
+ --file=\$$DOCKERFILE \
+ \$$(dirname "\$$DOCKERFILE")
+docker push \$$IMAGE
+"""
+
+
+def docker_image(name, registry, image, tag, dockerfile, srcs, **kwargs):
+ native.genrule(
+ name = "%s.sh" % name,
+ executable = False,
+ outs = ["build_and_push.sh",],
+ cmd = """cat > $@ <<EOM
+%s
+EOM
+""" % __PUSH_TO_DEV_TMPL.format(
+ registry = registry,
+ image = image,
+ tag = tag,
+ dockerfile = dockerfile,
+ package = native.package_name(),
+ ))
+ native.sh_binary(
+ name = name,
+ srcs = ["build_and_push.sh",],
+ data = srcs + [dockerfile,],
+ deps = ["@bazel_tools//tools/bash/runfiles",])