func StopContainer(daemon *dockertest.Docker, id string) error {
	client, err := daemon.Client()
	if err != nil {
		return errors.Wrap(err, "unable to create docker client")
	}
	err = client.RemoveContainer(dockerclient.RemoveContainerOptions{
		ID:            id,
		RemoveVolumes: true,
		Force:         true,
	})
	if err != nil {
		return errors.Wrapf(err, "unable to stop container %s", id)
	}
	return nil
}
StopContainer stops a running container.
package docker_test
import (
	docker "."
	"fmt"
	"github.com/BlueDragonX/beacon/beacon"
	"github.com/BlueDragonX/go-docker-test/dockertest"
	dockerclient "github.com/fsouza/go-dockerclient"
	"github.com/pkg/errors"
	"sync"
	"testing"
	"time"
)
const (
	DOCKER_IMAGE_REPO = "alpine"
	DOCKER_IMAGE_TAG  = "latest"
)
func WaitForEvents(ch <-chan *beacon.Event, n int, timeout time.Duration) ([]*beacon.Event, error) {
	events := make([]*beacon.Event, 0, n)
	timer := time.After(timeout)
	for i := 0; i < n; i++ {
		select {
		case event, ok := <-ch:
			if !ok {
				return events, errors.New("channel closed")
			}
			events = append(events, event)
		case <-timer:
			return events, errors.New("timed out")
		}
	}
	return events, nil
}
func DockerSetup() (*dockertest.Docker, error) {
	daemon, err := dockertest.New()
	if err != nil {
		return nil, errors.Wrap(err, "unable to start docker daemon")
	}
	client, err := daemon.Client()
	if err != nil {
		daemon.Close()
		return nil, errors.Wrapf(err, "unable to pull docker image %s:%s", DOCKER_IMAGE_REPO, DOCKER_IMAGE_TAG)
	}
	err = client.PullImage(dockerclient.PullImageOptions{
		Repository: DOCKER_IMAGE_REPO,
		Tag:        DOCKER_IMAGE_TAG,
	}, dockerclient.AuthConfiguration{})
	if err != nil {
		daemon.Close()
		return nil, errors.Wrapf(err, "unable to pull docker image %s:%s", DOCKER_IMAGE_REPO, DOCKER_IMAGE_TAG)
	}
	return daemon, nil
}
func StartContainer(daemon *dockertest.Docker, bindings []*beacon.Binding, labels map[string]string) (string, error) {
	client, err := daemon.Client()
	if err != nil {
		return "", errors.Wrap(err, "unable to create docker client")
	}
	exposedPorts := map[dockerclient.Port]struct{}{}
	for _, binding := range bindings {
		port := dockerclient.Port(fmt.Sprintf("%d/%s", binding.ContainerPort, binding.Protocol))
		exposedPorts[port] = struct{}{}
	}
	container, err := client.CreateContainer(dockerclient.CreateContainerOptions{
		Config: &dockerclient.Config{
			Image:        fmt.Sprintf("%s:%s", DOCKER_IMAGE_REPO, DOCKER_IMAGE_TAG),
			Cmd:          []string{"/bin/sh", "-c", "trap exit SIGTERM SIGINT; while true; do sleep 0.1; done"},
			ExposedPorts: exposedPorts,
			Labels:       labels,
		},
	})
	if err != nil {
		return "", errors.Wrap(err, "unable to create docker container")
	}
	portBindings := map[dockerclient.Port][]dockerclient.PortBinding{}
	for _, binding := range bindings {
		port := dockerclient.Port(fmt.Sprintf("%d/%s", binding.ContainerPort, binding.Protocol))
		portBindings[port] = []dockerclient.PortBinding{{
			HostIP:   binding.HostIP,
			HostPort: fmt.Sprintf("%d", binding.HostPort),
		}}
	}
	err = client.StartContainer(container.ID, &dockerclient.HostConfig{
		PortBindings: portBindings,
	})
	if err != nil {
		StopContainer(daemon, container.ID)
		return "", errors.Wrap(err, "unable to start docker container")
	}
	return container.ID, nil
}