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 of building a Dockerfile with local tar file build context #702

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

njlr
Copy link

@njlr njlr commented Sep 21, 2024

This PR adds an example of using a Dockerfile with a local tar as a build context.

This is not the ideal approach, which this is clearly signposted in the README!, but it shows an easy migration path from rules_docker. The existing example does not show how Bazel build artefacts can be passed to BuildX.

How it works:

  1. Create a tar file using rules_pkg
  2. Pipe the tar file into BuildX using sh_binary and run_binary
  3. Use the BuildX output as a base for an oci_image

This is a replacement for container_run_and_commit.

The example could easily be adapted to behave like container_run_and_extract by changing the --output flag to a tar.

@thesayyn
Copy link
Collaborator

We have https://github.com/bazel-contrib/rules_oci/tree/main/examples/dockerfile, how is this different than that one?

@njlr
Copy link
Author

njlr commented Sep 21, 2024

We have https://github.com/bazel-contrib/rules_oci/tree/main/examples/dockerfile, how is this different than that one?

That example does not show how to use the output of other Bazel targets as input to the Dockerfile build

@thesayyn
Copy link
Collaborator

That example does not show how to use the output of other Bazel targets as input to the Dockerfile build

Than can we change it so it does? i don't see the difference between this example and the other one, other than putting the srcs into a tar file beforehand so you don't have to deal with paths that start with bazel-bin/cfg/bin

@njlr
Copy link
Author

njlr commented Sep 21, 2024

That example does not show how to use the output of other Bazel targets as input to the Dockerfile build

Than can we change it so it does? i don't see the difference between this example and the other one, other than putting the srcs into a tar file beforehand so you don't have to deal with paths that start with bazel-bin/cfg/bin

Yes, happy to do this.

The reason I made a new example is that it's a little more complicated than the existing example, so might hinder understanding.

What do you think?

@thesayyn
Copy link
Collaborator

We can add some comments explaining why there is two different buildx targets in the same package and the difference.

@njlr
Copy link
Author

njlr commented Oct 16, 2024

We can add some comments explaining why there is two different buildx targets in the same package and the difference.

I have merged the two examples

Copy link
Collaborator

@thesayyn thesayyn left a comment

Choose a reason for hiding this comment

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

thanks!

# This tar can be arranged in arbitrary ways.
# For example, one could grab pkg_files from elsewhere in the workspace.
# Anything in the tar can be used by a Dockerfile ADD or COPY.
pkg_tar(
Copy link
Collaborator

Choose a reason for hiding this comment

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

actually can we not use pkg_tar here? we have bazel-lib dependency here already. you can use https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md

(i don't want a pkg_tar dependency even if it's dev_dep)

Copy link
Author

Choose a reason for hiding this comment

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

I might need some assistance with this one.

When I try bazel build //examples/dockerfile:buildx_context I get:

tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
Target //examples/dockerfile:buildx_context failed to build

Any ideas?

@thesayyn
Copy link
Collaborator

There are some failures, can you take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants