This repository contains a package for fast database deployment in Docker container.
Download and install it:
go get github.com/egorgasay/dockerdb/v3
Import it in your code:
import "github.com/egorgasay/dockerdb/v3"
The first launch should look like this:
vdb, err := dockerdb.New(ctx, config)
if err != nil {
log.Fatal(err)
}
If the database was turned off, then you can turn it on using:
err := vdb.Run(ctx)
if err != nil {
log.Fatal(err)
}
package main
import (
"context"
"fmt"
"log"
"github.com/egorgasay/dockerdb/v3"
_ "github.com/lib/pq"
)
func main() {
ctx := context.TODO()
config := dockerdb.EmptyConfig().DBName("test").DBUser("test").
DBPassword("test").StandardDBPort("5432").
Vendor(dockerdb.Postgres15).SQL().PullImage()
vdb, err := dockerdb.New(ctx, config.Build())
if err != nil {
log.Fatal(err)
}
defer vdb.Clear(ctx)
var result string
err = vdb.SQL().QueryRow("SELECT 'db is up'").Scan(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
package main
import (
"context"
"fmt"
"log"
"github.com/egorgasay/dockerdb/v3"
_ "github.com/lib/pq"
)
func main() {
ctx := context.TODO()
vdb, err := dockerdb.New(ctx, dockerdb.PostgresConfig("simple-postgres").Build())
if err != nil {
log.Fatal(err)
}
defer vdb.Clear(ctx)
var result string
err = vdb.SQL().QueryRow("SELECT 'db is up'").Scan(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
package main
import (
"context"
"fmt"
"log"
"github.com/egorgasay/dockerdb/v3"
redis "github.com/redis/go-redis/v9"
)
func main() {
var cl *keydb.Client
var err error
ctx := context.TODO()
config := dockerdb.EmptyConfig().
DBName("myredisdb").StandardDBPort("6379").
Vendor("redis"). // name from dockerhub
NoSQL(func(conf dockerdb.Config) (stop bool) { // func that will determine that the db is ready for use
cl = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", "127.0.0.1", conf.GetActualPort()),
DB: 0,
})
_, err = cl.Ping(ctx).Result()
log.Println(err)
return err == nil
}, 10, time.Second*2).PullImage()
vdb, err := dockerdb.New(ctx, config.Build())
if err != nil {
log.Fatal(err)
}
defer vdb.Clear(ctx)
fmt.Println("db is up")
}