Skip to content

Container networking #4080

Closed
Closed

Description

@shykes

Overview

This issue tracks the design and implementation status of container networking, in its various forms.

Host-to-container (h2c) Container-to-host (c2h) Container-to-container (c2c)
Use cases Expose a service to the host
  • SSH agent forwarding
  • Connect to docker engine
  • Connect to a service only reachable from the host
  • Run a test database
  • Run e2e integration tests
  • Run sidecar services
Status ❌ not implemented 🏗 in development ✔️ #4505
Notes Will only be available for long-running containerized services, not short-lived exec
  • Unix sockets: ✅ implemented
  • TCP: 🏗 API discussion
  • UDP: 🏗 API discussion
Use bass c2c networking as a reference

Design proposals

Container networking is a vast feature area. There are several ongoing proposals to design certain parts of it. Some of these proposals are compatible, while others are not.

Proposal c2h (tcp/udp) h2c (tcp/udp) c2c c2h (unix) h2c (unix) status
Mount unix socket into exec ❌ ❌ ❌ ✅ ❌ Implemented
forwarding host TCP/UDP addresses to container ✅ ❌ ❌ ❌ ❌ In Discussion
inbound and outbound tunnels ✅ ✅ ❌ ❌ ❌ Withdrawn
bass-style c2c networking ❌ ❌ ✅ ❌ ❌ Implemented
“bass-plus” container networking ✅ ✅ ✅ ❌ ❌ Partially implemented

Submitting a proposal

  1. Open a draft PR with graphql schema changes and, ideally, skeleton documentation
  2. Post a link to the PR in the comments to this issue. Mention which areas are covered by the proposal (c2h, h2c, c2c)

Discussing a proposal

  1. Comment on the relevant PR

Research material

  • SSH port forwarding (ssh -L and ssh -R)
  • Reverse proxy configurations (nginx, apache, varnish, envoy)
  • Kubernetes and Docker networking terminology
  • Bass-lang networking

Activity

  1. sipsma commented on Dec 2, 2022

    @sipsma
    Contributor

    Are we considering the container-to-host case (i.e. I want to run a dev webserver in dagger and access it from my browser) to be the same as host-to-container? In theory they are technically different (who does the connect, who does the listen), though if you do a reverse proxy thing locally then they can become the same.


    Also, should probably clarify whether "container" means "exec" or "service" or both. My starting point is that we should not support serving sockets from execs and only serve them from services; execs are only able to consume them. But there's a lot to unpack and think about there so I'm far from adamant, just my first inclination.

  2. aluzzardi commented on Dec 2, 2022

    @aluzzardi
    Contributor

    My starting point is that we should not support serving sockets from execs and only serve them from services;

    I agree -- had some comments about that in #3691 (comment)

    IF that's the case, then we don't necessarily need to mirror all calls for bind/connect.

    My straw man proposal for container-to-service was to mirror host:

    c.Container().WithTCPSocket(8080, c.Host().TCPSocket(8080))
    c.Container().WithTCPSocket(8080, myService.TCPSocket(8080))

    Regarding host-to-service (IF it's always a service), then we can have some service specific instructions:

    // 1) At service creation time
    c.Service().Create(<args from the future>, dagger.ServiceCreateOpts{
      Expose: [some port bindings]
    })
    
    // 2) Dynamic exposure
    // ??? Unsure how it'd play out ??
    // Probably a bit too dynamic (once the service is running it's too late to mess with the shim, without some serious effort)
    myService.TCPSocket(8080).ExposeTCP("localhost:8080")
    
    // 3) CLI command
    $ dagger service proxy <my service>:8080:8080
  3. jlongtine commented on Dec 2, 2022

    @jlongtine
    Contributor

    Regarding host-to-service (IF it's always a service), then we can have some service specific instructions:

    I like option 1, personally. Keeps it in code, keeps it at service "boot". The only suggestion I have is that we clarify that it is being exposed on the host, not just within the Buildkit network/context. I'm not quite sure how to do that yet, but just Expose feels a bit unclear to my eyes.

    Now that I've written that down, I'm wondering if it's possible have an API like this:

    type Host {
      bind|expose(service: Service!): ??
    }

    That would make pure GraphQL service queries a bit harder/uglier, but I like the clarity that would provide for the SDKs. Having said that, it seems like it will have the same issues at option 2 around messing with the shim, etc.

  4. shykes commented on Dec 2, 2022

    @shykes
    Author
  5. vito commented on Dec 2, 2022

    @vito
    Contributor

    +1 to starting with a distinction between services and containers for the sake of conversation.

    Depending on how this goes, it might be the case that the service's container needs to be aware that it's being used as a service - via the underlying LLB we generate and/or the responsibilities of the shim - but we can cross that bridge when we get to it.

    I put a bunch of details around how Bass's c2c/service networking works here: #3850 (comment)

    Here are some questions based on that:

    How do we want the traffic to flow in the following snippet?

    c.Container().WithTCPSocket(8080, myService.TCPSocket(8080))

    Typically a client container will need to be given the address to reach as a flag/arg/env/etc. With this scheme would they just use 127.0.0.1:8080?

    Does that imply we have something proxying traffic between them? Are we OK with the performance hit there? The answer might simply be yes, and maybe it's a minor performance hit anyhow, but it's worth trying to think about any performance-critical use cases here. If we only care about dev/test this is probably fine. It's hard to imagine someone running e.g. a production database with Dagger since it's not really built for persistence.

    Another option is to use direct c2c addressing with a bridge network (+ DNS to prevent ephemeral container IPs from busting the cache) but then we'd need to figure out how the user configures the address for the client container. We can also use DNS to avoid cache busting while keeping the proxy to solve that.

    Will we implement health/liveness/readiness checks?

    My take on this is the minimum viable health check is to just wait for a port to be listening, and that we should just do that by default since it's really annoying to have to manually poll for simple cases.

    I don't have a strong opinion on continuous health monitoring beyond that.


    @jlongtine Good question - I think we likely won't ever need to call 'expose' for container-to-container (within Buildkit network). So I think we can get away with having Expose live on the service and have it always mean "expose on the host" which would draw parallels to e.g. Container.Export, Directory.Export which also affect the the host.

  6. moved this to In Progress in @vito's Dagger backlogon Dec 5, 2022
  7. moved this from In Progress to Awaiting Feedback in @vito's Dagger backlogon Dec 5, 2022
  8. shykes commented on Dec 6, 2022

    @shykes
    Author
  9. jlongtine commented on Dec 6, 2022

    @jlongtine
    Contributor

    @jlongtine Good question - I think we likely won't ever need to call 'expose' for container-to-container (within Buildkit network). So I think we can get away with having Expose live on the service and have it always mean "expose on the host" which would draw parallels to e.g. Container.Export, Directory.Export which also affect the the host.

    Yeah, this makes sense, @vito. I'm on board.

  10. changed the title [-]Socket forwarding[/-] [+]Tunneling[/+] on Dec 7, 2022
  11. shykes commented on Dec 7, 2022

    @shykes
    ContributorAuthor

    Yesterday we had a 1-1 chat with @vito that melted my brain. After sleeping on it, I am updating this issue with:

    1. A rewrite of the context and summary table. Hopefully it's more useful now
    2. A list of open questions
    3. A proposal for terminology and API design. To quote @vito: "i think there's potentially something better still, but it's a good strawman"

    Please take a look and share your thoughts.

  12. shykes commented on Dec 7, 2022

    @shykes
    ContributorAuthor

    Addressing a few comments and questions above:

    @sipsma:
    Are we considering the container-to-host case (i.e. I want to run a dev webserver in dagger and access it from my browser) to be the same as host-to-container? In theory they are technically different (who does the connect, who does the listen), though if you do a reverse proxy thing locally then they can become the same.

    👍 You are right, they are different and my framing was awkward. The root cause of the awkwardness was the use of the term "socket" which does actually apply to both (a socket is something you either connect or bind. then bytes are forwarded in both directions). Removing the term "socket" from feature name and API design allowed me to reconcile 1) non-confusing terminology for the thing being forwarded, and 2) non-confusing terminology for the "direction" of things.

    @sipsma:
    Also, should probably clarify whether "container" means "exec" or "service" or both. My starting point is that we should not support serving sockets from execs and only serve them from services; execs are only able to consume them. But there's a lot to unpack and think about there so I'm far from adamant, just my first inclination.
    @aluzzardi :
    I agree

    👍 I agree also, clarified this in the issue.

    For high-level discussion, it's still useful to use c2h/h2c/c2c (3 terms), instead of c2h/s2h/h2s/c2s/s2s (5 terms) :) But that's just terminology, we're all on the same page on design and implementation.

    @aluzzardi @vito @jlongtine
    (on the concept of "expose" for h2c)

    🙋🏻‍♂️It makes sense in theory, since we have "directory export" and "container export" which follow the same pattern.
    However, I think we should not use this particular pattern here, because it is not intuitive whether the port is actually bound in the host, or simply available for ulterior service discovery from either the host or a container.

    In Docker we had terminology for both: you could "expose" a port (available for service discovery), or "publish" it (bound to the host). This was generally confusing to users.

    I propose a variation of this pattern that still controls the tunneling from Service (rather than Host), but is more explicit about what's going on. withInboundTunnel clearly means "create a tunnel that will forward inbound traffic".

  13. grouville commented on Dec 9, 2022

    @grouville
    Member

    With the implementation of the WithUnixSocket primitive, users (and myself) have started to spawn local registries to their host docker engine, using it as a long-runner engine: #4089

    By relying on Docker for most long-running tasks:

    • the Host-to-container (h2c) is not as urgent as Container-to-host (c2h). Same for the Container-to-container (c2c), which could be workarounded by users if we had access to c2h primitives
    • healthchecks are quite easy to implement natively and might alternatively be tackled using extensions:
    // query localhost:5000/v2/ until it returns 200 with a 20 seconds timeout
    func waitForRegistry() error {
    	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
    	defer cancel()
    
    	req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost:5000/v2/", nil)
    	if err != nil {
    		return err
    	}
    
    	client := http.Client{}
    	for {
    		resp, err := client.Do(req)
    		if err != nil {
    			// retry
    			time.Sleep(1 * time.Second)
    			continue
    		}
    
    		if resp.StatusCode == http.StatusOK {
    			return nil
    		}
    	}
    }

    Current blockers
    I am currently relying on the WithMountedSockets API for #4038.

    However, I am blocked by the fact that I cannot export an image (using our core API) to my private registry if it is not part of the dagger-engine network. I currently have 2 options:

    • create a registry that is part of the dagger-engine network
    • push my image using the docker CLI

    I feel that the socket implementation tackles well the SSH forwarding issues, but the Container-to-host begins to feel needed for more complex use-cases

    Why am I blocked ?
    We have the hack/make makefile that abstracts a lot of complexity and relies on Dagger-in-Dagger to build the engine. We have a engine:publish method that publishes to a registry, however, it doesn't work (due to above reasons) with a local registry, as we do not run the engine with the --net=host option when running buildkit (+ I don't think that the current architecture would allow it)

    From current understanding, I will have to:

    1. Either move that logic outside of Dagger
    2. Hack around that (current exploration)
  14. 3 remaining items

  15. shykes commented on Dec 10, 2022

    @shykes
    Author
  16. changed the title [-]Tunneling[/-] [+]Container networking[/+] on Dec 10, 2022
  17. shykes commented on Dec 10, 2022

    @shykes
    Author
  18. shykes commented on Dec 10, 2022

    @shykes
    Author
  19. sipsma commented on Dec 10, 2022

    @sipsma
    Contributor

    DNS addresses would not be user-configurable, to avoid collisions. Instead they would be a content-addressed hash. For Bass I use base32 + xxh3 to get short addresses like GRLO5DSIPLJPQ.

    @vito What's the input to the hash you use in bass? The LLB of the service being depended on? If so, then I'm wondering how we'd deal with sources where the LLB is stable but the actual contents can change (e.g. image:latest or local dirs)

  20. vito commented on Dec 12, 2022

    @vito
    Contributor

    @vito What's the input to the hash you use in bass? The LLB of the service being depended on? If so, then I'm wondering how we'd deal with sources where the LLB is stable but the actual contents can change (e.g. image:latest or local dirs)

    @sipsma Bass uses the hash of the thunk, so it's a hash of the LLB "recipe" rather than the actual LLB. Thunks are fully specified - they pin image digests and don't use any other Git/HTTP LLB sources. Just images and commands.

    So this is a great point. I think it would be fine to use a hash of the LLB even with unresolved references since they should be stable within that session, as long as the single session is used for running both the service and the client. This definitely relates to #3613 especially if that's not actually possible for some reason (similar to the current Container.Export limitation).

  21. yonidavidson commented on Dec 30, 2022

    @yonidavidson

    Adding my +1 to this feature, I would like to use this for running pre migration validations on databases and I need to spin up a MySQL DB and another container that communicates with it

  22. added
    area/engine
    on Jan 10, 2023
  23. sourcec0de commented on Feb 14, 2023

    @sourcec0de

    I've got a few use cases for this.

    1. as @yonidavidson said, we have a pipeline that needs to run validations and migrations against a database
    2. we deploy to private GKE clusters exposed by VPNs

    Both locally and in Github actions we are able to reach these systems through tailscale.

  24. mircubed commented on Feb 24, 2023

    @mircubed
    Contributor

    @vito with #4505 merged, can you update the issue description? It looks like it wasn't updated since Dec 13th, so I want to make sure that it reflects the work that still needs to be done. Thanks!

  25. jpadams commented on Jul 18, 2023

    @jpadams
    Contributor

    @verdverm Would be great for you to outline your needs/use cases around docker, nerdctl, podman and container networking here. 🙏

    rootless, multi-os, multi-arch

  26. moved this from Awaiting Feedback to Done in @vito's Dagger backlogon Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions