resolve the config file from the sudo user
Vincent Demeester authored on 2016/02/26 00:05:25... | ... |
@@ -29,9 +29,20 @@ var ( |
29 | 29 |
configDir = os.Getenv("DOCKER_CONFIG") |
30 | 30 |
) |
31 | 31 |
|
32 |
+func getDefaultConfigDir(confFile string) string { |
|
33 |
+ confDir := filepath.Join(homedir.Get(), confFile) |
|
34 |
+ // if the directory doesn't exist, maybe we called docker with sudo |
|
35 |
+ if _, err := os.Stat(configDir); err != nil { |
|
36 |
+ if os.IsNotExist(err) { |
|
37 |
+ return filepath.Join(homedir.GetWithSudoUser(), confFile) |
|
38 |
+ } |
|
39 |
+ } |
|
40 |
+ return confDir |
|
41 |
+} |
|
42 |
+ |
|
32 | 43 |
func init() { |
33 | 44 |
if configDir == "" { |
34 |
- configDir = filepath.Join(homedir.Get(), ".docker") |
|
45 |
+ configDir = getDefaultConfigDir(".docker") |
|
35 | 46 |
} |
36 | 47 |
} |
37 | 48 |
|
... | ... |
@@ -178,7 +189,7 @@ func Load(configDir string) (*ConfigFile, error) { |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
// Can't find latest config file so check for the old one |
181 |
- confFile := filepath.Join(homedir.Get(), oldConfigfile) |
|
181 |
+ confFile := getDefaultConfigDir(oldConfigfile) |
|
182 | 182 |
if _, err := os.Stat(confFile); err != nil { |
183 | 183 |
return &configFile, nil //missing file is not an error |
184 | 184 |
} |
... | ... |
@@ -78,6 +78,9 @@ For example: |
78 | 78 |
Instructs Docker to use the configuration files in your `~/testconfigs/` |
79 | 79 |
directory when running the `ps` command. |
80 | 80 |
|
81 |
+> **Note**: If you run docker commands with `sudo`, Docker first looks for a configuration |
|
82 |
+> file in `/root/.docker/`, before looking in `~/.docker/` for the user that did the sudo call. |
|
83 |
+ |
|
81 | 84 |
Docker manages most of the files in the configuration directory |
82 | 85 |
and you should not modify them. However, you *can modify* the |
83 | 86 |
`config.json` file to control certain aspects of how the `docker` |
... | ... |
@@ -142,7 +142,7 @@ func rawJSON(value interface{}) *json.RawMessage { |
142 | 142 |
// ValidateID checks whether an ID string is a valid image ID. |
143 | 143 |
func ValidateID(id string) error { |
144 | 144 |
if ok := validHex.MatchString(id); !ok { |
145 |
- return fmt.Errorf("image ID '%s' is invalid ", id) |
|
145 |
+ return fmt.Errorf("image ID %q is invalid", id) |
|
146 | 146 |
} |
147 | 147 |
return nil |
148 | 148 |
} |
... | ... |
@@ -29,6 +29,19 @@ func Get() string { |
29 | 29 |
return home |
30 | 30 |
} |
31 | 31 |
|
32 |
+// GetWithSudoUser returns the home directory of the user who called sudo (if |
|
33 |
+// available, retrieved from $SUDO_USER). It fallbacks to Get if any error occurs. |
|
34 |
+// Returned path should be used with "path/filepath" to form new paths. |
|
35 |
+func GetWithSudoUser() string { |
|
36 |
+ sudoUser := os.Getenv("SUDO_USER") |
|
37 |
+ if sudoUser != "" { |
|
38 |
+ if user, err := user.LookupUser(sudoUser); err == nil { |
|
39 |
+ return user.Home |
|
40 |
+ } |
|
41 |
+ } |
|
42 |
+ return Get() |
|
43 |
+} |
|
44 |
+ |
|
32 | 45 |
// GetShortcutString returns the string that is shortcut to user's home directory |
33 | 46 |
// in the native shell of the platform running on. |
34 | 47 |
func GetShortcutString() string { |