コード例 #1
0
func startDocker(docker *dockerclient.DockerClient, s2 string, s3 string, s4 string) {

	var dirpath string = "/tmp/shareDocker" + s3
	os.RemoveAll(dirpath)
	os.Mkdir(dirpath, 0777)
	var filepath string = dirpath + "/config.ini"

	//l4g.Info("startDocker -> %v", filepath)
	var RS string = fmt.Sprintf("startDocker -> %v", filepath)
	l4g.Info(RS)
	writeSDLstr(RS)

	f, _ := os.Create(filepath)
	f.WriteString("ip_broker=rabbitmq\n")
	f.Sync()
	f.WriteString("\n")
	f.Sync()
	f.WriteString(s2)
	f.Sync()
	f.WriteString("\n")
	defer f.Close()

	//l4g.Info("Init the client")
	RS = fmt.Sprintf("Init the client")
	l4g.Info(RS)
	writeSDLstr(RS)
	l4g.Info(os.Getenv("DOCKER_HOST"))

	//l4g.Info("DOCKER_HOST")
	RS = fmt.Sprintf("DOCKER_HOST")
	l4g.Info(RS)
	writeSDLstr(RS)

	var dockerName string = "docker" + s3

	supprDocker(docker, dockerName)

	//l4g.Info(" Create a container")
	RS = fmt.Sprintf(" Create a container")
	l4g.Info(RS)
	writeSDLstr(RS)
	containerConfig := &dockerclient.ContainerConfig{
		Image: "hwgrepo:hwg",
		User:  "******",
		//		Env: []string{"PATH=\/home\/developer\/sdl_sensor_broker\/"},
		Cmd:          []string{"sh"},
		AttachStdin:  false,
		AttachStdout: false,
		AttachStderr: false,
		Tty:          true,
		OpenStdin:    true,
		StdinOnce:    false,
	}
	containerId, err := docker.CreateContainer(containerConfig, dockerName)

	if err != nil {
		log.Fatal(err)
	}
	var port_tcp string = s3 + "/tcp"
	portBinding := map[string][]dockerclient.PortBinding{port_tcp: {{"", s3}}}

	var sharedDocker string = dirpath + ":/home/developer/sdl_sensor_broker/log"

	l4g.Info(" Start the container")
	hostConfig := &dockerclient.HostConfig{
		Binds:        []string{sharedDocker, "/tmp/.X11-unix:/tmp/.X11-unix:rw"},
		Privileged:   true,
		PortBindings: portBinding,
		Links:        []string{"rabbitmq:rabbitmq"},
	}
	err = docker.StartContainer(containerId, hostConfig)
	if err != nil {
		log.Fatal(err)
	}

	execConfig := &dockerclient.ExecConfig{
		AttachStdin:  false,
		AttachStdout: false,
		AttachStderr: false,
		Tty:          true,
		Cmd:          []string{"sh", "runNoVNCplayer.sh", s4, s3},
		Container:    containerId,
		Detach:       true}

	_, err = docker.Exec(execConfig)

	if err != nil {
		log.Fatal(err)
		l4g.Info(" error exec container")
		RS = fmt.Sprintf(" error exec container")
		l4g.Info(RS)
		writeSDLstr(RS)
	}

}