#Running docker build from inside of dagger

1 messages · Page 1 of 1 (latest)

icy mural
#

I have the following golang function which tries to build a docker image from inside of dagger, but I get this error:

func build_docker_image(ctx context.Context, client *dagger.Client) {
    // get reference to source code directory
    source := client.Host().Directory(".", dagger.HostDirectoryOpts{
        Exclude: []string{"ci", "build"},
    })

    app := client.Container().
        From("docker:25-dind").
        WithMountedDirectory("/src", source).
        WithWorkdir("/src")

    build, build_err := app.WithExec([]string{"docker", "build", "-t", "hexchess-server", "."}).Stdout(ctx)
    if build_err != nil {
        panic(build_err)
    }
    fmt.Println(build)

        // Tag and publish the build
}
tame hearth
#

The simplest way to build an image from dockerfile is to call the builtin Directory.dockerBuild() function. It's fully compatible and doesn't require the docker cli or engine.

icy mural
#

Aaah, that would 100% solve my problem sorry haha

tame hearth
#
func build_docker_image(ctx context.Context, client *dagger.Client) {
    // get reference to source code directory
    source := client.Host().Directory(".", dagger.HostDirectoryOpts{
        Exclude: []string{"ci", "build"},
    })
    ctr := source.DockerBuild()
    ref, err := ctr.Publish(ctx, "myregistry/myimage:mytag")
}
tame hearth
icy mural
#

My keytype is _json_key

tame hearth
#

Yes it's the withRegistryAuth method 👍

#

I think as a special case, Dagger will detect and use your local docker config

#

withRegistryAuth to be safe though

icy mural
#

Ok I got everything working and publishing. Last question here sorry - how do I tag the newly built docker container? What's the equivalent of docker tag ?
Everything I'm publishing to GCR is tagged w/ latest which I'd like to customize

tame hearth
icy mural
#

Is it meant to be passed in the name like ${url}:${tag} or is it part of the PublishOpts? It's not super clear where it's supposed to go sorry