func (c *Postgres) Create(ctx context.Context, container types.ContainerJSON, title, imageName string) error {
	buildContext, err := ioutil.TempDir("", "dksnap-context")
	if err != nil {
		return fmt.Errorf("make build context dir: %w", err)
	}
	defer os.RemoveAll(buildContext)
	dump, err := exec(ctx, c.client, container.ID, []string{"pg_dumpall", "-U", c.dbUser})
	if err != nil {
		return fmt.Errorf("dump: %w", err)
	}
	if err := ioutil.WriteFile(filepath.Join(buildContext, "dump.sql"), dump, 0644); err != nil {
		return fmt.Errorf("write dump: %w", err)
	}
	loadScript := []byte(`#!/bin/bash
psql --username "${POSTGRES_USER:-postgres}" -d "${POSTGRES_DB:-postgres}" --no-password < /dksnap-dump.sql`)
	if err := ioutil.WriteFile(filepath.Join(buildContext, "load-dump.sh"), loadScript, 0755); err != nil {
		return fmt.Errorf("write dump: %w", err)
	}
	err = buildImage(ctx, c.client, buildOptions{
		baseImage: container.Image,
		context:   buildContext,
		bootCommands: []string{
			"rm -rf /var/lib/postgresql/data/*",
		},
		buildInstructions: []string{
			"COPY load-dump.sh /docker-entrypoint-initdb.d/load-dump.sh",
			"COPY dump.sql /dksnap-dump.sql",
		},
		title:      title,
		imageNames: []string{imageName},
		dumpPath:   "/dksnap-dump.sql",
	})
	if err != nil {
		return fmt.Errorf("build image: %w", err)
	}
	return nil
}
Create creates a new snapshot.
package snapshot
import (
	"bytes"
	"context"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/client"
	"github.com/docker/docker/pkg/stdcopy"
)
type Postgres struct {
	client *client.Client
	dbUser string
}
func NewPostgres(c *client.Client, dbUser string) Snapshotter {
	return &Postgres{c, dbUser}
}