mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Server: -p option to export TCP ports
This commit is contained in:
parent
bd2f51290f
commit
f857fa0ddd
1 changed files with 37 additions and 18 deletions
|
@ -11,10 +11,11 @@ import (
|
||||||
"github.com/dotcloud/docker/image"
|
"github.com/dotcloud/docker/image"
|
||||||
"github.com/dotcloud/docker/rcli"
|
"github.com/dotcloud/docker/rcli"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
// "net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
@ -392,20 +393,20 @@ func (srv *Server) CmdPull(stdin io.ReadCloser, stdout io.Writer, args ...string
|
||||||
fmt.Fprintf(stdout, "Downloading from %s\n", u.String())
|
fmt.Fprintf(stdout, "Downloading from %s\n", u.String())
|
||||||
// Download with curl (pretty progress bar)
|
// Download with curl (pretty progress bar)
|
||||||
// If curl is not available, fallback to http.Get()
|
// If curl is not available, fallback to http.Get()
|
||||||
archive, err := future.Curl(u.String(), stdout)
|
// archive, err := future.Curl(u.String(), stdout)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
if resp, err := http.Get(u.String()); err != nil {
|
// if resp, err := http.Get(u.String()); err != nil {
|
||||||
return err
|
// return err
|
||||||
} else {
|
// } else {
|
||||||
archive = resp.Body
|
// archive = resp.Body
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
fmt.Fprintf(stdout, "Unpacking to %s\n", name)
|
// fmt.Fprintf(stdout, "Unpacking to %s\n", name)
|
||||||
img, err := srv.images.Import(name, archive, nil)
|
// img, err := srv.images.Import(name, archive, nil)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
fmt.Fprintln(stdout, img.Id)
|
// fmt.Fprintln(stdout, img.Id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -679,10 +680,10 @@ func (srv *Server) CmdLogs(stdin io.ReadCloser, stdout io.Writer, args ...string
|
||||||
return errors.New("No such container: " + cmd.Arg(0))
|
return errors.New("No such container: " + cmd.Arg(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) CreateContainer(img *image.Image, user string, tty bool, openStdin bool, comment string, cmd string, args ...string) (*docker.Container, error) {
|
func (srv *Server) CreateContainer(img *image.Image, ports []int, user string, tty bool, openStdin bool, comment string, cmd string, args ...string) (*docker.Container, error) {
|
||||||
id := future.RandomId()[:8]
|
id := future.RandomId()[:8]
|
||||||
container, err := srv.containers.Create(id, cmd, args, img.Layers,
|
container, err := srv.containers.Create(id, cmd, args, img.Layers,
|
||||||
&docker.Config{Hostname: id, User: user, Tty: tty, OpenStdin: openStdin})
|
&docker.Config{Hostname: id, Ports: ports, User: user, Tty: tty, OpenStdin: openStdin})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -743,6 +744,22 @@ func (srv *Server) CmdAttach(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ports type - Used to parse multiple -p flags
|
||||||
|
type ports []int
|
||||||
|
|
||||||
|
func (p *ports) String() string {
|
||||||
|
return fmt.Sprint(*p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ports) Set(value string) error {
|
||||||
|
port, err := strconv.Atoi(value)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Invalid port: %v", value)
|
||||||
|
}
|
||||||
|
*p = append(*p, port)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
||||||
cmd := rcli.Subcmd(stdout, "run", "[OPTIONS] IMAGE COMMAND [ARG...]", "Run a command in a new container")
|
cmd := rcli.Subcmd(stdout, "run", "[OPTIONS] IMAGE COMMAND [ARG...]", "Run a command in a new container")
|
||||||
fl_user := cmd.String("u", "", "Username or UID")
|
fl_user := cmd.String("u", "", "Username or UID")
|
||||||
|
@ -750,6 +767,8 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
||||||
fl_stdin := cmd.Bool("i", false, "Keep stdin open even if not attached")
|
fl_stdin := cmd.Bool("i", false, "Keep stdin open even if not attached")
|
||||||
fl_tty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
fl_tty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
||||||
fl_comment := cmd.String("c", "", "Comment")
|
fl_comment := cmd.String("c", "", "Comment")
|
||||||
|
var fl_ports ports
|
||||||
|
cmd.Var(&fl_ports, "p", "Map a network port to the container")
|
||||||
if err := cmd.Parse(args); err != nil {
|
if err := cmd.Parse(args); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -775,7 +794,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
||||||
return errors.New("No such image: " + name)
|
return errors.New("No such image: " + name)
|
||||||
}
|
}
|
||||||
// Create new container
|
// Create new container
|
||||||
container, err := srv.CreateContainer(img, *fl_user, *fl_tty, *fl_stdin, *fl_comment, cmdline[0], cmdline[1:]...)
|
container, err := srv.CreateContainer(img, fl_ports, *fl_user, *fl_tty, *fl_stdin, *fl_comment, cmdline[0], cmdline[1:]...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Error creating container: " + err.Error())
|
return errors.New("Error creating container: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue