#Problem binding mongo container

1 messages · Page 1 of 1 (latest)

left lion
#

I have the following code:

const (
    GO    = "golang:1.20-alpine3.17"
    MONGO = "bitnami/mongodb:4.0"
)

func run(ctx context.Context) error {
    mongo := client.Container().From(MONGO).
        WithEnvVariable("MONGODB_ADVERTISED_HOSTNAME", "mongo").
        WithEnvVariable("MONGODB_REPLICA_SET_MODE", "primary").
        WithEnvVariable("MONGODB_ROOT_PASSWORD", "password123").
        WithEnvVariable("MONGODB_REPLICA_SET_KEY", "replicasetkey123").
        WithEnvVariable("BITNAMI_DEBUG", "true").
        WithExposedPort(27017)

    golang := client.Container().From(GO).
        WithEnvVariable("MONGO_ENDPOINT", "mongo:27017").
        WithEnvVariable("MONGO_USERNAME", "root").
        WithEnvVariable("MONGO_PASSWORD", "password123").
        WithEnvVariable("MONGO_REPLICA_SET", "replicaset").
        WithServiceBinding("mongo", mongo)

    log.Info().Msg("Running Integration tests")
    golang = golang.WithExec([]string{"make", "test-integration"})

    stdout, err := golang.Stdout(ctx)
    if err != nil {
        log.Error().Err(err).Msg("error getting stdout")
        return err
    }
    fmt.Println(stdout)
    return nil
}```
When running with 
go run dagger/integration/main.go
I get the following:
```bash
...
#3 1.096 Successfully added user: {
#3 1.096        "user" : "root",
#3 1.096        "roles" : [
#3 1.096                {
#3 1.096                        "role" : "root",
#3 1.096                        "db" : "admin"
#3 1.096                }
#3 1.096        ]
#3 1.096 }
#3 1.097 bye
#3 1.103 mongodb 21:34:42.73 INFO  ==> Users created
#3 1.104 mongodb 21:34:42.73 INFO  ==> Writing keyfile for replica set authentication...
#3 1.124 mongodb 21:34:42.76 INFO  ==> Configuring MongoDB replica set...
#3 1.131 mongodb 21:34:42.76 INFO  ==> Stopping MongoDB...

any Idea how I can make mongo run well with dagger? it runs well using docker-compose

edgy ingot
#

👋 that's because Dagger doesn't have an embedded tiny init like Docker has so when the bitnami mongo image starts mongo in --fork mode (can't be disabled), then the dagger service exists. Here's an alternative to add tiny to the service conainer to make it work.

#

const (
    GO    = "golang:1.20-alpine3.17"
    MONGO = "bitnami/mongodb:4.0"
)

func main() {
    ctx := context.Background()
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
    if err != nil {
        panic(err)
    }

    tiny := client.HTTP("https://github.com/krallin/tini/releases/download/v0.19.0/tini")
    mongo := client.Container().From(MONGO).
        WithEnvVariable("MONGODB_ADVERTISED_HOSTNAME", "mongo").
        WithEnvVariable("MONGODB_REPLICA_SET_MODE", "primary").
        WithEnvVariable("MONGODB_ROOT_PASSWORD", "password123").
        WithEnvVariable("MONGODB_REPLICA_SET_KEY", "replicasetkey123").
        WithEnvVariable("BITNAMI_DEBUG", "true").
        WithFile("/tiny", tiny).
        WithUser("root").
        WithExec([]string{"chmod", "+x", "/tiny"}, dagger.ContainerWithExecOpts{SkipEntrypoint: true}).
        WithUser("1001").
        WithEntrypoint([]string{"/tiny", "-s", "--", "bash", "/opt/bitnami/scripts/mongodb/entrypoint.sh"}).
        WithExec([]string{"/opt/bitnami/scripts/mongodb/run.sh"}).
        WithExposedPort(27017)

    golang := client.Container().From(GO).
        WithEnvVariable("MONGO_ENDPOINT", "mongo:27017").
        WithEnvVariable("MONGO_USERNAME", "root").
        WithEnvVariable("MONGO_PASSWORD", "password123").
        WithEnvVariable("MONGO_REPLICA_SET", "replicaset").
        WithServiceBinding("mongo", mongo)

    golang = golang.WithExec([]string{"apk", "add", "mongodb-tools"})
    golang = golang.WithExec([]string{"mongostat", "--host", "mongo", "--username", "root", "--password", "password123", "--authenticationDatabase", "admin", "--discover"})

    stdout, err := golang.Stdout(ctx)
    if err != nil {
        panic(err)
    }
    fmt.Println(stdout)
}
#

^ take into account that tiny is being downloaded for amd64 there. If you use a different architecutre, you should check that

#

I'll open an issue to document this

#

silent ping to @formal roost and @thorn frigate if you can think of a better way to handle this today