mounts_linux_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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/system"
  16. "github.com/moby/sys/mount"
  17. "github.com/moby/sys/mountinfo"
  18. "gotest.tools/v3/assert"
  19. is "gotest.tools/v3/assert/cmp"
  20. "gotest.tools/v3/fs"
  21. "gotest.tools/v3/poll"
  22. "gotest.tools/v3/skip"
  23. )
  24. func TestContainerNetworkMountsNoChown(t *testing.T) {
  25. // chown only applies to Linux bind mounted volumes; must be same host to verify
  26. skip.If(t, testEnv.IsRemoteDaemon)
  27. defer setupTest(t)()
  28. ctx := context.Background()
  29. tmpDir := fs.NewDir(t, "network-file-mounts", fs.WithMode(0755), fs.WithFile("nwfile", "network file bind mount", fs.WithMode(0644)))
  30. defer tmpDir.Remove()
  31. tmpNWFileMount := tmpDir.Join("nwfile")
  32. config := containertypes.Config{
  33. Image: "busybox",
  34. }
  35. hostConfig := containertypes.HostConfig{
  36. Mounts: []mounttypes.Mount{
  37. {
  38. Type: "bind",
  39. Source: tmpNWFileMount,
  40. Target: "/etc/resolv.conf",
  41. },
  42. {
  43. Type: "bind",
  44. Source: tmpNWFileMount,
  45. Target: "/etc/hostname",
  46. },
  47. {
  48. Type: "bind",
  49. Source: tmpNWFileMount,
  50. Target: "/etc/hosts",
  51. },
  52. },
  53. }
  54. cli, err := client.NewClientWithOpts(client.FromEnv)
  55. assert.NilError(t, err)
  56. defer cli.Close()
  57. ctrCreate, err := cli.ContainerCreate(ctx, &config, &hostConfig, &network.NetworkingConfig{}, nil, "")
  58. assert.NilError(t, err)
  59. // container will exit immediately because of no tty, but we only need the start sequence to test the condition
  60. err = cli.ContainerStart(ctx, ctrCreate.ID, types.ContainerStartOptions{})
  61. assert.NilError(t, err)
  62. // Check that host-located bind mount network file did not change ownership when the container was started
  63. // Note: If the user specifies a mountpath from the host, we should not be
  64. // attempting to chown files outside the daemon's metadata directory
  65. // (represented by `daemon.repository` at init time).
  66. // This forces users who want to use user namespaces to handle the
  67. // ownership needs of any external files mounted as network files
  68. // (/etc/resolv.conf, /etc/hosts, /etc/hostname) separately from the
  69. // daemon. In all other volume/bind mount situations we have taken this
  70. // same line--we don't chown host file content.
  71. // See GitHub PR 34224 for details.
  72. statT, err := system.Stat(tmpNWFileMount)
  73. assert.NilError(t, err)
  74. assert.Check(t, is.Equal(uint32(0), statT.UID()), "bind mounted network file should not change ownership from root")
  75. }
  76. func TestMountDaemonRoot(t *testing.T) {
  77. skip.If(t, testEnv.IsRemoteDaemon)
  78. defer setupTest(t)()
  79. client := testEnv.APIClient()
  80. ctx := context.Background()
  81. info, err := client.Info(ctx)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. for _, test := range []struct {
  86. desc string
  87. propagation mounttypes.Propagation
  88. expected mounttypes.Propagation
  89. }{
  90. {
  91. desc: "default",
  92. propagation: "",
  93. expected: mounttypes.PropagationRSlave,
  94. },
  95. {
  96. desc: "private",
  97. propagation: mounttypes.PropagationPrivate,
  98. },
  99. {
  100. desc: "rprivate",
  101. propagation: mounttypes.PropagationRPrivate,
  102. },
  103. {
  104. desc: "slave",
  105. propagation: mounttypes.PropagationSlave,
  106. },
  107. {
  108. desc: "rslave",
  109. propagation: mounttypes.PropagationRSlave,
  110. expected: mounttypes.PropagationRSlave,
  111. },
  112. {
  113. desc: "shared",
  114. propagation: mounttypes.PropagationShared,
  115. },
  116. {
  117. desc: "rshared",
  118. propagation: mounttypes.PropagationRShared,
  119. expected: mounttypes.PropagationRShared,
  120. },
  121. } {
  122. t.Run(test.desc, func(t *testing.T) {
  123. test := test
  124. t.Parallel()
  125. propagationSpec := fmt.Sprintf(":%s", test.propagation)
  126. if test.propagation == "" {
  127. propagationSpec = ""
  128. }
  129. bindSpecRoot := info.DockerRootDir + ":" + "/foo" + propagationSpec
  130. bindSpecSub := filepath.Join(info.DockerRootDir, "containers") + ":/foo" + propagationSpec
  131. for name, hc := range map[string]*containertypes.HostConfig{
  132. "bind root": {Binds: []string{bindSpecRoot}},
  133. "bind subpath": {Binds: []string{bindSpecSub}},
  134. "mount root": {
  135. Mounts: []mounttypes.Mount{
  136. {
  137. Type: mounttypes.TypeBind,
  138. Source: info.DockerRootDir,
  139. Target: "/foo",
  140. BindOptions: &mounttypes.BindOptions{Propagation: test.propagation},
  141. },
  142. },
  143. },
  144. "mount subpath": {
  145. Mounts: []mounttypes.Mount{
  146. {
  147. Type: mounttypes.TypeBind,
  148. Source: filepath.Join(info.DockerRootDir, "containers"),
  149. Target: "/foo",
  150. BindOptions: &mounttypes.BindOptions{Propagation: test.propagation},
  151. },
  152. },
  153. },
  154. } {
  155. t.Run(name, func(t *testing.T) {
  156. hc := hc
  157. t.Parallel()
  158. c, err := client.ContainerCreate(ctx, &containertypes.Config{
  159. Image: "busybox",
  160. Cmd: []string{"true"},
  161. }, hc, nil, nil, "")
  162. if err != nil {
  163. if test.expected != "" {
  164. t.Fatal(err)
  165. }
  166. // expected an error, so this is ok and should not continue
  167. return
  168. }
  169. if test.expected == "" {
  170. t.Fatal("expected create to fail")
  171. }
  172. defer func() {
  173. if err := client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
  174. panic(err)
  175. }
  176. }()
  177. inspect, err := client.ContainerInspect(ctx, c.ID)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. if len(inspect.Mounts) != 1 {
  182. t.Fatalf("unexpected number of mounts: %+v", inspect.Mounts)
  183. }
  184. m := inspect.Mounts[0]
  185. if m.Propagation != test.expected {
  186. t.Fatalf("got unexpected propagation mode, expected %q, got: %v", test.expected, m.Propagation)
  187. }
  188. })
  189. }
  190. })
  191. }
  192. }
  193. func TestContainerBindMountNonRecursive(t *testing.T) {
  194. skip.If(t, testEnv.IsRemoteDaemon)
  195. skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "BindOptions.NonRecursive requires API v1.40")
  196. skip.If(t, testEnv.IsRootless, "cannot be tested because RootlessKit executes the daemon in private mount namespace (https://github.com/rootless-containers/rootlesskit/issues/97)")
  197. defer setupTest(t)()
  198. tmpDir1 := fs.NewDir(t, "tmpdir1", fs.WithMode(0755),
  199. fs.WithDir("mnt", fs.WithMode(0755)))
  200. defer tmpDir1.Remove()
  201. tmpDir1Mnt := filepath.Join(tmpDir1.Path(), "mnt")
  202. tmpDir2 := fs.NewDir(t, "tmpdir2", fs.WithMode(0755),
  203. fs.WithFile("file", "should not be visible when NonRecursive", fs.WithMode(0644)))
  204. defer tmpDir2.Remove()
  205. err := mount.Mount(tmpDir2.Path(), tmpDir1Mnt, "none", "bind,ro")
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. defer func() {
  210. if err := mount.Unmount(tmpDir1Mnt); err != nil {
  211. t.Fatal(err)
  212. }
  213. }()
  214. // implicit is recursive (NonRecursive: false)
  215. implicit := mounttypes.Mount{
  216. Type: "bind",
  217. Source: tmpDir1.Path(),
  218. Target: "/foo",
  219. ReadOnly: true,
  220. }
  221. recursive := implicit
  222. recursive.BindOptions = &mounttypes.BindOptions{
  223. NonRecursive: false,
  224. }
  225. recursiveVerifier := []string{"test", "-f", "/foo/mnt/file"}
  226. nonRecursive := implicit
  227. nonRecursive.BindOptions = &mounttypes.BindOptions{
  228. NonRecursive: true,
  229. }
  230. nonRecursiveVerifier := []string{"test", "!", "-f", "/foo/mnt/file"}
  231. ctx := context.Background()
  232. client := testEnv.APIClient()
  233. containers := []string{
  234. container.Run(ctx, t, client, container.WithMount(implicit), container.WithCmd(recursiveVerifier...)),
  235. container.Run(ctx, t, client, container.WithMount(recursive), container.WithCmd(recursiveVerifier...)),
  236. container.Run(ctx, t, client, container.WithMount(nonRecursive), container.WithCmd(nonRecursiveVerifier...)),
  237. }
  238. for _, c := range containers {
  239. poll.WaitOn(t, container.IsSuccessful(ctx, client, c), poll.WithDelay(100*time.Millisecond))
  240. }
  241. }
  242. func TestContainerVolumesMountedAsShared(t *testing.T) {
  243. // Volume propagation is linux only. Also it creates directories for
  244. // bind mounting, so needs to be same host.
  245. skip.If(t, testEnv.IsRemoteDaemon)
  246. skip.If(t, testEnv.IsUserNamespace)
  247. skip.If(t, testEnv.IsRootless, "cannot be tested because RootlessKit executes the daemon in private mount namespace (https://github.com/rootless-containers/rootlesskit/issues/97)")
  248. defer setupTest(t)()
  249. // Prepare a source directory to bind mount
  250. tmpDir1 := fs.NewDir(t, "volume-source", fs.WithMode(0755),
  251. fs.WithDir("mnt1", fs.WithMode(0755)))
  252. defer tmpDir1.Remove()
  253. tmpDir1Mnt := filepath.Join(tmpDir1.Path(), "mnt1")
  254. // Convert this directory into a shared mount point so that we do
  255. // not rely on propagation properties of parent mount.
  256. if err := mount.MakePrivate(tmpDir1.Path()); err != nil {
  257. t.Fatal(err)
  258. }
  259. defer func() {
  260. if err := mount.Unmount(tmpDir1.Path()); err != nil {
  261. t.Fatal(err)
  262. }
  263. }()
  264. if err := mount.MakeShared(tmpDir1.Path()); err != nil {
  265. t.Fatal(err)
  266. }
  267. sharedMount := mounttypes.Mount{
  268. Type: mounttypes.TypeBind,
  269. Source: tmpDir1.Path(),
  270. Target: "/volume-dest",
  271. BindOptions: &mounttypes.BindOptions{
  272. Propagation: mounttypes.PropagationShared,
  273. },
  274. }
  275. bindMountCmd := []string{"mount", "--bind", "/volume-dest/mnt1", "/volume-dest/mnt1"}
  276. ctx := context.Background()
  277. client := testEnv.APIClient()
  278. containerID := container.Run(ctx, t, client, container.WithPrivileged(true), container.WithMount(sharedMount), container.WithCmd(bindMountCmd...))
  279. poll.WaitOn(t, container.IsSuccessful(ctx, client, containerID), poll.WithDelay(100*time.Millisecond))
  280. // Make sure a bind mount under a shared volume propagated to host.
  281. if mounted, _ := mountinfo.Mounted(tmpDir1Mnt); !mounted {
  282. t.Fatalf("Bind mount under shared volume did not propagate to host")
  283. }
  284. mount.Unmount(tmpDir1Mnt)
  285. }
  286. func TestContainerVolumesMountedAsSlave(t *testing.T) {
  287. // Volume propagation is linux only. Also it creates directories for
  288. // bind mounting, so needs to be same host.
  289. skip.If(t, testEnv.IsRemoteDaemon)
  290. skip.If(t, testEnv.IsUserNamespace)
  291. skip.If(t, testEnv.IsRootless, "cannot be tested because RootlessKit executes the daemon in private mount namespace (https://github.com/rootless-containers/rootlesskit/issues/97)")
  292. // Prepare a source directory to bind mount
  293. tmpDir1 := fs.NewDir(t, "volume-source", fs.WithMode(0755),
  294. fs.WithDir("mnt1", fs.WithMode(0755)))
  295. defer tmpDir1.Remove()
  296. tmpDir1Mnt := filepath.Join(tmpDir1.Path(), "mnt1")
  297. // Prepare a source directory with file in it. We will bind mount this
  298. // directory and see if file shows up.
  299. tmpDir2 := fs.NewDir(t, "volume-source2", fs.WithMode(0755),
  300. fs.WithFile("slave-testfile", "Test", fs.WithMode(0644)))
  301. defer tmpDir2.Remove()
  302. // Convert this directory into a shared mount point so that we do
  303. // not rely on propagation properties of parent mount.
  304. if err := mount.MakePrivate(tmpDir1.Path()); err != nil {
  305. t.Fatal(err)
  306. }
  307. defer func() {
  308. if err := mount.Unmount(tmpDir1.Path()); err != nil {
  309. t.Fatal(err)
  310. }
  311. }()
  312. if err := mount.MakeShared(tmpDir1.Path()); err != nil {
  313. t.Fatal(err)
  314. }
  315. slaveMount := mounttypes.Mount{
  316. Type: mounttypes.TypeBind,
  317. Source: tmpDir1.Path(),
  318. Target: "/volume-dest",
  319. BindOptions: &mounttypes.BindOptions{
  320. Propagation: mounttypes.PropagationSlave,
  321. },
  322. }
  323. topCmd := []string{"top"}
  324. ctx := context.Background()
  325. client := testEnv.APIClient()
  326. containerID := container.Run(ctx, t, client, container.WithTty(true), container.WithMount(slaveMount), container.WithCmd(topCmd...))
  327. // Bind mount tmpDir2/ onto tmpDir1/mnt1. If mount propagates inside
  328. // container then contents of tmpDir2/slave-testfile should become
  329. // visible at "/volume-dest/mnt1/slave-testfile"
  330. if err := mount.Mount(tmpDir2.Path(), tmpDir1Mnt, "none", "bind"); err != nil {
  331. t.Fatal(err)
  332. }
  333. defer func() {
  334. if err := mount.Unmount(tmpDir1Mnt); err != nil {
  335. t.Fatal(err)
  336. }
  337. }()
  338. mountCmd := []string{"cat", "/volume-dest/mnt1/slave-testfile"}
  339. if result, err := container.Exec(ctx, client, containerID, mountCmd); err == nil {
  340. if result.Stdout() != "Test" {
  341. t.Fatalf("Bind mount under slave volume did not propagate to container")
  342. }
  343. } else {
  344. t.Fatal(err)
  345. }
  346. }