#Exposing container service to host

1 messages ยท Page 1 of 1 (latest)

vapid pendant
#

Hi all!

I am trying to expose a redis container service to the host system. But I do lack some basic fundamentals I guess.

Imagine following dagger approach:

async def main():
    async with dagger.Connection(dagger.Config(log_output=sys.stderr)) as client:
        redis_srv =  (
            client.container()
            .from_("redis")
            .with_exposed_port(6379)
            .with_mounted_cache("/redis_data", client.cache_volume("my-redis"))
            .with_workdir("/redis_data")
            .as_service()
        )

        redis_cli = (
            client.container()
            .from_("redis")
            .with_service_binding("redis-srv", redis_srv)
            .with_entrypoint(["redis-cli", "-h", "redis-srv"])
        )
        await redis_cli.with_exec(["set", "foo", "abc"]).with_exec(["save"]).stdout()

        tunnel = await client.host().tunnel(redis_srv).start()
        endpoint = await tunnel.endpoint()

        return endpoint

Within the main I am able to query the service etc. as expected. However, outside I am not. I guess I do miss something obvious.
I thought, that I might be able to call the service like so:

endpoint = await main()
host, port = endpoint.split(":")
r = redis.Redis(host=host, port=6379, decode_responses=True)
print(r.get("foo"))

But in this case, I get a connection error.

Thanks for the help ๐Ÿ™‚

near rapids
#

If you leave the with dagger.Connection context, the connection to the engine will be closed. Everything you need to do in dagger needs to be inside that context.

woeful moat
#

๐Ÿ‘‹ that's because the tunnel method uses a random port as frontend automatically to avoid port collisions so you need to use the endpoint variable to get the right host:port values. You can still make it use any port you want by using the ports argument in the tunnel method

vapid pendant
vapid pendant
woeful moat
#

if you print endpoint, you'll see it uses a different port

vapid pendant
vapid pendant
#

Thanks both of you, works now like I expect ๐Ÿ™‚