Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for how to create a static content server image with nginx. #255

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
117 changes: 117 additions & 0 deletions docs/static_content.md
Original file line number Diff line number Diff line change
@@ -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

<!DOCTYPE html>
<html>
<body>

<h1>Our Homepage</h1>

<p>Hello from index.html</p>

</body>
</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.