From 333254282c0c4fb602be78c53b728809b60398a6 Mon Sep 17 00:00:00 2001 From: Jason Schein Date: Wed, 31 May 2023 10:44:15 -0700 Subject: [PATCH] Add example for how to create a static content server image with nginx. --- README.md | 1 + docs/static_content.md | 117 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 docs/static_content.md diff --git a/README.md b/README.md index 470dec34..9b41a1f1 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ rules_oci does not contain language-specific rules, but we do have limited docum - [Rust](docs/rust.md) - [Scala](docs/scala.md) - [WASM](https://github.com/bazel-contrib/rules_oci/tree/main/e2e/wasm) (see https://docs.docker.com/desktop/wasm/) +- [Static Content](docs/static_content.md) (such as a html/javascript frontend) > Your language not listed above? Please contribute engineering resources or financially through our Sponsor link! diff --git a/docs/static_content.md b/docs/static_content.md new file mode 100644 index 00000000..675ae537 --- /dev/null +++ b/docs/static_content.md @@ -0,0 +1,117 @@ +# Serving Static Content + +This is useful for creating an image to serve static content, such as the output of building your +frontend javascript. + +In this example we'll use the [docker nginx image](https://hub.docker.com/_/nginx), but you could +use any other static content webserver the same way. + +## Example + +Pull our base image. + +**./WORKSPACE** + +```python +load("@rules_oci//oci:pull.bzl", "oci_pull") +oci_pull( + name = "nginx_debian_slim", + digest = "sha256:6b06964cdbbc517102ce5e0cef95152f3c6a7ef703e4057cb574539de91f72e6", + image = "docker.io/library/nginx", +) +``` + +Next lets create our static content files. + +**./frontend/index.html** + +```html + + + + + +

Our Homepage

+ +

Hello from index.html

+ + + +``` + +**./frontend/textfile.txt** + +```txt +This is text file. +``` + +And finally the build rules for our image. + +**./frontend/BUILD** + +```python +load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball") +load("@rules_pkg//:pkg.bzl", "pkg_tar") +filegroup( + name = "static", + srcs = ["index.html", "textfile.txt"], +) + +pkg_tar( + name = "static_tar", + srcs = [":static"], + package_dir = "/usr/share/nginx/html" +) + +oci_image( + name = "frontend_image", + base = "@nginx_debian_slim", + tars = [ + ":static_tar", + ], + # Intentionally omit cmd/entrypoint to default to the base nginx container's cmd/entrypoint. + # entrypoint = [], + # cmd = [], +) +oci_tarball( + name = "frontend_tarball", + image = ":frontend_image", + repo_tags = ["ourfrontend:latest"], +) + + +``` + +If you want to customize the nginx.conf you could create `./frontend/nginx.conf` and add this to +`./frontend/BUILD`. + +```python + +pkg_tar( + name = "nginx_conf_tar", + srcs = [":nginx.conf"], + package_dir = "/etc/nginx", +) + +# ... +oci_image( + #... + tars = [ + ":static_tar", + ":nginx_conf_tar + ], + # ... +) + +``` + +## Try running the container with docker + +```bash +# File created by running bazel build //frontend:frontend_tarball +tarball_file="" +docker load --input "$tarball_file" +docker run --rm -p 8080:80 "ourfrontend:latest" +``` + +Wait for nginx to start in your container, and then go to `localhost:8080` and `localhost:8080/example.txt` to see your static content.