#Get IP address of Service container

1 messages · Page 1 of 1 (latest)

steep grail
#

Hey!
I have a situation where I'm attempting to use Dagger for some custom loadbalancer integration tests. Part of this requires that a set of targets must be specified on a resource using IP addresses.
My idea was to spin up several Nginx containers, get their IP addresses, use them as the targets for the resource and then happily test that everything is working.

Unfortunately, I can't seem to find a nice and reliable way get the IP address of a Service container. I have searched Discord for similar questions and found one who's answer seems to suggest using another container to ping or dig the servicebind alias to get the IP address.

This seems overkill for something that with testcontainers is as simple as calling ContainerIP() on a Container instance.

Am I missing something?

Thanks all in advance!

ebon bramble
#

Service containers are more like k8s, where you normally refer to things by a name rather than an IP. I don't think Dagger / BuildKit is allocating unique IP addresses for them, instead they get associated with a name when you bind the service to the container running the tests against said services

steep grail
#

Ah that's a shame. Thanks for the info!

trail osprey
# steep grail Ah that's a shame. Thanks for the info!

👋 since services only get created when some other part of the pipeline uses them or via the Start function, the IPs will only get assigned once they start and they will be released a bit after the step that used the services finshes.

Having said that, when you use WithServiceBinding, Dagger will populate the /etc/hosts file in that container with the IP<>Name of each service so you could maybe use that file to get the IPs that you're looking for? 🤔

steep grail
#

Thanks @trail osprey

ember saffron
#

I think we can do that @trail osprey , but a new helper function GetHostname would really help @steep grail out for our testing

trail osprey
steep grail
#

Getting the hostname isn't the problem. It's getting the IP address of the container in a simple way without having to start another container just to be able to do so.
From what I understand from the previous messages in this thread, an IP address is only assigned once a container binds to the Service. A simple way to get the IP address from this bind would be very useful

trail osprey
steep grail
#

@trail osprey thanks! Yes, of course. I'll get on it shortly.
Thanks for your help

trail osprey
#

🙏

steep grail
#

Although, another question:

  • I create a service
  • I create another container which binds to the service with the sole purpose to be to get the IP address.
  • I get the IP address so the bind is no longer useful,

This IP that I've just got isn't necessarily going to be valid for another container when it binds to the service again, is it?

Once a container unbinds, does the IP get un-allocated? And then when another container comes along and binds to the service, it'll be allocated a new IP?

Thanks

trail osprey
#

This IP that I've just got isn't necessarily going to be valid for another container when it binds to the service again, is it?

Once a container unbinds, does the IP get un-allocated? And then when another container comes along and binds to the service, it'll be allocated a new IP?

This is partially correct. Services will get stopped after a bit (IIRC the default is 10s) as long as no other parts of the pipeline are referencing them. So you can prevent the IP from chaining there's another container referencing them

#

So if you get the IPs right before passing them them to the pipeline that needs them, you'll be ok

ebon bramble
steep grail
#

Ooh, and if you do manually manage them, i.e. start one without a binding at all, will it be assigned an IP address then?

#

~~Also, if I start multiple instances of a service like this

  target, err := dag.Container(opts).
        From("nginx").
        WithExposedPort(port, dagger.ContainerWithExposedPortOpts{
            Protocol:    dagger.NetworkProtocol(proto),
            Description: fmt.Sprintf("%s/%d", proto, port),
        }).
        AsService().Start(ctx)

this is in a function so I can call it with different hostnames for the bind etc.

All three containers have the same IP address and container ID. How do I create multiple distinct containers with the same configuration?~~

Don't worry, if I set a distinct label on the containers then I get new instances

trail osprey
trail osprey
ebon bramble
#

Same if I keep them running until the end of the pipeline, i.e. they will have a stable IP regardless of how many services I bind it to?

trail osprey
#

so in that case, the IP won't change

steep grail
#

Great! Thank you both. This is very useful!

ebon bramble
#

np, I actually have this need as well, though we have kicked that can down the road with some well placed comment blocks :]

steep grail
#

Can we not bind to a service when it's running?