mounts_linux_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "fmt"
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/docker/docker/api/types"
  9. containertypes "github.com/docker/docker/api/types/container"
  10. mounttypes "github.com/docker/docker/api/types/mount"
  11. "github.com/docker/docker/api/types/network"
  12. "github.com/docker/docker/api/types/versions"
  13. "github.com/docker/docker/client"
  14. "github.com/docker/docker/integration/internal/container"
  15. "github.com/docker/docker/pkg/mount"
  16. "github.com/docker/docker/pkg/system"
  17. "gotest.tools/assert"
  18. is "gotest.tools/assert/cmp"
  19. "gotest.tools/fs"
  20. "gotest.tools/poll"
  21. "gotest.tools/skip"
  22. )
  23. func TestContainerNetworkMountsNoChown(t *testing.T) {
  24. // chown only applies to Linux bind mounted volumes; must be same host to verify
  25. skip.If(t, testEnv.IsRemoteDaemon)
  26. defer setupTest(t)()
  27. ctx := context.Background()
  28. tmpDir := fs.NewDir(t, "network-file-mounts", fs.WithMode(0755), fs.WithFile("nwfile", "network file bind mount", fs.WithMode(0644)))
  29. defer tmpDir.Remove()
  30. tmpNWFileMount := tmpDir.Join("nwfile")
  31. config := containertypes.Config{
  32. Image: "busybox",
  33. }
  34. hostConfig := containertypes.HostConfig{
  35. Mounts: []mounttypes.Mount{
  36. {
  37. Type: "bind",
  38. Source: tmpNWFileMount,
  39. Target: "/etc/resolv.conf",
  40. },
  41. {
  42. Type: "bind",
  43. Source: tmpNWFileMount,
  44. Target: "/etc/hostname",
  45. },
  46. {
  47. Type: "bind",
  48. Source: tmpNWFileMount,
  49. Target: "/etc/hosts",
  50. },
  51. },
  52. }
  53. cli, err := client.NewClientWithOpts(client.FromEnv)
  54. assert.NilError(t, err)
  55. defer cli.Close()
  56. ctrCreate, err := cli.ContainerCreate(ctx, &config, &hostConfig, &network.NetworkingConfig{}, "")
  57. assert.NilError(t, err)
  58. // container will exit immediately because of no tty, but we only need the start sequence to test the condition
  59. err = cli.ContainerStart(ctx, ctrCreate.ID, types.ContainerStartOptions{})
  60. assert.NilError(t, err)
  61. // Check that host-located bind mount network file did not change ownership when the container was started
  62. // Note: If the user specifies a mountpath from the host, we should not be
  63. // attempting to chown files outside the daemon's metadata directory
  64. // (represented by `daemon.repository` at init time).
  65. // This forces users who want to use user namespaces to handle the
  66. // ownership needs of any external files mounted as network files
  67. // (/etc/resolv.conf, /etc/hosts, /etc/hostname) separately from the
  68. // daemon. In all other volume/bind mount situations we have taken this
  69. // same line--we don't chown host file content.
  70. // See GitHub PR 34224 for details.
  71. statT, err := system.Stat(tmpNWFileMount)
  72. assert.NilError(t, err)
  73. assert.Check(t, is.Equal(uint32(0), statT.UID()), "bind mounted network file should not change ownership from root")
  74. }
  75. func TestMountDaemonRoot(t *testing.T) {
  76. skip.If(t, testEnv.IsRemoteDaemon)
  77. defer setupTest(t)()
  78. client := testEnv.APIClient()
  79. ctx := context.Background()
  80. info, err := client.Info(ctx)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. for _, test := range []struct {
  85. desc string
  86. propagation mounttypes.Propagation
  87. expected mounttypes.Propagation
  88. }{
  89. {
  90. desc: "default",
  91. propagation: "",
  92. expected: mounttypes.PropagationRSlave,
  93. },
  94. {
  95. desc: "private",
  96. propagation: mounttypes.PropagationPrivate,
  97. },
  98. {
  99. desc: "rprivate",
  100. propagation: mounttypes.PropagationRPrivate,
  101. },
  102. {
  103. desc: "slave",
  104. propagation: mounttypes.PropagationSlave,
  105. },
  106. {
  107. desc: "rslave",
  108. propagation: mounttypes.PropagationRSlave,
  109. expected: mounttypes.PropagationRSlave,
  110. },
  111. {
  112. desc: "shared",
  113. propagation: mounttypes.PropagationShared,
  114. },
  115. {
  116. desc: "rshared",
  117. propagation: mounttypes.PropagationRShared,
  118. expected: mounttypes.PropagationRShared,
  119. },
  120. } {
  121. t.Run(test.desc, func(t *testing.T) {
  122. test := test
  123. t.Parallel()
  124. propagationSpec := fmt.Sprintf(":%s", test.propagation)
  125. if test.propagation == "" {
  126. propagationSpec = ""
  127. }
  128. bindSpecRoot := info.DockerRootDir + ":" + "/foo" + propagationSpec
  129. bindSpecSub := filepath.Join(info.DockerRootDir, "containers") + ":/foo" + propagationSpec
  130. for name, hc := range map[string]*containertypes.HostConfig{
  131. "bind root": {Binds: []string{bindSpecRoot}},
  132. "bind subpath": {Binds: []string{bindSpecSub}},
  133. "mount root": {
  134. Mounts: []mounttypes.Mount{
  135. {
  136. Type: mounttypes.TypeBind,
  137. Source: info.DockerRootDir,
  138. Target: "/foo",
  139. BindOptions: &mounttypes.BindOptions{Propagation: test.propagation},
  140. },
  141. },
  142. },
  143. "mount subpath": {
  144. Mounts: []mounttypes.Mount{
  145. {
  146. Type: mounttypes.TypeBind,
  147. Source: filepath.Join(info.DockerRootDir, "containers"),
  148. Target: "/foo",
  149. BindOptions: &mounttypes.BindOptions{Propagation: test.propagation},
  150. },
  151. },
  152. },
  153. } {
  154. t.Run(name, func(t *testing.T) {
  155. hc := hc
  156. t.Parallel()
  157. c, err := client.ContainerCreate(ctx, &containertypes.Config{
  158. Image: "busybox",
  159. Cmd: []string{"true"},
  160. }, hc, nil, "")
  161. if err != nil {
  162. if test.expected != "" {
  163. t.Fatal(err)
  164. }
  165. // expected an error, so this is ok and should not continue
  166. return
  167. }
  168. if test.expected == "" {
  169. t.Fatal("expected create to fail")
  170. }
  171. defer func() {
  172. if err := client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
  173. panic(err)
  174. }
  175. }()
  176. inspect, err := client.ContainerInspect(ctx, c.ID)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. if len(inspect.Mounts) != 1 {
  181. t.Fatalf("unexpected number of mounts: %+v", inspect.Mounts)
  182. }
  183. m := inspect.Mounts[0]
  184. if m.Propagation != test.expected {
  185. t.Fatalf("got unexpected propagation mode, expected %q, got: %v", test.expected, m.Propagation)
  186. }
  187. })
  188. }
  189. })
  190. }
  191. }
  192. func TestContainerBindMountNonRecursive(t *testing.T) {
  193. skip.If(t, testEnv.IsRemoteDaemon)
  194. skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "BindOptions.NonRecursive requires API v1.40")
  195. defer setupTest(t)()
  196. tmpDir1 := fs.NewDir(t, "tmpdir1", fs.WithMode(0755),
  197. fs.WithDir("mnt", fs.WithMode(0755)))
  198. defer tmpDir1.Remove()
  199. tmpDir1Mnt := filepath.Join(tmpDir1.Path(), "mnt")
  200. tmpDir2 := fs.NewDir(t, "tmpdir2", fs.WithMode(0755),
  201. fs.WithFile("file", "should not be visible when NonRecursive", fs.WithMode(0644)))
  202. defer tmpDir2.Remove()
  203. err := mount.Mount(tmpDir2.Path(), tmpDir1Mnt, "none", "bind,ro")
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. defer func() {
  208. if err := mount.Unmount(tmpDir1Mnt); err != nil {
  209. t.Fatal(err)
  210. }
  211. }()
  212. // implicit is recursive (NonRecursive: false)
  213. implicit := mounttypes.Mount{
  214. Type: "bind",
  215. Source: tmpDir1.Path(),
  216. Target: "/foo",
  217. ReadOnly: true,
  218. }
  219. recursive := implicit
  220. recursive.BindOptions = &mounttypes.BindOptions{
  221. NonRecursive: false,
  222. }
  223. recursiveVerifier := []string{"test", "-f", "/foo/mnt/file"}
  224. nonRecursive := implicit
  225. nonRecursive.BindOptions = &mounttypes.BindOptions{
  226. NonRecursive: true,
  227. }
  228. nonRecursiveVerifier := []string{"test", "!", "-f", "/foo/mnt/file"}
  229. ctx := context.Background()
  230. client := testEnv.APIClient()
  231. containers := []string{
  232. container.Run(t, ctx, client, container.WithMount(implicit), container.WithCmd(recursiveVerifier...)),
  233. container.Run(t, ctx, client, container.WithMount(recursive), container.WithCmd(recursiveVerifier...)),
  234. container.Run(t, ctx, client, container.WithMount(nonRecursive), container.WithCmd(nonRecursiveVerifier...)),
  235. }
  236. for _, c := range containers {
  237. poll.WaitOn(t, container.IsSuccessful(ctx, client, c), poll.WithDelay(100*time.Millisecond))
  238. }
  239. }