Skip to content

feat(config): Add optional Docker extraHosts (part 1)#832

Merged
peterpeterparker merged 3 commits intojunobuild:mainfrom
MAHDTech:feat/docker-hosts
Mar 16, 2026
Merged

feat(config): Add optional Docker extraHosts (part 1)#832
peterpeterparker merged 3 commits intojunobuild:mainfrom
MAHDTech:feat/docker-hosts

Conversation

@MAHDTech
Copy link
Contributor

@MAHDTech MAHDTech commented Mar 16, 2026

Overview

This is part 1/2.

This PR adds an additional configuration item to the Docker options called extraHosts

Further detail about it's intended use case is documented in part 2.

Related

Part 2

@peterpeterparker
Copy link
Contributor

There is likely a related test suite, if you could add a snippet to it that would be great as well.

@MAHDTech
Copy link
Contributor Author

MAHDTech commented Mar 16, 2026

You're definetly right, it is very broad.

There are a few options here according to the docs

  1. IPv4 Address my-hostname=8.8.8.8
  2. IPv6 Address myhostv6=::1
  3. IPv6 address (bracketed) myhostv6=[2001:db8::33]
  4. Keywords host.docker.internal=host-gateway

So my best attempts can think of are to use either

  • use regex 🤮
  • use the tuple method you suggested.

If we go with tuple, it will need to have logic in part 2 to re-assemble it.

I'm not the greatest at regex but something like this could work;

const ExtraHostEntrySchema = z
  .string()
  .regex(
    /^[^=:]+[=:](\d{1,3}(\.\d{1,3}){3}|[0-9a-fA-F:]+|\[[0-9a-fA-F:]+\]|host-gateway)$/,
    'Must be in the format "hostname=ip", "hostname=host-gateway", or "hostname:ip"'
  );

extraHosts: z.array(ExtraHostEntrySchema).optional()

I like the tuple idea it makes it much cleaner on this side, and we get to use those cool zod builtins z.ipv4() and z.ipv6()

const HostNameSchema = z.string().min(1);

const ExtraHostEntrySchema = z.tuple([
  HostNameSchema,
  z.union([z.ipv4(), z.ipv6(), z.literal('host-gateway'), HostNameSchema])
]);

extraHosts: z.array(ExtraHostEntrySchema).optional()

On the CLI side, we would then need to join: ([host, dest]) => ${host}:${dest} before it gets passed in to --add-host

Are you leaning a certain way, regex always feels brittle to me but if done right could work.

- use the cleaner tuple method
- add test suite
@peterpeterparker peterpeterparker changed the title feat: Add optional Docker extraHosts (part 1) feat(config): Add optional Docker extraHosts (part 1) Mar 16, 2026
Copy link
Contributor

@peterpeterparker peterpeterparker left a comment

Choose a reason for hiding this comment

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

LGTM, thanks a lot!

expect(result.success).toBe(true);
});

it('accepts an empty array', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll maybe add a rule to prevent empty.

@peterpeterparker peterpeterparker merged commit 429a65f into junobuild:main Mar 16, 2026
6 of 8 checks passed
@MAHDTech MAHDTech deleted the feat/docker-hosts branch March 16, 2026 10:51
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