Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit ff8b586

Browse files
committed
Merge pull request #184 from vdemeester/60-fix-volumes_from
Fix the `volumes_from` behaviour
2 parents c12bd12 + 2e74689 commit ff8b586

File tree

4 files changed

+76
-20
lines changed

4 files changed

+76
-20
lines changed

docker/convert.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package docker
22

33
import (
4+
"fmt"
45
"strings"
56

67
"github.com/docker/docker/runconfig/opts"
@@ -122,6 +123,14 @@ func Convert(c *project.ServiceConfig, ctx project.Context) (*container.Config,
122123
return nil, nil, err
123124
}
124125

126+
var volumesFrom []string
127+
if c.VolumesFrom != nil {
128+
volumesFrom, err = getVolumesFrom(c.VolumesFrom, ctx.Project.Configs, ctx.ProjectName)
129+
if err != nil {
130+
return nil, nil, err
131+
}
132+
}
133+
125134
config := &container.Config{
126135
Entrypoint: strslice.StrSlice(utils.CopySlice(c.Entrypoint.Slice())),
127136
Hostname: c.Hostname,
@@ -162,7 +171,7 @@ func Convert(c *project.ServiceConfig, ctx project.Context) (*container.Config,
162171
}
163172

164173
hostConfig := &container.HostConfig{
165-
VolumesFrom: utils.CopySlice(c.VolumesFrom),
174+
VolumesFrom: volumesFrom,
166175
CapAdd: strslice.StrSlice(utils.CopySlice(c.CapAdd)),
167176
CapDrop: strslice.StrSlice(utils.CopySlice(c.CapDrop)),
168177
ExtraHosts: utils.CopySlice(c.ExtraHosts),
@@ -189,6 +198,20 @@ func Convert(c *project.ServiceConfig, ctx project.Context) (*container.Config,
189198
return config, hostConfig, nil
190199
}
191200

201+
func getVolumesFrom(volumesFrom []string, services map[string]*project.ServiceConfig, projectName string) ([]string, error) {
202+
volumes := []string{}
203+
for _, volumeFrom := range volumesFrom {
204+
if _, ok := services[volumeFrom]; ok {
205+
// It's a service - Use the first one
206+
name := fmt.Sprintf("%s_%s_1", projectName, volumeFrom)
207+
volumes = append(volumes, name)
208+
} else {
209+
volumes = append(volumes, volumeFrom)
210+
}
211+
}
212+
return volumes, nil
213+
}
214+
192215
func parseDevices(devices []string) ([]container.DeviceMapping, error) {
193216
// parse device mappings
194217
deviceMappings := []container.DeviceMapping{}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
first:
2+
image: busybox
3+
volumes:
4+
- /bundle
5+
second:
6+
image: busybox
7+
volumes_from:
8+
- first
9+

integration/up_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package integration
33
import (
44
"fmt"
55
"os/exec"
6-
"path/filepath"
76
"strings"
87

98
"golang.org/x/net/context"
@@ -299,24 +298,6 @@ func (s *RunSuite) TestLink(c *C) {
299298
}))
300299
}
301300

302-
func (s *RunSuite) TestRelativeVolume(c *C) {
303-
p := s.ProjectFromText(c, "up", `
304-
server:
305-
image: busybox
306-
volumes:
307-
- .:/path
308-
`)
309-
310-
absPath, err := filepath.Abs(".")
311-
c.Assert(err, IsNil)
312-
serverName := fmt.Sprintf("%s_%s_1", p, "server")
313-
cn := s.GetContainerByName(c, serverName)
314-
315-
c.Assert(cn, NotNil)
316-
c.Assert(len(cn.Mounts), DeepEquals, 1)
317-
c.Assert(cn.Mounts[0].Source, DeepEquals, absPath)
318-
}
319-
320301
func (s *RunSuite) TestUpNoBuildFailIfImageNotPresent(c *C) {
321302
p := s.RandomProject()
322303
cmd := exec.Command(s.command, "-f", "./assets/build/docker-compose.yml", "-p", p, "up", "--no-build")

integration/volume_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package integration
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"path/filepath"
7+
8+
. "gopkg.in/check.v1"
9+
)
10+
11+
func (s *RunSuite) TestVolumeFromService(c *C) {
12+
p := s.RandomProject()
13+
cmd := exec.Command(s.command, "-f", "./assets/regression/60-volume_from.yml", "-p", p, "create")
14+
err := cmd.Run()
15+
c.Assert(err, IsNil)
16+
17+
volumeFromContainer := fmt.Sprintf("%s_%s_1", p, "first")
18+
secondContainerName := p + "_second_1"
19+
20+
cn := s.GetContainerByName(c, secondContainerName)
21+
c.Assert(cn, NotNil)
22+
23+
c.Assert(len(cn.HostConfig.VolumesFrom), Equals, 1)
24+
c.Assert(cn.HostConfig.VolumesFrom[0], Equals, volumeFromContainer)
25+
}
26+
27+
func (s *RunSuite) TestRelativeVolume(c *C) {
28+
p := s.ProjectFromText(c, "up", `
29+
server:
30+
image: busybox
31+
volumes:
32+
- .:/path
33+
`)
34+
35+
absPath, err := filepath.Abs(".")
36+
c.Assert(err, IsNil)
37+
serverName := fmt.Sprintf("%s_%s_1", p, "server")
38+
cn := s.GetContainerByName(c, serverName)
39+
40+
c.Assert(cn, NotNil)
41+
c.Assert(len(cn.Mounts), DeepEquals, 1)
42+
c.Assert(cn.Mounts[0].Source, DeepEquals, absPath)
43+
}

0 commit comments

Comments
 (0)