func (d *DockerManager) Start() error {
	ctx, cancel := context.WithCancel(context.Background())
	d.cancel = cancel
	startHandler := func(m eventtypes.Message) {
		logger.Debugf("Started container '%s'", m.ID)
		service, err := d.getService(m.ID)
		if err != nil {
			logger.Errorf("%s", err)
		} else {
			d.list.AddService(m.ID, *service)
		}
	}
	stopHandler := func(m eventtypes.Message) {
		logger.Debugf("Stopped container '%s'", m.ID)
		if !d.config.All {
			d.list.RemoveService(m.ID)
		} else {
			logger.Debugf("Stopped container '%s' not removed as --all argument is true", m.ID)
		}
	}
	renameHandler := func(m eventtypes.Message) {
		oldName, ok := m.Actor.Attributes["oldName"]
		name, ok2 := m.Actor.Attributes["oldName"]
		if ok && ok2 {
			logger.Debugf("Renamed container '%s' into '%s'", oldName, name)
			d.list.RemoveService(oldName)
			service, err := d.getService(m.ID)
			if err != nil {
				logger.Errorf("%s", err)
			} else {
				d.list.AddService(m.ID, *service)
			}
		}
	}
	destroyHandler := func(m eventtypes.Message) {
		logger.Debugf("Destroy container '%s'", m.ID)
		if d.config.All {
			d.list.RemoveService(m.ID)
		}
	}
	eventHandler := events.NewHandler(events.ByAction)
	eventHandler.Handle("start", startHandler)
	eventHandler.Handle("stop", stopHandler)
	eventHandler.Handle("die", stopHandler)
	eventHandler.Handle("kill", stopHandler)
	eventHandler.Handle("destroy", destroyHandler)
	eventHandler.Handle("rename", renameHandler)
	events.MonitorWithHandler(ctx, d.client, types.EventsOptions{}, eventHandler)
	containers, err := d.client.ContainerList(context.Background(), types.ContainerListOptions{})
	if err != nil {
		return errors.New("Error getting containers: " + err.Error())
	}
	for _, container := range containers {
		service, err := d.getService(container.ID)
		if err != nil {
			logger.Errorf("%s", err)
			continue
		}
		d.list.AddService(container.ID, *service)
	}
	return nil
}
Start starts the DockerManager
package core
import (
	"crypto/tls"
	"errors"
	"github.com/aacebedo/dnsdock/src/servers"
	"github.com/aacebedo/dnsdock/src/utils"
	"github.com/docker/engine-api/client"
	"github.com/docker/engine-api/types"
	eventtypes "github.com/docker/engine-api/types/events"
	"github.com/vdemeester/docker-events"
	"golang.org/x/net/context"
	"net"
	"regexp"
	"strconv"
	"strings"
)
type DockerManager struct {
	config *utils.Config
	list   servers.ServiceListProvider
	client *client.Client
	cancel context.CancelFunc
}
func NewDockerManager(c *utils.Config, list servers.ServiceListProvider, tlsConfig *tls.Config) (*DockerManager, error) {
	defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
	dclient, err := client.NewClient(c.DockerHost, "v1.22", nil, defaultHeaders)
	if err != nil {
		return nil, err
	}
	return &DockerManager{config: c, list: list, client: dclient}, nil
}