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

Recipe using bake to build with custom arguments #2141

Merged
merged 5 commits into from
Sep 2, 2024
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
45 changes: 45 additions & 0 deletions docs/using/recipe_code/docker-bake.python312.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
group "default" {
targets = ["custom-notebook"]
}

target "foundation" {
context = "https://github.com/jupyter/docker-stacks.git#main:images/docker-stacks-foundation"
args = {
PYTHON_VERSION = "3.12"
}
tags = ["docker-stacks-foundation"]
}


target "base-notebook" {
context = "https://github.com/jupyter/docker-stacks.git#main:images/base-notebook"
contexts = {
docker-stacks-foundation = "target:foundation"
}
args = {
BASE_CONTAINER = "docker-stacks-foundation"
}
tags = ["base-notebook"]
}

target "minimal-notebook" {
context = "https://github.com/jupyter/docker-stacks.git#main:images/minimal-notebook"
contexts = {
base-notebook = "target:base-notebook"
}
args = {
BASE_CONTAINER = "base-notebook"
}
tags = ["minimal-notebook"]
}

target "custom-notebook" {
context = "."
contexts = {
minimal-notebook = "target:minimal-notebook"
}
args = {
BASE_CONTAINER = "minimal-notebook"
}
tags = ["custom-jupyter"]
}
54 changes: 54 additions & 0 deletions docs/using/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,57 @@ they may be explained in the "Installation instructions" section of the Download
```{literalinclude} recipe_code/oracledb.dockerfile
:language: docker
```

## Building stack images with custom arguments

A selection of prebuilt images are available from Quay.io,
however, it's impossible to cater to everybody's needs.
For extensive customization with an automated build pipeline,
you may wish to create a [community-maintained stack](https://jupyter-docker-stacks.readthedocs.io/en/latest/contributing/stacks.html),
however, for minor customizations, this may be overkill.
For example, you may wish to use the same jupyter stacks but built on a different base image,
or build with a different Python version.

To achieve this you can use [Docker Bake](https://docs.docker.com/build/bake/)
to build the stacks locally with custom arguments.

```{note}
Custom arguments may result in build errors due to incompatibility.
If so your use-case may require a fully customized stack.
```

As a basic example, if you want to build a custom image based on the `minimal-notebook` image using `Python 3.12`,
then with a Dockerfile like:

```{code-block} Dockerfile
:caption: Dockerfile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks cool!


ARG BASE_CONTAINER=minimal-notebook
FROM $BASE_CONTAINER
...
```

Include the below file in your project:

```{literalinclude} recipe_code/docker-bake.python312.hcl
:force:
:language: hcl
:caption: docker-bake.hcl
```

To build this stack, in the same directory run:

```bash
docker buildx bake
```

Docker Bake then determines the correct build order from the `contexts` parameters
and builds the stack as requested.

This image can then be run using:

```bash
docker run custom-jupyter
```

or referenced in a docker compose file.