railwayapp / nixpacks

App source + Nix packages + Docker = Image
https://nixpacks.com
MIT License
2.33k stars 222 forks source link

Able to set Docker network and custom hosts during build #1057

Open andrasbacsai opened 5 months ago

andrasbacsai commented 5 months ago

Feature request

Simply able to set custom docker network (--network) and add hosts (--add-host) to the nixpacks command, so the build process will have access to the internal resources - and in case of a user defined network - and dns.

Motivation

For example, if you build a NodeJS based project with Prisma(prisma.io), the build process is not able to access the database to generate types for the final image.

Contribution

I can help with more details, or even submit a PR.

JakeCooper commented 4 months ago

Would defs take a PR for this!

Thijmen commented 3 weeks ago

I'll tinker around a little bit, see if I can open up a PR. No Rust experience yet though, going to be a bumpy ride but I'm in for it 😄

Thijmen commented 3 weeks ago

Out of curiosity @JakeCooper; would you like to see this as a configuration option in the toml (the nixpack plan) or a command line argument?

JakeCooper commented 3 weeks ago

Ideally both but start with the command line arg?

Thijmen commented 3 weeks ago

Uh... there are some issues with this unfortunately. It seems that it is not possible with the --network build argument;

https://github.com/moby/buildkit/issues/978 https://github.com/docker/buildx/issues/175

This looks worrying for this feature to be honest. Any ideas here?


Perhaps this could be possible;

  1. docker network create foonet
  2. Create builder
    --name mybuilder \
    --driver docker-container \
    --driver-opt "network=foonet"
  3. docker buildx use mybuilder
  4. docker buildx build -t my_image .

What are your thoughts?

Thijmen commented 3 weeks ago

After tinkering a lot with it, I believe I can make this work. Here's my proposal;

When the --docker-network option is provided (in this case my-network);

  1. We create a new builder instance. docker buildx create --name random-builder-uuid --driver docker-container --driver-opt network=my-network --use
  2. We create an image with this buildx instance and publish it to docker. `docker buildx b -f Dockerfile --load --no-cache

This allows the Dockerfile to fetch endpoints in the network my-network.

This would involve some small changes to the working of nixpack, being;

  1. When the --docker-network option is provided, Nixpacks will create a builder.
  2. It will then use that builder
  3. After building, it will need to reset to the previous builder
  4. ... and ofcouse, remove the builder instance.

This can add some complexity to Nixpacks that I can understand, you folks perhaps don't want in Nixpacks. I myself see this as an added value to Nixpacks, seeing as it might be used for applications which need to fetch some data from database containers in order to build a performant production build.

Admitted; Rust is not my primary language at all, but I feel confident that I am able to draft up a PR that;

  1. Implements the steps above and,
  2. Adds unit tests to validate the behaviour and cleanup of the builder instances.

How do you feel about this, @JakeCooper?

Thijmen commented 3 weeks ago

I've created https://github.com/railwayapp/nixpacks/pull/1119 to allow -add-host during build time.