Fixes #4393
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
| ... | ... |
@@ -184,6 +184,12 @@ func (container *Container) parseVolumeMountConfig() (map[string]*Mount, error) |
| 184 | 184 |
continue |
| 185 | 185 |
} |
| 186 | 186 |
|
| 187 |
+ if stat, err := os.Stat(filepath.Join(container.basefs, path)); err == nil {
|
|
| 188 |
+ if !stat.IsDir() {
|
|
| 189 |
+ return nil, fmt.Errorf("file exists at %s, can't create volume there")
|
|
| 190 |
+ } |
|
| 191 |
+ } |
|
| 192 |
+ |
|
| 187 | 193 |
vol, err := container.daemon.volumes.FindOrCreateVolume("", true)
|
| 188 | 194 |
if err != nil {
|
| 189 | 195 |
return nil, err |
| ... | ... |
@@ -4784,3 +4784,20 @@ RUN echo " \ |
| 4784 | 4784 |
|
| 4785 | 4785 |
logDone("build - test spaces with quotes")
|
| 4786 | 4786 |
} |
| 4787 |
+ |
|
| 4788 |
+// #4393 |
|
| 4789 |
+func TestBuildVolumeFileExistsinContainer(t *testing.T) {
|
|
| 4790 |
+ buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-errcreatevolumewithfile", "-") |
|
| 4791 |
+ buildCmd.Stdin = strings.NewReader(` |
|
| 4792 |
+ FROM busybox |
|
| 4793 |
+ RUN touch /foo |
|
| 4794 |
+ VOLUME /foo |
|
| 4795 |
+ `) |
|
| 4796 |
+ |
|
| 4797 |
+ out, _, err := runCommandWithOutput(buildCmd) |
|
| 4798 |
+ if err == nil || !strings.Contains(out, "file exists") {
|
|
| 4799 |
+ t.Fatalf("expected build to fail when file exists in container at requested volume path")
|
|
| 4800 |
+ } |
|
| 4801 |
+ |
|
| 4802 |
+ logDone("build - errors when volume is specified where a file exists")
|
|
| 4803 |
+} |