fix: respect DOKKU_LIB_HOST_ROOT for storage bind mounts
When Dokku runs in Docker with a shared socket, create/chown still use DOKKU_LIB_ROOT inside the container, but Docker -v flags for storage:exec and deploy must use DOKKU_LIB_HOST_ROOT so the daemon mounts the host data volume instead of the real host's /var/lib/dokku. Closes #9126.
This commit is contained in:
8 files changed
+103
-3
No files matched your search
@@ -3,6 +3,7 @@ package storage
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -190,6 +191,26 @@ func GetStorageDirectory() string {
|
||||
return fmt.Sprintf("%s/data/storage", dokkuLibRoot)
|
||||
}
|
||||
|
||||
// HostPathForDocker rewrites a path under DOKKU_LIB_ROOT to the
|
||||
// DOKKU_LIB_HOST_ROOT view used by the host Docker daemon. Named
|
||||
// volumes and paths outside DOKKU_LIB_ROOT are returned unchanged.
|
||||
//
|
||||
// When Dokku runs in a container with a shared Docker socket, filesystem
|
||||
// operations must use DOKKU_LIB_ROOT (visible inside the Dokku container)
|
||||
// while Docker -v host paths must use DOKKU_LIB_HOST_ROOT (visible to the
|
||||
// daemon on the real host).
|
||||
func HostPathForDocker(path string) string {
|
||||
libRoot := common.GetenvWithDefault("DOKKU_LIB_ROOT", "/var/lib/dokku")
|
||||
libHost := os.Getenv("DOKKU_LIB_HOST_ROOT")
|
||||
if libHost == "" || libHost == libRoot {
|
||||
return path
|
||||
}
|
||||
if path == libRoot || strings.HasPrefix(path, libRoot+"/") {
|
||||
return libHost + strings.TrimPrefix(path, libRoot)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// storageDirScriptNames lists the sudo helpers shipped in the plugin's bin
|
||||
// directory. Each one takes a storage entry basename and builds the path
|
||||
// under $DOKKU_LIB_ROOT/data/storage itself, so no caller can point them
|
||||
|
||||
@@ -107,3 +107,46 @@ func TestGetStorageDirectory(t *testing.T) {
|
||||
dir := GetStorageDirectory()
|
||||
Expect(dir).To(ContainSubstring("data/storage"))
|
||||
}
|
||||
|
||||
func TestHostPathForDocker(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
|
||||
t.Setenv("DOKKU_LIB_ROOT", "/var/lib/dokku")
|
||||
|
||||
t.Run("unset host root leaves path unchanged", func(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
t.Setenv("DOKKU_LIB_HOST_ROOT", "")
|
||||
Expect(HostPathForDocker("/var/lib/dokku/data/storage/foo")).To(Equal("/var/lib/dokku/data/storage/foo"))
|
||||
})
|
||||
|
||||
t.Run("equal host root leaves path unchanged", func(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
t.Setenv("DOKKU_LIB_HOST_ROOT", "/var/lib/dokku")
|
||||
Expect(HostPathForDocker("/var/lib/dokku/data/storage/foo")).To(Equal("/var/lib/dokku/data/storage/foo"))
|
||||
})
|
||||
|
||||
t.Run("rewrites paths under lib root", func(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
t.Setenv("DOKKU_LIB_HOST_ROOT", "/host/var/lib/dokku")
|
||||
Expect(HostPathForDocker("/var/lib/dokku/data/storage/foo")).To(Equal("/host/var/lib/dokku/data/storage/foo"))
|
||||
Expect(HostPathForDocker("/var/lib/dokku")).To(Equal("/host/var/lib/dokku"))
|
||||
})
|
||||
|
||||
t.Run("leaves named volumes unchanged", func(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
t.Setenv("DOKKU_LIB_HOST_ROOT", "/host/var/lib/dokku")
|
||||
Expect(HostPathForDocker("myvolume")).To(Equal("myvolume"))
|
||||
})
|
||||
|
||||
t.Run("leaves custom absolute paths unchanged", func(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
t.Setenv("DOKKU_LIB_HOST_ROOT", "/host/var/lib/dokku")
|
||||
Expect(HostPathForDocker("/srv/demo")).To(Equal("/srv/demo"))
|
||||
})
|
||||
|
||||
t.Run("does not false-match similar prefixes", func(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
t.Setenv("DOKKU_LIB_HOST_ROOT", "/host/var/lib/dokku")
|
||||
Expect(HostPathForDocker("/var/lib/dokku-extra/data")).To(Equal("/var/lib/dokku-extra/data"))
|
||||
})
|
||||
}
|
||||
@@ -308,7 +308,7 @@ var appSchedulerFor = common.GetAppScheduler
|
||||
// buildDockerVFlag formats the Docker -v argument for a docker-local
|
||||
// attachment.
|
||||
func buildDockerVFlag(entry *Entry, attachment *Attachment) string {
|
||||
host := entry.HostPath
|
||||
host := HostPathForDocker(entry.HostPath)
|
||||
container := attachment.ContainerPath
|
||||
if host == "" || container == "" {
|
||||
return ""
|
||||
|
||||
@@ -61,6 +61,17 @@ func TestBuildDockerVFlagVolumeOptions(t *testing.T) {
|
||||
Expect(roOpts).To(Equal("-v /host:/container:ro,noexec,nosuid"))
|
||||
}
|
||||
|
||||
func TestBuildDockerVFlagRewritesLibHostRoot(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
|
||||
t.Setenv("DOKKU_LIB_ROOT", "/var/lib/dokku")
|
||||
t.Setenv("DOKKU_LIB_HOST_ROOT", "/host/var/lib/dokku")
|
||||
|
||||
entry := &Entry{HostPath: "/var/lib/dokku/data/storage/demo"}
|
||||
flag := buildDockerVFlag(entry, &Attachment{ContainerPath: "/app/data"})
|
||||
Expect(flag).To(Equal("-v /host/var/lib/dokku/data/storage/demo:/app/data"))
|
||||
}
|
||||
|
||||
func TestTriggerDockerArgsRejectsSchedulerMismatch(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
root := withTempLibRoot(t)
|
||||
|
||||
Reference in new issue
Block a user