mounts_linux_test.go 5.5 KB

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