Skip to content

Adding the ability to build a minimal docker container using bazel - #15312

Closed
omriz wants to merge 1 commit into
envoyproxy:mainfrom
omriz:bazel_docker
Closed

omriz wants to merge 1 commit into
envoyproxy:mainfrom
omriz:bazel_docker

Conversation

@omriz

@omriz omriz commented Mar 4, 2021

Copy link
Copy Markdown
Contributor

Commit Message: Adding the ability to build a minimal docker container using bazel
Additional Description:
This is an initial example of using rules_docker to build envoy container image. This has certain benefits over the current do_ci.sh:

  1. Declarative build instead of scripts
  2. Reproducible docker containers
  3. Leveraging bazel build flags
  4. Minimal and secure container with nonroot user based on distroless

It is also possible to add container_push rule to add pushing the container as part of the bazel targets

Risk Level: Low
Testing: Container built and run
Docs Changes: Pending
Release Notes:
Platform Specific Features:

@omriz

omriz commented Mar 4, 2021

Copy link
Copy Markdown
Contributor Author

@htuch @yanavlasov - FYI

@phlax

phlax commented Mar 4, 2021

Copy link
Copy Markdown
Member

@omriz i like the change - but can it build x-arch ?

atm we use qemu and buildx to build mutl-arch containers

@phlax

phlax commented Mar 4, 2021

Copy link
Copy Markdown
Member

cc @lizan

@alyssawilk

Copy link
Copy Markdown
Contributor

Looks like this needs format checks but due to ci breakage yesterday it probably needs a main merge too, sorry for the trouble!

@omriz

omriz commented Mar 4, 2021

Copy link
Copy Markdown
Contributor Author

Rebased on current master.
@phlax - Regarding multi-arch. So right now it seems the answer is no :-(
bazelbuild/rules_docker#1599
bazelbuild/rules_docker#300

From a quick look in the code it seems there is support for arm64 and such - the same way bazel supports it. But I think in order to generate multi arch images you'll have to execute bazel in your target arch.

Will that be a reasonable approach?
Otherwise we'll have to maintain the old way of building, perhaps alongside this method, or try and work with the bazel folk to add support for that.

@phlax

phlax commented Mar 4, 2021

Copy link
Copy Markdown
Member

Otherwise we'll have to maintain the old way of building, perhaps alongside this method, or try and work with the bazel folk to add support for that.

you will have to check with @lizan - but my guess/opinion is that we wouldnt want to change from buildx without the multiiarch support - adding actual builds for each arch would come at a cost, if it was possible (not sure)

im wondering how difficult it would be to add buildx support upstream

@htuch

htuch commented Mar 5, 2021

Copy link
Copy Markdown
Member

This is super cool, thanks @omriz! I hope the multiarch stuff doesn't block. @phlax any use of this in the examples/?

@phlax

phlax commented Mar 5, 2021

Copy link
Copy Markdown
Member

any use of this in the examples/?

i think probably not on the basis that we dont want to force end users to install bazel when docker/compose will suffice

@htuch

htuch commented Mar 5, 2021 •

Copy link
Copy Markdown
Member

@phlax ack. The main win of Bazel is caching when doing things at scale; you might want to think about it as examples grow and overall build time increases. It also makes things more repeatable, Docker local state might cause some pain, but we can see if there are any real issues in practice.

@omriz

omriz commented Mar 8, 2021

Copy link
Copy Markdown
Contributor Author

I did some looking into the rules_docker. It doesn't actually run 'docker' in the normal sense but rather let's bazel build the layers one by one. That's why the build is reproducible.
Now I think that if we specify a cross-build toolchain we can build an image for arm/mac/whatnot on a linux host but I'll have to double check it.
In any case - would you like me to put it in experimental or as an option? Or would you rather abandon this?

@phlax

phlax commented Mar 8, 2021

Copy link
Copy Markdown
Member

it would defo be good to hear @lizan 's take but imho...

i think there are kinda 2 separate changes here

Minimal and secure container with nonroot user based on distroless

for production images this seems like a good idea - im aware tetrates getenvoy images are already built from this base.

The slight problem is that for users who already depend upon the published images it would be a significant change, and may break their current environment/expectations

which then opens the question of whether we would want to add another os ie ubuntu/alpine + distroless, or whether we drop one or both of the existing os images. This question is multiplied to dev and dbg versions as well as architectures.

using rules_docker to build envoy container image.

the other change is essentially in terms of using rules_docker

i like the idea mostly because as bazel is used extensively already its helpful to have the docker declarations in bazel

not sure about the benefits in terms of caching/layers/reproducibility.

these are all broadly features of docker itself also, and besides its also not clear to me whether bazel is still passing context etc to the docker daemon to run the actual build (my guess is yes, and that its just acting as a client), in which case i think its not really that much different in terms of where things are cached/stored

@htuch

htuch commented Mar 8, 2021

Copy link
Copy Markdown
Member

I think the key thing I'd like to understand is whether Bazel is handling this hermetically. Usually, Bazel does not take local system state dependencies, which is very powerful when doing things like RBE or trying to ensure that errors in build aren't due to a local snafu. If rules_docker manages hermeticity in the Bazel style, this is a clear win for reproducability of build. But, I don't really know the details on rules_docker, it would just be surprising if it broke with the rest of the Bazel ecosystem here.

@phlax

phlax commented Mar 8, 2021

Copy link
Copy Markdown
Member

Usually, Bazel does not take local system state dependencies

iiuc neither does docker

when you build you send a hashed context to the build api

if anything changes - ie hash changes - you rebuild - if it doesnt you dont

whatever else bazel does im pretty confident that it doenst reimplement docker (a la podman) it just acts as a client to the docker api

@htuch

htuch commented Mar 8, 2021

Copy link
Copy Markdown
Member

I see, makes sense. This then leaves the benefit of consistency and moving away from crufty shell scripts.

@phlax

phlax commented Mar 8, 2021

Copy link
Copy Markdown
Member

This then leaves the benefit of consistency and moving away from crufty shell scripts.

yep - defo a worthy goal still 8)

@github-actions

github-actions Bot commented Apr 7, 2021

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot added the stale stalebot believes this issue/PR has not been touched recently label Apr 7, 2021
@lizan

lizan commented Apr 7, 2021

Copy link
Copy Markdown
Member

I would love to have rules_docker to build the images, so move away from shell scripts. Though it won't be straight forward to do multi-arch since we're not doing cross compile in a single bazel build.

@github-actions github-actions Bot removed the stale stalebot believes this issue/PR has not been touched recently label Apr 7, 2021
@htuch

htuch commented Apr 8, 2021

Copy link
Copy Markdown
Member

@lizan can you advise on how to make the next steps on the PR?

@lizan

lizan commented Apr 9, 2021

Copy link
Copy Markdown
Member

Let's make this PR into a shape, i.e. we can build x64 image on x64 and arm64 on arm64 CI. Then we still need a shell script to make it multi-arch until rules_docker support them.

@github-actions

github-actions Bot commented May 9, 2021

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot added the stale stalebot believes this issue/PR has not been touched recently label May 9, 2021
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because it has not had activity in the last 37 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot closed this May 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale stalebot believes this issue/PR has not been touched recently

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants