mirror of https://github.com/docker/docs.git
Merge pull request #3029 from pnasrat/container-refactoring
Container refactoring
This commit is contained in:
commit
61aad8fc10
379
container.go
379
container.go
|
@ -541,196 +541,19 @@ func (container *Container) Start() (err error) {
|
||||||
log.Printf("WARNING: IPv4 forwarding is disabled. Networking will not work")
|
log.Printf("WARNING: IPv4 forwarding is disabled. Networking will not work")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the requested bind mounts
|
|
||||||
binds := make(map[string]BindMap)
|
|
||||||
// Define illegal container destinations
|
|
||||||
illegalDsts := []string{"/", "."}
|
|
||||||
|
|
||||||
for _, bind := range container.hostConfig.Binds {
|
|
||||||
// FIXME: factorize bind parsing in parseBind
|
|
||||||
var src, dst, mode string
|
|
||||||
arr := strings.Split(bind, ":")
|
|
||||||
if len(arr) == 2 {
|
|
||||||
src = arr[0]
|
|
||||||
dst = arr[1]
|
|
||||||
mode = "rw"
|
|
||||||
} else if len(arr) == 3 {
|
|
||||||
src = arr[0]
|
|
||||||
dst = arr[1]
|
|
||||||
mode = arr[2]
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("Invalid bind specification: %s", bind)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bail if trying to mount to an illegal destination
|
|
||||||
for _, illegal := range illegalDsts {
|
|
||||||
if dst == illegal {
|
|
||||||
return fmt.Errorf("Illegal bind destination: %s", dst)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bindMap := BindMap{
|
|
||||||
SrcPath: src,
|
|
||||||
DstPath: dst,
|
|
||||||
Mode: mode,
|
|
||||||
}
|
|
||||||
binds[path.Clean(dst)] = bindMap
|
|
||||||
}
|
|
||||||
|
|
||||||
if container.Volumes == nil || len(container.Volumes) == 0 {
|
if container.Volumes == nil || len(container.Volumes) == 0 {
|
||||||
container.Volumes = make(map[string]string)
|
container.Volumes = make(map[string]string)
|
||||||
container.VolumesRW = make(map[string]bool)
|
container.VolumesRW = make(map[string]bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply volumes from another container if requested
|
// Apply volumes from another container if requested
|
||||||
if container.Config.VolumesFrom != "" {
|
if err := container.applyExternalVolumes(); err != nil {
|
||||||
containerSpecs := strings.Split(container.Config.VolumesFrom, ",")
|
|
||||||
for _, containerSpec := range containerSpecs {
|
|
||||||
mountRW := true
|
|
||||||
specParts := strings.SplitN(containerSpec, ":", 2)
|
|
||||||
switch len(specParts) {
|
|
||||||
case 0:
|
|
||||||
return fmt.Errorf("Malformed volumes-from specification: %s", container.Config.VolumesFrom)
|
|
||||||
case 2:
|
|
||||||
switch specParts[1] {
|
|
||||||
case "ro":
|
|
||||||
mountRW = false
|
|
||||||
case "rw": // mountRW is already true
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("Malformed volumes-from speficication: %s", containerSpec)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c := container.runtime.Get(specParts[0])
|
|
||||||
if c == nil {
|
|
||||||
return fmt.Errorf("Container %s not found. Impossible to mount its volumes", container.ID)
|
|
||||||
}
|
|
||||||
for volPath, id := range c.Volumes {
|
|
||||||
if _, exists := container.Volumes[volPath]; exists {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := os.MkdirAll(path.Join(container.RootfsPath(), volPath), 0755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
container.Volumes[volPath] = id
|
|
||||||
if isRW, exists := c.VolumesRW[volPath]; exists {
|
|
||||||
container.VolumesRW[volPath] = isRW && mountRW
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
volumesDriver := container.runtime.volumes.driver
|
|
||||||
// Create the requested volumes if they don't exist
|
|
||||||
for volPath := range container.Config.Volumes {
|
|
||||||
volPath = path.Clean(volPath)
|
|
||||||
volIsDir := true
|
|
||||||
// Skip existing volumes
|
|
||||||
if _, exists := container.Volumes[volPath]; exists {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var srcPath string
|
|
||||||
var isBindMount bool
|
|
||||||
srcRW := false
|
|
||||||
// If an external bind is defined for this volume, use that as a source
|
|
||||||
if bindMap, exists := binds[volPath]; exists {
|
|
||||||
isBindMount = true
|
|
||||||
srcPath = bindMap.SrcPath
|
|
||||||
if strings.ToLower(bindMap.Mode) == "rw" {
|
|
||||||
srcRW = true
|
|
||||||
}
|
|
||||||
if file, err := os.Open(bindMap.SrcPath); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
defer file.Close()
|
|
||||||
if stat, err := file.Stat(); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
volIsDir = stat.IsDir()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Otherwise create an directory in $ROOT/volumes/ and use that
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// Do not pass a container as the parameter for the volume creation.
|
|
||||||
// The graph driver using the container's information ( Image ) to
|
|
||||||
// create the parent.
|
|
||||||
c, err := container.runtime.volumes.Create(nil, nil, "", "", nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
srcPath, err = volumesDriver.Get(c.ID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Driver %s failed to get volume rootfs %s: %s", volumesDriver, c.ID, err)
|
|
||||||
}
|
|
||||||
srcRW = true // RW by default
|
|
||||||
}
|
|
||||||
container.Volumes[volPath] = srcPath
|
|
||||||
container.VolumesRW[volPath] = srcRW
|
|
||||||
// Create the mountpoint
|
|
||||||
rootVolPath := path.Join(container.RootfsPath(), volPath)
|
|
||||||
if volIsDir {
|
|
||||||
if err := os.MkdirAll(rootVolPath, 0755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
volPath = path.Join(container.RootfsPath(), volPath)
|
|
||||||
if _, err := os.Stat(volPath); err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
if volIsDir {
|
|
||||||
if err := os.MkdirAll(volPath, 0755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err := os.MkdirAll(path.Dir(volPath), 0755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if f, err := os.OpenFile(volPath, os.O_CREATE, 0755); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
f.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not copy or change permissions if we are mounting from the host
|
|
||||||
if srcRW && !isBindMount {
|
|
||||||
volList, err := ioutil.ReadDir(rootVolPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(volList) > 0 {
|
|
||||||
srcList, err := ioutil.ReadDir(srcPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(srcList) == 0 {
|
|
||||||
// If the source volume is empty copy files from the root into the volume
|
|
||||||
if err := archive.CopyWithTar(rootVolPath, srcPath); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var stat syscall.Stat_t
|
if err := container.createVolumes(); err != nil {
|
||||||
if err := syscall.Stat(rootVolPath, &stat); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var srcStat syscall.Stat_t
|
|
||||||
if err := syscall.Stat(srcPath, &srcStat); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Change the source volume's ownership if it differs from the root
|
|
||||||
// files that where just copied
|
|
||||||
if stat.Uid != srcStat.Uid || stat.Gid != srcStat.Gid {
|
|
||||||
if err := os.Chown(srcPath, int(stat.Uid), int(stat.Gid)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := container.generateLXCConfig(); err != nil {
|
if err := container.generateLXCConfig(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -915,6 +738,204 @@ func (container *Container) Start() (err error) {
|
||||||
return ErrContainerStart
|
return ErrContainerStart
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (container *Container) getBindMap() (map[string]BindMap, error) {
|
||||||
|
// Create the requested bind mounts
|
||||||
|
binds := make(map[string]BindMap)
|
||||||
|
// Define illegal container destinations
|
||||||
|
illegalDsts := []string{"/", "."}
|
||||||
|
|
||||||
|
for _, bind := range container.hostConfig.Binds {
|
||||||
|
// FIXME: factorize bind parsing in parseBind
|
||||||
|
var src, dst, mode string
|
||||||
|
arr := strings.Split(bind, ":")
|
||||||
|
if len(arr) == 2 {
|
||||||
|
src = arr[0]
|
||||||
|
dst = arr[1]
|
||||||
|
mode = "rw"
|
||||||
|
} else if len(arr) == 3 {
|
||||||
|
src = arr[0]
|
||||||
|
dst = arr[1]
|
||||||
|
mode = arr[2]
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("Invalid bind specification: %s", bind)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bail if trying to mount to an illegal destination
|
||||||
|
for _, illegal := range illegalDsts {
|
||||||
|
if dst == illegal {
|
||||||
|
return nil, fmt.Errorf("Illegal bind destination: %s", dst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bindMap := BindMap{
|
||||||
|
SrcPath: src,
|
||||||
|
DstPath: dst,
|
||||||
|
Mode: mode,
|
||||||
|
}
|
||||||
|
binds[path.Clean(dst)] = bindMap
|
||||||
|
}
|
||||||
|
return binds, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (container *Container) createVolumes() error {
|
||||||
|
binds, err := container.getBindMap()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
volumesDriver := container.runtime.volumes.driver
|
||||||
|
// Create the requested volumes if they don't exist
|
||||||
|
for volPath := range container.Config.Volumes {
|
||||||
|
volPath = path.Clean(volPath)
|
||||||
|
volIsDir := true
|
||||||
|
// Skip existing volumes
|
||||||
|
if _, exists := container.Volumes[volPath]; exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var srcPath string
|
||||||
|
var isBindMount bool
|
||||||
|
srcRW := false
|
||||||
|
// If an external bind is defined for this volume, use that as a source
|
||||||
|
if bindMap, exists := binds[volPath]; exists {
|
||||||
|
isBindMount = true
|
||||||
|
srcPath = bindMap.SrcPath
|
||||||
|
if strings.ToLower(bindMap.Mode) == "rw" {
|
||||||
|
srcRW = true
|
||||||
|
}
|
||||||
|
if file, err := os.Open(bindMap.SrcPath); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
defer file.Close()
|
||||||
|
if stat, err := file.Stat(); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
volIsDir = stat.IsDir()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Otherwise create an directory in $ROOT/volumes/ and use that
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// Do not pass a container as the parameter for the volume creation.
|
||||||
|
// The graph driver using the container's information ( Image ) to
|
||||||
|
// create the parent.
|
||||||
|
c, err := container.runtime.volumes.Create(nil, nil, "", "", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
srcPath, err = volumesDriver.Get(c.ID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Driver %s failed to get volume rootfs %s: %s", volumesDriver, c.ID, err)
|
||||||
|
}
|
||||||
|
srcRW = true // RW by default
|
||||||
|
}
|
||||||
|
container.Volumes[volPath] = srcPath
|
||||||
|
container.VolumesRW[volPath] = srcRW
|
||||||
|
// Create the mountpoint
|
||||||
|
rootVolPath := path.Join(container.RootfsPath(), volPath)
|
||||||
|
if volIsDir {
|
||||||
|
if err := os.MkdirAll(rootVolPath, 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
volPath = path.Join(container.RootfsPath(), volPath)
|
||||||
|
if _, err := os.Stat(volPath); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
if volIsDir {
|
||||||
|
if err := os.MkdirAll(volPath, 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := os.MkdirAll(path.Dir(volPath), 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if f, err := os.OpenFile(volPath, os.O_CREATE, 0755); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do not copy or change permissions if we are mounting from the host
|
||||||
|
if srcRW && !isBindMount {
|
||||||
|
volList, err := ioutil.ReadDir(rootVolPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(volList) > 0 {
|
||||||
|
srcList, err := ioutil.ReadDir(srcPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(srcList) == 0 {
|
||||||
|
// If the source volume is empty copy files from the root into the volume
|
||||||
|
if err := archive.CopyWithTar(rootVolPath, srcPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var stat syscall.Stat_t
|
||||||
|
if err := syscall.Stat(rootVolPath, &stat); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var srcStat syscall.Stat_t
|
||||||
|
if err := syscall.Stat(srcPath, &srcStat); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Change the source volume's ownership if it differs from the root
|
||||||
|
// files that where just copied
|
||||||
|
if stat.Uid != srcStat.Uid || stat.Gid != srcStat.Gid {
|
||||||
|
if err := os.Chown(srcPath, int(stat.Uid), int(stat.Gid)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (container *Container) applyExternalVolumes() error {
|
||||||
|
if container.Config.VolumesFrom != "" {
|
||||||
|
containerSpecs := strings.Split(container.Config.VolumesFrom, ",")
|
||||||
|
for _, containerSpec := range containerSpecs {
|
||||||
|
mountRW := true
|
||||||
|
specParts := strings.SplitN(containerSpec, ":", 2)
|
||||||
|
switch len(specParts) {
|
||||||
|
case 0:
|
||||||
|
return fmt.Errorf("Malformed volumes-from specification: %s", container.Config.VolumesFrom)
|
||||||
|
case 2:
|
||||||
|
switch specParts[1] {
|
||||||
|
case "ro":
|
||||||
|
mountRW = false
|
||||||
|
case "rw": // mountRW is already true
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("Malformed volumes-from speficication: %s", containerSpec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c := container.runtime.Get(specParts[0])
|
||||||
|
if c == nil {
|
||||||
|
return fmt.Errorf("Container %s not found. Impossible to mount its volumes", container.ID)
|
||||||
|
}
|
||||||
|
for volPath, id := range c.Volumes {
|
||||||
|
if _, exists := container.Volumes[volPath]; exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := os.MkdirAll(path.Join(container.RootfsPath(), volPath), 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
container.Volumes[volPath] = id
|
||||||
|
if isRW, exists := c.VolumesRW[volPath]; exists {
|
||||||
|
container.VolumesRW[volPath] = isRW && mountRW
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (container *Container) Run() error {
|
func (container *Container) Run() error {
|
||||||
if err := container.Start(); err != nil {
|
if err := container.Start(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue