@@ -16,12 +16,9 @@ import (
1616)
1717
1818type DockerComposeYaml struct {
19- Services ComposeAppService
19+ Services map [ string ] ComposeService
2020 Volumes map [string ]* interface {}
2121}
22- type ComposeAppService struct {
23- App ComposeService
24- }
2522type ComposeService struct {
2623 Image string
2724 Build ComposeBuild
@@ -38,14 +35,7 @@ type ComposeBuild struct {
3835 No_Cache bool
3936}
4037
41- func WriteDockerCompose (config config.Config , dir string , bakeEnv bool ) error {
42- if err := WriteEnvConfig (config , dir ); err != nil {
43- return err
44- }
45- pupsArgs := "--skip-tags=precompile,migrate,db"
46- if err := WriteDockerfile (config , dir , pupsArgs , bakeEnv ); err != nil {
47- return err
48- }
38+ func DockerComposeService (config config.Config ) ComposeService {
4939 labels := map [string ]string {}
5040 for k , v := range config .Labels {
5141 labels [k ] = v
@@ -64,14 +54,8 @@ func WriteDockerCompose(config config.Config, dir string, bakeEnv bool) error {
6454 }
6555 slices .Sort (links )
6656 volumes := []string {}
67- composeVolumes := map [string ]* interface {}{}
6857 for _ , v := range config .Volumes {
6958 volumes = append (volumes , v .Volume .Host + ":" + v .Volume .Guest )
70- // if this is a docker volume (vs a bind mount), add to global volume list
71- matched , _ := regexp .MatchString (`^[A-Za-z]` , v .Volume .Host )
72- if matched {
73- composeVolumes [v .Volume .Host ] = nil
74- }
7559 }
7660 slices .Sort (volumes )
7761 ports := []string {}
@@ -85,23 +69,49 @@ func WriteDockerCompose(config config.Config, dir string, bakeEnv bool) error {
8569 args = append (args , k )
8670 }
8771 slices .Sort (args )
88- compose := & DockerComposeYaml {
89- Services : ComposeAppService {
90- App : ComposeService {
91- Image : utils .DefaultNamespace + "/" + config .Name ,
92- Build : ComposeBuild {
93- Dockerfile : "./" + config .Name + ".dockerfile" ,
94- Labels : labels ,
95- Shm_Size : "512m" ,
96- Args : args ,
97- No_Cache : true ,
98- },
99- Environment : env ,
100- Links : links ,
101- Volumes : volumes ,
102- Ports : ports ,
103- },
72+
73+ return ComposeService {
74+ Image : utils .DefaultNamespace + "/" + config .Name ,
75+ Build : ComposeBuild {
76+ Dockerfile : "./" + config .Name + ".dockerfile" ,
77+ Labels : labels ,
78+ Shm_Size : "512m" ,
79+ Args : args ,
80+ No_Cache : true ,
10481 },
82+ Environment : env ,
83+ Links : links ,
84+ Volumes : volumes ,
85+ Ports : ports ,
86+ }
87+ }
88+
89+ func WriteDockerCompose (configs []config.Config , dir string , bakeEnv bool ) error {
90+ //TODO: env config should be a merge of all env across all configs.
91+ if err := WriteEnvConfig (configs [0 ], dir ); err != nil {
92+ return err
93+ }
94+ pupsArgs := "--skip-tags=precompile,migrate,db"
95+
96+ composeServices := map [string ]ComposeService {}
97+ composeVolumes := map [string ]* interface {}{}
98+ for _ , config := range configs {
99+ if err := WriteDockerfile (config , dir , pupsArgs , bakeEnv ); err != nil {
100+ return err
101+ }
102+ composeServices [config .Name ] = DockerComposeService (config )
103+
104+ for _ , v := range config .Volumes {
105+ // if this is a docker volume (vs a bind mount), add to global volume list
106+ matched , _ := regexp .MatchString (`^[A-Za-z]` , v .Volume .Host )
107+ if matched {
108+ composeVolumes [v .Volume .Host ] = nil
109+ }
110+ }
111+ }
112+
113+ compose := & DockerComposeYaml {
114+ Services : composeServices ,
105115 Volumes : composeVolumes ,
106116 }
107117
@@ -113,6 +123,7 @@ func WriteDockerCompose(config config.Config, dir string, bakeEnv bool) error {
113123 if err != nil {
114124 return errors .New ("error marshalling compose file to write docker-compose.yaml" )
115125 }
126+ // TODO: docker-compose name ???
116127 if err := os .WriteFile (strings .TrimRight (dir , "/" )+ "/" + "docker-compose.yaml" , yaml , 0660 ); err != nil {
117128 return errors .New ("error writing compose file docker-compose.yaml" )
118129 }
@@ -150,7 +161,7 @@ func ExportEnv(config config.Config) string {
150161 return strings .Join (builder , "\n " )
151162}
152163
153- //TODO: main docker-compose should include secondary docker-compose files inside it somehow.
164+ //TODO: docker-compose should include all services... somehow.
154165type DockerComposeCmd struct {
155166 OutputDir string `name:"output dir" default:"./compose" short:"o" help:"Output dir for docker compose files." predictor:"dir"`
156167 BakeEnv bool `short:"e" help:"Bake in the configured environment to image after build."`
@@ -168,16 +179,16 @@ func (r *DockerComposeCmd) Run(cli *Cli, ctx *context.Context) error {
168179 return err
169180 }
170181
171- configs := []* config.Config {}
182+ configs := []config.Config {}
172183 for _ , configName := range (r .Config ) {
173184 config , err := config .LoadConfig (cli .ConfDir , configName , true , cli .TemplatesDir )
174185 if err != nil {
175186 return errors .New ("YAML syntax error. Please check your containers/*.yml config files." )
176187 }
177- if err := WriteDockerCompose (* config , dir , r .BakeEnv ); err != nil {
188+ configs = append (configs , * config )
189+ }
190+ if err := WriteDockerCompose (configs , dir , r .BakeEnv ); err != nil {
178191 return err
179192 }
180- configs = append (configs , config )
181- }
182193 return nil
183194}
0 commit comments