#Using Dagger to run containers that don't exit

1 messages · Page 1 of 1 (latest)

obtuse marlin
#

Is there a way to use dagger to effectively do a "docker run" command on a dagger.Container object? I've tried just using container.Stdout(ctx) but I can not find the mongo container with docker ps -a. I'm probably missing something obvious, or trying to mis-use CI/CD to run app code

In my use-case I do not need to push the images to a remote registry, I want to just build the mongo container and start running it

func BuildMongo(ctx context.Context, client *dagger.Client) *dagger.Container {
  err := godotenv.Load()
  if err != nil {
      panic(err)
  }

  username := os.Getenv("MONGO_INITDB_ROOT_USERNAME")
  password := os.Getenv("MONGO_INITDB_ROOT_PASSWORD")

    mongoContainer := client.Container().From("docker.io/mongo:latest").WithEnvVariable("MONGO_INITDB_ROOT_USERNAME", username).WithEnvVariable("MONGO_INITDB_ROOT_PASSWORD", password)

    mongoContainer.WithEntrypoint([]string{"-d", "hak247", "--port", "27017"})
    return mongoContainer
}

func StartMongo(ctx context.Context, mongoContainer *dagger.Container) {
    mongoContainer.Stdout(ctx)
}
low skiff
obtuse marlin
#

Thanks @low skiff I'll try it out as a service

low skiff
#

Once you add the with_exposed_port method to the Mongo container you'll need with_service_binding on whatever container calls the Mongo service

obtuse marlin
coarse axle
obtuse marlin
#

Updated the mongo code with @low skiff's suggestions. Ended up just publishing the mongo image and running it with bash docker run instead of services as I needed the mongo container to run standalone. Thanks for help guys, can close this.

func BuildMongo(ctx context.Context, client *dagger.Client, portNo int) (*dagger.Container, error) {
    err := godotenv.Load()
    if err != nil {
        return nil, err
    }

    username, err := client.Host().EnvVariable("MONGO_INITDB_ROOT_USERNAME").Value(ctx)
    if err != nil {
        return nil, err
    }
    password, err := client.Host().EnvVariable("MONGO_INITDB_ROOT_PASSWORD").Value(ctx)
    if err != nil {
        return nil, err
    }
    passwordSecret := client.SetSecret("MONGO_INITDB_ROOT_PASSWORD", password)

    mongoContainer := client.Container().From("docker.io/mongo:latest").WithEnvVariable("MONGO_INITDB_ROOT_USERNAME", username).WithSecretVariable("MONGO_INITDB_ROOT_PASSWORD", passwordSecret).WithExposedPort(portNo)

    mongoContainer.WithEntrypoint([]string{"-d", "hak247", "--port", strconv.Itoa(portNo)})
    return mongoContainer, nil
}
coarse axle
sacred rose
coarse axle
sacred rose
#

you can already run them like svc.ExitCode(ctx) since they're regular containers, but i'm not sure if our docs cover that already