#I hope you don't get this the wrong way

1 messages · Page 1 of 1 (latest)

vivid trail
#

The situation is that I'm trying to build up a container image with only the necessary runtime dependencies, while also keeping the ability to stem off the original base image (with some additional build dependencies) to generate files/directories (like only keeping the gems from a ruby "bundle install").

While contrived, this might(?) provide a bit of a code example?

type Container struct {
    Container *dagger.Container
    baseImage string
    platform  string
}

func New(baseImage string, platform string) *Container {
    if platform == "" {
        platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
    }

    ctr := dag.
        Container(dagger.ContainerOpts{Platform: dagger.Platform(platform)}).
        From(baseImage)

    return &Container{
        Container: ctr,
        baseImage: baseImage,
        platform:  platform,
    }
}

func (m *Container) Build() *dagger.Container {
    return m.
        WithGeneratedFiles().
        Container
}

func (m *Container) WithGeneratedFiles() *Container {
    dir := m.BaseContainer().
        WithWorkDir("/src").
        WithExec([]string{"touch", "thing"}).
        WithDirectory("foo", dag.Directory()).
        WithNewFile("bar", "this is a file").
        Directory("/src")

    ctr := m.Container.WithDirectory("/generated", dir)

    return &Container{
        Container: ctr,
        baseImage: baseImage,
        platform:  platform,
    }
}

// A base container which would have the necessary BUILD dependencies
// generating files like a ruby "bundle install"
func (m *Container) BaseContainer() *Container {
    ctr := dag.
        Container(dagger.ContainerOpts{Platform: m.platform}).
        From(m.imageRef)

    return &Container{
        Container: ctr,
        baseImage: m.baseImage,
        platform:  m.platform,
    }
}
#

Having to keep the baseImage and platform around is tedious, whereas having to use a context to get them is also a bit cumbersome for chaining calls

pastel vigil
#

You can add the context to any function and dagger takes care of passing that in.

vivid trail
#

That's true. However, using something like ImageRef would require handling an error, which makes chaining calls a bit awkward if an intermediate step can return an error?

sharp valve
#

@vivid trail Can you also show what dagger call you are using? I think I get how you use this, and I still think this could be converted to what I proposed fairly easily.