Skip to content

Commit bf2e6cd

Browse files
authored
Merge pull request #2137 from iteratee/kb/rules_oci_in_docs
Update example in haskell-use-cases.rst to rules_oci
2 parents 8a59b40 + 0aab405 commit bf2e6cd

File tree

1 file changed

+40
-50
lines changed

1 file changed

+40
-50
lines changed

docs/haskell-use-cases.rst

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,47 +1081,40 @@ globally on the `command line`_.
10811081
.. _per package: https://docs.bazel.build/versions/master/be/functions.html#package.features
10821082
.. _command line: https://docs.bazel.build/versions/master/command-line-reference.html#flag--features
10831083

1084-
Containerization with rules_docker
1084+
Containerization with rules_oci
10851085
----------------------------------
10861086

1087-
Making use of both ``rules_docker`` and ``rules_nixpkgs``, it's possible to containerize
1087+
Making use of both ``rules_oci`` and ``rules_nixpkgs``, it's possible to containerize
10881088
``rules_haskell`` ``haskell_binary`` build targets for deployment. In a nutshell, first we must use
10891089
``rules_nixpkgs`` to build a ``dockerTools.buildLayeredImage`` target with the basic library dependencies
1090-
required to run a typical Haskell binary. Thereafter, we can use ``rules_docker`` to use this as
1090+
required to run a typical Haskell binary. Thereafter, we can use ``rules_oci`` to use this as
10911091
a base image upon which we can layer a Bazel built Haskell binary.
10921092

1093-
Step one is to ensure you have all the necessary ``rules_docker`` paraphernalia loaded in your ``WORKSPACE``
1093+
Step one is to ensure you have all the necessary ``rules_oci`` paraphernalia loaded in your ``WORKSPACE``
10941094
file: ::
10951095

10961096
http_archive(
1097-
name = "io_bazel_rules_docker",
1098-
sha256 = "df13123c44b4a4ff2c2f337b906763879d94871d16411bf82dcfeba892b58607",
1099-
strip_prefix = "rules_docker-0.13.0",
1100-
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.13.0/rules_docker-v0.13.0.tar.gz"],
1097+
name = "rules_oci",
1098+
sha256 = "4a276e9566c03491649eef63f27c2816cc222f41ccdebd97d2c5159e84917c3b",
1099+
strip_prefix = "rules_oci-1.7.4",
1100+
url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.7.4/rules_oci-v1.7.4.tar.gz",
11011101
)
11021102

1103-
load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl", docker_toolchain_configure="toolchain_configure")
1103+
load("@rules_oci//oci:dependencies.bzl", "rules_oci_dependencies")
11041104

1105-
To make full use of post-build ``rules_docker`` functionality, we'll want to make sure this is set
1106-
to the Docker binary's location ::
1105+
rules_oci_dependencies()
11071106

1108-
docker_toolchain_configure(
1109-
name = "docker_config",
1110-
docker_path = "/usr/bin/docker"
1111-
)
1112-
1113-
load("@io_bazel_rules_docker//container:container.bzl", "container_load")
1114-
1115-
load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories")
1116-
container_repositories()
1107+
load("@rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "oci_register_toolchains")
11171108

1118-
load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")
1119-
container_deps()
1109+
oci_register_toolchains(
1110+
name = "oci",
1111+
crane_version = LATEST_CRANE_VERSION,
1112+
)
11201113

1121-
Then we're ready to specify a base image built using the ``rules_nixpkgs`` ``nixpkgs_package`` rule for ``rules_docker`` to layer its products on top of ::
1114+
Then we're ready to specify a base image built using the ``rules_nixpkgs`` ``nixpkgs_package`` rule for ``rules_oci`` to layer its products on top of ::
11221115

11231116
nixpkgs_package(
1124-
name = "raw-haskell-base-image",
1117+
name = "haskell-base-image",
11251118
repository = "//nixpkgs:default.nix",
11261119
# See below for how to define this
11271120
nix_file = "//nixpkgs:haskellBaseImageDocker.nix",
@@ -1131,22 +1124,16 @@ Then we're ready to specify a base image built using the ``rules_nixpkgs`` ``nix
11311124
""",
11321125
)
11331126

1134-
And finally use the ``rules_docker`` ``container_load`` functionality to grab the Docker image built by the previous ``raw-haskell-base-image`` target ::
1135-
1136-
container_load(
1137-
name = "haskell-base-image",
1138-
file = "@raw-haskell-base-image//:image",
1139-
)
1140-
11411127
Step two requires that we specify our nixpkgs/haskellBaseImageDocker.nix file as follows ::
11421128

11431129
# nixpkgs is provisioned by rules_nixpkgs for us which we set to be ./default.nix
11441130
with import <nixpkgs> { system = "x86_64-linux"; };
11451131

11461132
# Build the base image.
1147-
# The output of this derivation will be a Docker archive in the same format as
1133+
# The output of this derivation will be a Docker format archive in the same format as
11481134
# the output of `docker save` that we can feed to
1149-
# [container_load](https://github.com/bazelbuild/rules_docker#container_load)
1135+
# [oci_image](https://github.com/bazel-contrib/rules_oci/blob/main/docs/image.md#oci_image)
1136+
# as a base image.
11501137
let
11511138
haskellBase = dockerTools.buildLayeredImage {
11521139
name = "haskell-base-image-unwrapped";
@@ -1160,13 +1147,13 @@ Step two requires that we specify our nixpkgs/haskellBaseImageDocker.nix file as
11601147
gunzip -c ${haskellBase} > $out/image
11611148
''
11621149

1163-
Step three pulls all this together in a build file to actually assemble our final Docker image. In a BUILD.bazel file, we'll need the following ::
1150+
Step three pulls all this together in a build file to actually assemble our final container image. In a BUILD.bazel file, we'll need the following ::
11641151

1165-
load("@io_bazel_rules_docker//cc:image.bzl", "cc_image")
1166-
load("@io_bazel_rules_docker//container:container.bzl", "container_push")
1152+
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_push")
1153+
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
11671154

11681155
haskell_binary(
1169-
name = "my_binary,
1156+
name = "my_binary",
11701157
srcs = ["Main.hs"],
11711158
ghcopts = [
11721159
"-O2",
@@ -1180,28 +1167,31 @@ Step three pulls all this together in a build file to actually assemble our fina
11801167
],
11811168
)
11821169

1183-
cc_image(
1170+
pkg_tar(
1171+
name = "my_binary_tar",
1172+
srcs = [":my_binary"],
1173+
)
1174+
1175+
oci_image(
11841176
name = "my_binary_image",
11851177
base = "@haskell-base-image//image",
1186-
binary = ":my_binary",
1187-
ports = [ "8000/tcp" ],
1188-
creation_time = "{BUILD_TIMESTAMP}",
1189-
stamp = True,
1178+
tars = [":pkg_tar"],
1179+
exposed_ports = [ "8000/tcp" ],
1180+
entrypoint = ["/my_binary"],
11901181
)
11911182

1192-
And you may want to use ``rules_docker`` to push your Docker image as follows ::
1183+
And you may want to use ``rules_oci`` to push your container image as follows ::
11931184

1194-
container_push(
1185+
oci_push(
11951186
name = "my_binary_push",
11961187
image = ":my_binary_image",
1197-
format = "Docker",
1198-
registry = "gcr.io", # For example using a GCP GCR repository
1199-
repository = "$project-name-here/$my_binary_image_label",
1200-
tag = "{BUILD_USER}",
1201-
)
1188+
# For example using a GCP GCR repository
1189+
repository = "gcr.io/$project-name-here/$my_binary_image_label",
1190+
remote_tags = ["{BUILD_USER}"],
1191+
)
12021192

12031193
*n.b.* Due to the `current inability`_ of Nix to be used on macOS (darwin) for building Docker images, it's currently
1204-
not possible to build Docker images for Haskell binaries as above using ``rules_docker`` and Nixpkgs on macOS.
1194+
not possible to build Docker images for Haskell binaries as above using ``rules_oci`` and Nixpkgs on macOS.
12051195

12061196
.. _current inability: https://github.com/NixOS/nixpkgs/issues/16696
12071197

0 commit comments

Comments
 (0)