@@ -1081,47 +1081,40 @@ globally on the `command line`_.
1081
1081
.. _per package : https://docs.bazel.build/versions/master/be/functions.html#package.features
1082
1082
.. _command line : https://docs.bazel.build/versions/master/command-line-reference.html#flag--features
1083
1083
1084
- Containerization with rules_docker
1084
+ Containerization with rules_oci
1085
1085
----------------------------------
1086
1086
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
1088
1088
``rules_haskell `` ``haskell_binary `` build targets for deployment. In a nutshell, first we must use
1089
1089
``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
1091
1091
a base image upon which we can layer a Bazel built Haskell binary.
1092
1092
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 ``
1094
1094
file: ::
1095
1095
1096
1096
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",
1101
1101
)
1102
1102
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 ")
1104
1104
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()
1107
1106
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")
1117
1108
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
+ )
1120
1113
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 ::
1122
1115
1123
1116
nixpkgs_package(
1124
- name = "raw- haskell-base-image",
1117
+ name = "haskell-base-image",
1125
1118
repository = "//nixpkgs:default.nix",
1126
1119
# See below for how to define this
1127
1120
nix_file = "//nixpkgs:haskellBaseImageDocker.nix",
@@ -1131,22 +1124,16 @@ Then we're ready to specify a base image built using the ``rules_nixpkgs`` ``nix
1131
1124
""",
1132
1125
)
1133
1126
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
-
1141
1127
Step two requires that we specify our nixpkgs/haskellBaseImageDocker.nix file as follows ::
1142
1128
1143
1129
# nixpkgs is provisioned by rules_nixpkgs for us which we set to be ./default.nix
1144
1130
with import <nixpkgs> { system = "x86_64-linux"; };
1145
1131
1146
1132
# 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
1148
1134
# 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.
1150
1137
let
1151
1138
haskellBase = dockerTools.buildLayeredImage {
1152
1139
name = "haskell-base-image-unwrapped";
@@ -1160,13 +1147,13 @@ Step two requires that we specify our nixpkgs/haskellBaseImageDocker.nix file as
1160
1147
gunzip -c ${haskellBase} > $out/image
1161
1148
''
1162
1149
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 ::
1164
1151
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 ")
1167
1154
1168
1155
haskell_binary(
1169
- name = "my_binary,
1156
+ name = "my_binary" ,
1170
1157
srcs = ["Main.hs"],
1171
1158
ghcopts = [
1172
1159
"-O2",
@@ -1180,28 +1167,31 @@ Step three pulls all this together in a build file to actually assemble our fina
1180
1167
],
1181
1168
)
1182
1169
1183
- cc_image(
1170
+ pkg_tar(
1171
+ name = "my_binary_tar",
1172
+ srcs = [":my_binary"],
1173
+ )
1174
+
1175
+ oci_image(
1184
1176
name = "my_binary_image",
1185
1177
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"],
1190
1181
)
1191
1182
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 ::
1193
1184
1194
- container_push (
1185
+ oci_push (
1195
1186
name = "my_binary_push",
1196
1187
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
+ )
1202
1192
1203
1193
*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.
1205
1195
1206
1196
.. _current inability : https://github.com/NixOS/nixpkgs/issues/16696
1207
1197
0 commit comments