dockerfile: enable tmpfs mounts on run —mount

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
docker-18.09
Tonis Tiigi 2018-06-08 13:42:38 -07:00
parent 596b03a8a0
commit 6f569455d0
3 changed files with 27 additions and 12 deletions

View File

@ -16,11 +16,11 @@ func detectRunMount(cmd *command, dispatchStatesByName map[string]*dispatchState
mounts := instructions.GetMounts(c) mounts := instructions.GetMounts(c)
sources := make([]*dispatchState, len(mounts)) sources := make([]*dispatchState, len(mounts))
for i, mount := range mounts { for i, mount := range mounts {
if mount.From == "" && mount.Type == "cache" { if mount.From == "" && mount.Type == instructions.MountTypeCache {
mount.From = emptyImageName mount.From = emptyImageName
} }
from := mount.From from := mount.From
if from == "" { if from == "" || mount.Type == instructions.MountTypeTmpfs {
continue continue
} }
stn, ok := dispatchStatesByName[strings.ToLower(from)] stn, ok := dispatchStatesByName[strings.ToLower(from)]
@ -45,7 +45,7 @@ func dispatchRunMounts(d *dispatchState, c *instructions.RunCommand, sources []*
mounts := instructions.GetMounts(c) mounts := instructions.GetMounts(c)
for i, mount := range mounts { for i, mount := range mounts {
if mount.From == "" && mount.Type == "cache" { if mount.From == "" && mount.Type == instructions.MountTypeCache {
mount.From = emptyImageName mount.From = emptyImageName
} }
st := opt.buildContext st := opt.buildContext
@ -53,10 +53,14 @@ func dispatchRunMounts(d *dispatchState, c *instructions.RunCommand, sources []*
st = sources[i].state st = sources[i].state
} }
var mountOpts []llb.MountOption var mountOpts []llb.MountOption
if mount.Type == instructions.MountTypeTmpfs {
st = llb.Scratch()
mountOpts = append(mountOpts, llb.Tmpfs())
}
if mount.ReadOnly { if mount.ReadOnly {
mountOpts = append(mountOpts, llb.Readonly) mountOpts = append(mountOpts, llb.Readonly)
} }
if mount.Type == "cache" { if mount.Type == instructions.MountTypeCache {
mountOpts = append(mountOpts, llb.AsPersistentCacheDir(opt.cacheIDNamespace+"/"+mount.CacheID)) mountOpts = append(mountOpts, llb.AsPersistentCacheDir(opt.cacheIDNamespace+"/"+mount.CacheID))
} }
if src := path.Join("/", mount.Source); src != "/" { if src := path.Join("/", mount.Source); src != "/" {

View File

@ -10,6 +10,16 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
const MountTypeBind = "bind"
const MountTypeCache = "cache"
const MountTypeTmpfs = "tmpfs"
var allowedMountTypes = map[string]struct{}{
MountTypeBind: {},
MountTypeCache: {},
MountTypeTmpfs: {},
}
type mountsKeyT string type mountsKeyT string
var mountsKey = mountsKeyT("dockerfile/run/mounts") var mountsKey = mountsKeyT("dockerfile/run/mounts")
@ -19,6 +29,11 @@ func init() {
parseRunPostHooks = append(parseRunPostHooks, runMountPostHook) parseRunPostHooks = append(parseRunPostHooks, runMountPostHook)
} }
func isValidMountType(s string) bool {
_, ok := allowedMountTypes[s]
return ok
}
func runMountPreHook(cmd *RunCommand, req parseRequest) error { func runMountPreHook(cmd *RunCommand, req parseRequest) error {
st := &mountState{} st := &mountState{}
st.flag = req.flags.AddStrings("mount") st.flag = req.flags.AddStrings("mount")
@ -76,7 +91,7 @@ func parseMount(value string) (*Mount, error) {
return nil, errors.Wrap(err, "failed to parse csv mounts") return nil, errors.Wrap(err, "failed to parse csv mounts")
} }
m := &Mount{Type: "bind"} m := &Mount{Type: MountTypeBind}
roAuto := true roAuto := true
@ -104,11 +119,7 @@ func parseMount(value string) (*Mount, error) {
value := parts[1] value := parts[1]
switch key { switch key {
case "type": case "type":
allowedTypes := map[string]struct{}{ if !isValidMountType(strings.ToLower(value)) {
"cache": {},
"bind": {},
}
if _, ok := allowedTypes[strings.ToLower(value)]; !ok {
return nil, errors.Errorf("invalid mount type %q", value) return nil, errors.Errorf("invalid mount type %q", value)
} }
m.Type = strings.ToLower(value) m.Type = strings.ToLower(value)
@ -139,7 +150,7 @@ func parseMount(value string) (*Mount, error) {
} }
if roAuto { if roAuto {
if m.Type == "cache" { if m.Type == MountTypeCache {
m.ReadOnly = false m.ReadOnly = false
} else { } else {
m.ReadOnly = true m.ReadOnly = true

View File

@ -401,7 +401,7 @@ func (m *tmpfsMount) Mount() ([]mount.Mount, error) {
} }
return []mount.Mount{{ return []mount.Mount{{
Type: "tmpfs", Type: "tmpfs",
Source: "none", Source: "tmpfs",
Options: opt, Options: opt,
}}, nil }}, nil
} }