func (c *Container) Hostname() string {
	return c.hostname
}
Hostname gets hostname to access Docker registry we run inside our container
package container
import (
	"bufio"
	crand "crypto/rand"
	"fmt"
	"io"
	"math/rand"
	"net/http"
	"time"
	log "github.com/sirupsen/logrus"
	dockerclient "github.com/ivanilves/lstags/docker/client"
	dockerconfig "github.com/ivanilves/lstags/docker/config"
	"github.com/ivanilves/lstags/repository"
	"github.com/ivanilves/lstags/util/getenv"
	"github.com/ivanilves/lstags/util/wait"
)
const (
	imageRef   = "registry:2"
	baseName   = "registry"
	basePort   = 5000
	retryCount = 10
)
type Container struct {
	id           string
	hostname     string
	dockerClient *dockerclient.DockerClient
}
func getRandomPort() int {
	rand.Seed(time.Now().UnixNano())
	b := make([]byte, 1)
	crand.Read(b)
	return basePort + int(b[0]) + rand.Intn(400)
}
func getDockerClient() (*dockerclient.DockerClient, error) {
	dockerConfig, _ := dockerconfig.Load(dockerconfig.DefaultDockerJSON)
	return dockerclient.New(dockerConfig)
}
func getHostname(port int) string {
	return fmt.Sprintf("%s:%d", getenv.String("LOCAL_REGISTRY", "127.0.0.1"), port)
}
func run(dockerClient *dockerclient.DockerClient, hostPort int) (string, error) {
	portSpec := fmt.Sprintf("0.0.0.0:%d:%d", hostPort, basePort)
	name := fmt.Sprintf("%s-%d", baseName, hostPort)
	id, err := dockerClient.Run(imageRef, name, []string{portSpec})
	if err != nil {
		return "", err
	}
	return id, nil
}
func verify(hostname string) error {
	url := fmt.Sprintf("http://%s/v2/", hostname)
	var err error
	for retry := 0; retry < retryCount; retry++ {
		time.Sleep(1 * time.Second)
		_, err = http.Get(url)
		if err == nil {
			break
		}
	}
	return err
}
func Launch() (*Container, error) {
	dockerClient, _ := getDockerClient()
	hostPort := getRandomPort()
	id, _ := run(dockerClient, hostPort)
	hostname := getHostname(hostPort)
	return &Container{id: id, hostname: hostname, dockerClient: dockerClient}, verify(hostname)
}
func (c *Container) ID() string {
	return c.id
}