#How would someone implement live reload

1 messages · Page 1 of 1 (latest)

proud phoenix
#

Curious about this one too.

vagrant beacon
#

A little bit hacky but you can use cache volume in shared mod and sync to files to that volume with another command.

package main

type Test struct{}

func (m *Test) Sync(hello string) *Container {
    return dag.Container().
        From("alpine:latest").
        WithMountedCache("/cache", dag.CacheVolume("live-reload"), ContainerWithMountedCacheOpts{Sharing: Shared}).
        WithExec([]string{"sh", "-c", "echo " + hello + "> /cache/hello.txt"})
}

func (m *Test) Service() *Service {
    return dag.Container().
        From("alpine:latest").
        WithExec([]string{"apk", "add", "--no-cache", "python3"}).
        WithMountedCache("/cache", dag.CacheVolume("live-reload"), ContainerWithMountedCacheOpts{Sharing: Shared}).
        WithWorkdir("/cache").
        WithExec([]string{"python3", "-m", "http.server", "8080"}).
        WithExposedPort(8080).
        AsService()
}
#
  • Start service
➜  test git:(main) ✗ dagger up service --port 8080
⣷ dagger up service [2m46.6s]                                                                                                                                                                                                                                         
┃ 8080/TCP: tunnel 0.0.0.0:8080 -> ncmvm0cqausum.pke2jbr3jlaqs.dagger.local:8080                                                                                                                                                                                      
• Engine: d60327cbf4da (version v0.9.2)                                                                                                                                                                                                                               
⧗ 2m49.7s ⣷ 2754 • start python3 -m http.server 8080: 10.87.0.1 - - [02/Nov/2023 13:40:57] "GET /hello.txt HTTP/1.1" 200 -   
  • Update hello.txt
➜  test git:(main) ✗ dagger call sync --hello world
✔ dagger call sync [0.72s]
• Engine: d60327cbf4da (version v0.9.2)
⧗ 3.68s ✔ 681
➜  test git:(main) ✗ dagger call sync --hello world-2
✔ dagger call sync [0.88s]
• Engine: d60327cbf4da (version v0.9.2)
⧗ 3.87s ✔ 681
reef hawk
#

You are right: this is indeed hacky 😃

viscid gale
#

Yeah so IIUC, the main thing that's missing today is that Dagger doesn't support any sort of "continuous" syncing between host + containers; the Host() API only supports once-per-session imports and only explicit exports.

Our vague idea for this in the past has been to use something like Mutagen (https://mutagen.io) to enable more continuous syncing (either one-way or two-way). That's actually totally possible now thanks to host network proxying in v0.9.x, though it will require some host-specific setup unless/until we integrate it directly w/ the dagger cli.

It could be a very cool experiment even now though (though 100% chefkiss hacky). Rough sketch:

  1. Setup mutagen on host, listening on a port
  2. Create a service in dagger that has access to that host port mutagen is listening on
  3. That service should also have a shared cache volume, which is where it syncs the files to (same sort of idea as what Ali suggested 🙂 )
  4. Then anything else needs access to the files (i.e. another service that is watching changes and doing hot reload) also mounts that shared cache volume

Obviously we'd like to have official non-hacky support for this too, this sort of approach would just be for fun atm.