github.com/robryk/drone@v0.2.1-0.20140602202253-40fe4305815d/pkg/build/docker/container.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  )
     7  
     8  type ContainerService struct {
     9  	*Client
    10  }
    11  
    12  // List only running containers.
    13  func (c *ContainerService) List() ([]*Containers, error) {
    14  	containers := []*Containers{}
    15  	err := c.do("GET", "/containers/json?all=0", nil, &containers)
    16  	return containers, err
    17  }
    18  
    19  // List all containers
    20  func (c *ContainerService) ListAll() ([]*Containers, error) {
    21  	containers := []*Containers{}
    22  	err := c.do("GET", "/containers/json?all=1", nil, &containers)
    23  	return containers, err
    24  }
    25  
    26  // Create a Container
    27  func (c *ContainerService) Create(conf *Config) (*Run, error) {
    28  	run, err := c.create(conf)
    29  	switch {
    30  	// if no error, exit immediately
    31  	case err == nil:
    32  		return run, nil
    33  	// if error we exit, unless it is
    34  	// a NOT FOUND error, which means we just
    35  	// need to download the Image from the center
    36  	// image index
    37  	case err != nil && err != ErrNotFound:
    38  		return nil, err
    39  	}
    40  
    41  	// attempt to pull the image
    42  	if err := c.Images.Pull(conf.Image); err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	// now that we have the image, re-try creation
    47  	return c.create(conf)
    48  }
    49  
    50  func (c *ContainerService) create(conf *Config) (*Run, error) {
    51  	run := Run{}
    52  	err := c.do("POST", "/containers/create", conf, &run)
    53  	return &run, err
    54  }
    55  
    56  // Start the container id
    57  func (c *ContainerService) Start(id string, conf *HostConfig) error {
    58  	return c.do("POST", fmt.Sprintf("/containers/%s/start", id), &conf, nil)
    59  }
    60  
    61  // Stop the container id
    62  func (c *ContainerService) Stop(id string, timeout int) error {
    63  	return c.do("POST", fmt.Sprintf("/containers/%s/stop?t=%v", id, timeout), nil, nil)
    64  }
    65  
    66  // Remove the container id from the filesystem.
    67  func (c *ContainerService) Remove(id string) error {
    68  	return c.do("DELETE", fmt.Sprintf("/containers/%s", id), nil, nil)
    69  }
    70  
    71  // Block until container id stops, then returns the exit code
    72  func (c *ContainerService) Wait(id string) (*Wait, error) {
    73  	wait := Wait{}
    74  	err := c.do("POST", fmt.Sprintf("/containers/%s/wait", id), nil, &wait)
    75  	return &wait, err
    76  }
    77  
    78  // Attach to the container to stream the stdout and stderr
    79  func (c *ContainerService) Attach(id string, out io.Writer) error {
    80  	path := fmt.Sprintf("/containers/%s/attach?&stream=1&stdout=1&stderr=1", id)
    81  	return c.hijack("POST", path, false, out)
    82  }
    83  
    84  // Stop the container id
    85  func (c *ContainerService) Inspect(id string) (*Container, error) {
    86  	container := Container{}
    87  	err := c.do("GET", fmt.Sprintf("/containers/%s/json", id), nil, &container)
    88  	return &container, err
    89  }
    90  
    91  // Run the container
    92  func (c *ContainerService) Run(conf *Config, host *HostConfig, out io.Writer) (*Wait, error) {
    93  	// create the container from the image
    94  	run, err := c.Create(conf)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	// attach to the container
   100  	go func() {
   101  		c.Attach(run.ID, out)
   102  	}()
   103  
   104  	// start the container
   105  	if err := c.Start(run.ID, host); err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	// wait for the container to stop
   110  	wait, err := c.Wait(run.ID)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	return wait, nil
   116  }
   117  
   118  // Run the container as a Daemon
   119  func (c *ContainerService) RunDaemon(conf *Config, host *HostConfig) (*Run, error) {
   120  	run, err := c.Create(conf)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	// start the container
   126  	err = c.Start(run.ID, host)
   127  	return run, err
   128  }
   129  
   130  func (c *ContainerService) RunDaemonPorts(image string, ports map[Port]struct{}) (*Run, error) {
   131  	// setup configuration
   132  	config := Config{Image: image}
   133  	config.ExposedPorts = ports
   134  
   135  	// host configuration
   136  	host := HostConfig{}
   137  	host.PortBindings = make(map[Port][]PortBinding)
   138  
   139  	// loop through and add ports
   140  	for port, _ := range ports {
   141  		host.PortBindings[port] = []PortBinding{{HostIp: "127.0.0.1", HostPort: ""}}
   142  	}
   143  	//127.0.0.1::%s
   144  	//map[3306/tcp:{}] map[3306/tcp:[{127.0.0.1 }]]
   145  	return c.RunDaemon(&config, &host)
   146  }