@@ -2,6 +2,7 @@ package build
22
33import (
44 "fmt"
5+ "path/filepath"
56 "reflect"
67 "strings"
78
@@ -15,8 +16,14 @@ import (
1516type ServiceConfig struct {
1617 Name string `yaml:"-" json:"-"`
1718
18- Build * ImageBuildConfig
19- Image * string
19+ Build * ImageBuildConfig
20+ Image * string
21+ Volumes []ServiceVolumeConfig `yaml:",omitempty" json:"volumes,omitempty"`
22+ }
23+
24+ type ServiceVolumeConfig struct {
25+ Type string `yaml:",omitempty" json:"type,omitempty"`
26+ Source string `yaml:",omitempty" json:"source,omitempty"`
2027}
2128
2229type ImageBuildConfig struct {
@@ -46,6 +53,13 @@ func loadServices(servicesDict map[string]interface{}, buildArgs []string) ([]Se
4653 return nil , err
4754 }
4855 services = append (services , * serviceConfig )
56+
57+ // Sanity check
58+ for _ , volume := range serviceConfig .Volumes {
59+ if volume .Type == "bind" && ! filepath .IsAbs (volume .Source ) {
60+ return nil , fmt .Errorf ("invalid service %q: can't use relative path as volume source" , name )
61+ }
62+ }
4963 }
5064 return services , nil
5165}
@@ -57,6 +71,9 @@ func loadService(name string, serviceDict map[string]interface{}, buildArgs []st
5771 if err := loader .Transform (serviceDict , serviceConfig , loader.Transformer {
5872 TypeOf : reflect .TypeOf (ImageBuildConfig {}),
5973 Func : transformBuildConfig ,
74+ }, loader.Transformer {
75+ TypeOf : reflect .TypeOf (ServiceVolumeConfig {}),
76+ Func : transformVolumeConfig ,
6077 }); err != nil {
6178 return nil , err
6279 }
@@ -77,6 +94,22 @@ func transformBuildConfig(data interface{}) (interface{}, error) {
7794 }
7895}
7996
97+ func transformVolumeConfig (data interface {}) (interface {}, error ) {
98+ switch value := data .(type ) {
99+ case string :
100+ spec := data .(string )
101+ volume , err := loader .ParseVolume (spec )
102+ if err != nil {
103+ return nil , err
104+ }
105+ return ServiceVolumeConfig {Type : volume .Type , Source : volume .Source }, nil
106+ case map [string ]interface {}:
107+ return data , nil
108+ default :
109+ return data , errors .Errorf ("invalid type %T for service volume" , value )
110+ }
111+ }
112+
80113func buildArgsToMap (array []string ) map [string ]string {
81114 result := make (map [string ]string )
82115 for _ , value := range array {
0 commit comments