本文整理汇总了Golang中github.com/samalba/dockerclient.Client.StopContainer方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.StopContainer方法的具体用法?Golang Client.StopContainer怎么用?Golang Client.StopContainer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/samalba/dockerclient.Client
的用法示例。
在下文中一共展示了Client.StopContainer方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Wait
// Wait blocks until the named container exits, returning the exit information.
func Wait(client dockerclient.Client, name string) (*dockerclient.ContainerInfo, error) {
defer func() {
client.StopContainer(name, 5)
client.KillContainer(name, "9")
}()
for attempts := 0; attempts < 5; attempts++ {
done := client.Wait(name)
<-done
info, err := client.InspectContainer(name)
if err != nil {
return nil, err
}
if !info.State.Running {
return info, nil
}
log.Debugf("attempting to resume waiting after %d attempts.\n", attempts)
}
return nil, errors.New("reached maximum wait attempts")
}
示例2: run
func run(client dockerclient.Client, args []string, input string) (int, error) {
image := "drone/drone-exec:latest"
entrypoint := []string{"/bin/drone-exec"}
args = append(args, "--", input)
conf := &dockerclient.ContainerConfig{
Image: image,
Entrypoint: entrypoint,
Cmd: args,
HostConfig: dockerclient.HostConfig{
Binds: []string{"/var/run/docker.sock:/var/run/docker.sock"},
},
Volumes: map[string]struct{}{
"/var/run/docker.sock": struct{}{},
},
}
info, err := docker.Run(client, conf, false)
if err != nil {
return 0, err
}
client.StopContainer(info.Id, 15)
client.RemoveContainer(info.Id, true, true)
return info.State.ExitCode, err
}
示例3: Stop
// Stop a container
func Stop(docker dockerclient.Client, id string) string {
// Stop the container (with 5 seconds timeout)
err := docker.StopContainer(id, 5)
if err != nil {
log.Fatal(err)
}
return "OK"
}
示例4: Run
func Run(client dockerclient.Client, conf *dockerclient.ContainerConfig, auth *dockerclient.AuthConfig, pull bool, outw, errw io.Writer) (*dockerclient.ContainerInfo, error) {
if outw == nil {
outw = os.Stdout
}
if errw == nil {
errw = os.Stdout
}
// fetches the container information.
info, err := Start(client, conf, auth, pull)
if err != nil {
return nil, err
}
// ensures the container is always stopped
// and ready to be removed.
defer func() {
client.StopContainer(info.Id, 5)
client.KillContainer(info.Id, "9")
}()
// channel listening for errors while the
// container is running async.
errc := make(chan error, 1)
infoc := make(chan *dockerclient.ContainerInfo, 1)
go func() {
// blocks and waits for the container to finish
// by streaming the logs (to /dev/null). Ideally
// we could use the `wait` function instead
rc, err := client.ContainerLogs(info.Id, logOptsTail)
if err != nil {
log.Errorf("Error tailing %s. %s\n", conf.Image, err)
errc <- err
return
}
defer rc.Close()
StdCopy(outw, errw, rc)
// fetches the container information
info, err := client.InspectContainer(info.Id)
if err != nil {
log.Errorf("Error getting exit code for %s. %s\n", conf.Image, err)
errc <- err
return
}
infoc <- info
}()
select {
case info := <-infoc:
return info, nil
case err := <-errc:
return info, err
}
}
示例5: Wait
// Wait blocks until the named container exits, returning the exit information.
func Wait(client dockerclient.Client, name string) (*dockerclient.ContainerInfo, error) {
defer func() {
client.StopContainer(name, 5)
client.KillContainer(name, "9")
}()
errc := make(chan error, 1)
infoc := make(chan *dockerclient.ContainerInfo, 1)
go func() {
// blocks and waits for the container to finish
// by streaming the logs (to /dev/null). Ideally
// we could use the `wait` function instead
rc, err := client.ContainerLogs(name, LogOptsTail)
if err != nil {
errc <- err
return
}
io.Copy(ioutil.Discard, rc)
rc.Close()
info, err := client.InspectContainer(name)
if err != nil {
errc <- err
return
}
infoc <- info
}()
select {
case info := <-infoc:
return info, nil
case err := <-errc:
return nil, err
}
}
示例6: Run
func Run(client dockerclient.Client, conf *dockerclient.ContainerConfig, auth *dockerclient.AuthConfig, pull bool, outw, errw io.Writer) (*dockerclient.ContainerInfo, error) {
if outw == nil {
outw = os.Stdout
}
if errw == nil {
errw = os.Stdout
}
// fetches the container information.
info, err := Start(client, conf, auth, pull)
if err != nil {
return nil, err
}
// ensures the container is always stopped
// and ready to be removed.
defer func() {
client.StopContainer(info.Id, 5)
client.KillContainer(info.Id, "9")
}()
// channel listening for errors while the
// container is running async.
errc := make(chan error, 1)
infoc := make(chan *dockerclient.ContainerInfo, 1)
go func() {
// options to fetch the stdout and stderr logs
// by tailing the output.
logOptsTail := &dockerclient.LogOptions{
Follow: true,
Stdout: true,
Stderr: true,
}
// It's possible that the docker logs endpoint returns before the container
// is done, we'll naively resume up to 5 times if when the logs unblocks
// the container is still reported to be running.
for attempts := 0; attempts < 5; attempts++ {
if attempts > 0 {
// When resuming the stream, only grab the last line when starting
// the tailing.
logOptsTail.Tail = 1
}
// blocks and waits for the container to finish
// by streaming the logs (to /dev/null). Ideally
// we could use the `wait` function instead
rc, err := client.ContainerLogs(info.Id, logOptsTail)
if err != nil {
log.Errorf("Error tailing %s. %s\n", conf.Image, err)
errc <- err
return
}
defer rc.Close()
_, err = StdCopy(outw, errw, rc)
if err != nil {
log.Errorf("Error streaming docker logs for %s. %s\n", conf.Image, err)
errc <- err
return
}
// fetches the container information
info, err := client.InspectContainer(info.Id)
if err != nil {
log.Errorf("Error getting exit code for %s. %s\n", conf.Image, err)
errc <- err
return
}
if !info.State.Running {
// The container is no longer running, there should be no more logs to tail.
infoc <- info
return
}
log.Debugf("Attempting to resume log tailing after %d attempts.\n", attempts)
}
errc <- errors.New("Maximum number of attempts made while tailing logs.")
}()
select {
case info := <-infoc:
return info, nil
case err := <-errc:
return info, err
}
}
示例7: Run
func Run(client dockerclient.Client, conf *dockerclient.ContainerConfig, auth *dockerclient.AuthConfig, pull bool, outw, errw io.Writer) (*dockerclient.ContainerInfo, error) {
if outw == nil {
outw = os.Stdout
}
if errw == nil {
errw = os.Stdout
}
// fetches the container information.
info, err := Start(client, conf, auth, pull)
if err != nil {
return nil, err
}
// ensures the container is always stopped
// and ready to be removed.
defer func() {
client.StopContainer(info.Id, 5)
client.KillContainer(info.Id, "9")
}()
// channel listening for errors while the
// container is running async.
errc := make(chan error, 1)
infoc := make(chan *dockerclient.ContainerInfo, 1)
go func() {
// It's possible that the docker logs endpoint returns before the container
// is done, we'll naively resume up to 5 times if when the logs unblocks
// the container is still reported to be running.
var total int64
for attempts := 0; attempts < 5; attempts++ {
// blocks and waits for the container to finish
// by streaming the logs (to /dev/null). Ideally
// we could use the `wait` function instead
rc, err := client.ContainerLogs(info.Id, logOptsTail)
if err != nil {
log.Errorf("Error tailing %s. %s\n", conf.Image, err)
errc <- err
return
}
defer rc.Close()
if total != 0 {
// Discard off the total bytes we've received so far.
// io.LimitReader returns EOF once it has read the specified number
// of bytes as per https://golang.org/pkg/io/#LimitReader.
r := io.LimitReader(rc, total)
_, err := io.Copy(ioutil.Discard, r)
if err != nil && err != io.EOF {
log.Errorf("Error resuming streaming docker logs for %s. %s\n", conf.Image, err)
errc <- err
return
}
}
rcv, err := StdCopy(outw, errw, rc)
if err != nil {
log.Errorf("Error streaming docker logs for %s. %s\n", conf.Image, err)
errc <- err
return
}
// fetches the container information
info, err := client.InspectContainer(info.Id)
if err != nil {
log.Errorf("Error getting exit code for %s. %s\n", conf.Image, err)
errc <- err
return
}
if !info.State.Running {
// The container is no longer running, there should be no more logs to tail.
infoc <- info
return
}
total += rcv
log.Debugf("Attempting to resume log tailing after receiving %d bytes. Attempts %d.\n", total, attempts)
}
errc <- errors.New("Maximum number of attempts made while tailing logs.")
}()
select {
case info := <-infoc:
return info, nil
case err := <-errc:
return info, err
}
}