daemon_linux_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "testing"
  11. "time"
  12. "github.com/docker/docker/api/types"
  13. containerapi "github.com/docker/docker/api/types/container"
  14. realcontainer "github.com/docker/docker/container"
  15. "github.com/docker/docker/integration/internal/container"
  16. "github.com/docker/docker/testutil/daemon"
  17. "golang.org/x/sys/unix"
  18. "gotest.tools/v3/assert"
  19. is "gotest.tools/v3/assert/cmp"
  20. "gotest.tools/v3/skip"
  21. )
  22. // This is a regression test for #36145
  23. // It ensures that a container can be started when the daemon was improperly
  24. // shutdown when the daemon is brought back up.
  25. //
  26. // The regression is due to improper error handling preventing a container from
  27. // being restored and as such have the resources cleaned up.
  28. //
  29. // To test this, we need to kill dockerd, then kill both the containerd-shim and
  30. // the container process, then start dockerd back up and attempt to start the
  31. // container again.
  32. func TestContainerStartOnDaemonRestart(t *testing.T) {
  33. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  34. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  35. skip.If(t, testEnv.IsRootless)
  36. t.Parallel()
  37. d := daemon.New(t)
  38. d.StartWithBusybox(t, "--iptables=false")
  39. defer d.Stop(t)
  40. c := d.NewClientT(t)
  41. ctx := context.Background()
  42. cID := container.Create(ctx, t, c)
  43. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  44. err := c.ContainerStart(ctx, cID, types.ContainerStartOptions{})
  45. assert.Check(t, err, "error starting test container")
  46. inspect, err := c.ContainerInspect(ctx, cID)
  47. assert.Check(t, err, "error getting inspect data")
  48. ppid := getContainerdShimPid(t, inspect)
  49. err = d.Kill()
  50. assert.Check(t, err, "failed to kill test daemon")
  51. err = unix.Kill(inspect.State.Pid, unix.SIGKILL)
  52. assert.Check(t, err, "failed to kill container process")
  53. err = unix.Kill(ppid, unix.SIGKILL)
  54. assert.Check(t, err, "failed to kill containerd-shim")
  55. d.Start(t, "--iptables=false")
  56. err = c.ContainerStart(ctx, cID, types.ContainerStartOptions{})
  57. assert.Check(t, err, "failed to start test container")
  58. }
  59. func getContainerdShimPid(t *testing.T, c types.ContainerJSON) int {
  60. statB, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", c.State.Pid))
  61. assert.Check(t, err, "error looking up containerd-shim pid")
  62. // ppid is the 4th entry in `/proc/pid/stat`
  63. ppid, err := strconv.Atoi(strings.Fields(string(statB))[3])
  64. assert.Check(t, err, "error converting ppid field to int")
  65. assert.Check(t, ppid != 1, "got unexpected ppid")
  66. return ppid
  67. }
  68. // TestDaemonRestartIpcMode makes sure a container keeps its ipc mode
  69. // (derived from daemon default) even after the daemon is restarted
  70. // with a different default ipc mode.
  71. func TestDaemonRestartIpcMode(t *testing.T) {
  72. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  73. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  74. t.Parallel()
  75. d := daemon.New(t)
  76. d.StartWithBusybox(t, "--iptables=false", "--default-ipc-mode=private")
  77. defer d.Stop(t)
  78. c := d.NewClientT(t)
  79. ctx := context.Background()
  80. // check the container is created with private ipc mode as per daemon default
  81. cID := container.Run(ctx, t, c,
  82. container.WithCmd("top"),
  83. container.WithRestartPolicy("always"),
  84. )
  85. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  86. inspect, err := c.ContainerInspect(ctx, cID)
  87. assert.NilError(t, err)
  88. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "private"))
  89. // restart the daemon with shareable default ipc mode
  90. d.Restart(t, "--iptables=false", "--default-ipc-mode=shareable")
  91. // check the container is still having private ipc mode
  92. inspect, err = c.ContainerInspect(ctx, cID)
  93. assert.NilError(t, err)
  94. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "private"))
  95. // check a new container is created with shareable ipc mode as per new daemon default
  96. cID = container.Run(ctx, t, c)
  97. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  98. inspect, err = c.ContainerInspect(ctx, cID)
  99. assert.NilError(t, err)
  100. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "shareable"))
  101. }
  102. // TestDaemonHostGatewayIP verifies that when a magic string "host-gateway" is passed
  103. // to ExtraHosts (--add-host) instead of an IP address, its value is set to
  104. // 1. Daemon config flag value specified by host-gateway-ip or
  105. // 2. IP of the default bridge network
  106. // and is added to the /etc/hosts file
  107. func TestDaemonHostGatewayIP(t *testing.T) {
  108. skip.If(t, testEnv.IsRemoteDaemon)
  109. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  110. skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
  111. t.Parallel()
  112. // Verify the IP in /etc/hosts is same as host-gateway-ip
  113. d := daemon.New(t)
  114. // Verify the IP in /etc/hosts is same as the default bridge's IP
  115. d.StartWithBusybox(t)
  116. c := d.NewClientT(t)
  117. ctx := context.Background()
  118. cID := container.Run(ctx, t, c,
  119. container.WithExtraHost("host.docker.internal:host-gateway"),
  120. )
  121. res, err := container.Exec(ctx, c, cID, []string{"cat", "/etc/hosts"})
  122. assert.NilError(t, err)
  123. assert.Assert(t, is.Len(res.Stderr(), 0))
  124. assert.Equal(t, 0, res.ExitCode)
  125. inspect, err := c.NetworkInspect(ctx, "bridge", types.NetworkInspectOptions{})
  126. assert.NilError(t, err)
  127. assert.Check(t, is.Contains(res.Stdout(), inspect.IPAM.Config[0].Gateway))
  128. c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  129. d.Stop(t)
  130. // Verify the IP in /etc/hosts is same as host-gateway-ip
  131. d.StartWithBusybox(t, "--host-gateway-ip=6.7.8.9")
  132. cID = container.Run(ctx, t, c,
  133. container.WithExtraHost("host.docker.internal:host-gateway"),
  134. )
  135. res, err = container.Exec(ctx, c, cID, []string{"cat", "/etc/hosts"})
  136. assert.NilError(t, err)
  137. assert.Assert(t, is.Len(res.Stderr(), 0))
  138. assert.Equal(t, 0, res.ExitCode)
  139. assert.Check(t, is.Contains(res.Stdout(), "6.7.8.9"))
  140. c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  141. d.Stop(t)
  142. }
  143. // TestRestartDaemonWithRestartingContainer simulates a case where a container is in "restarting" state when
  144. // dockerd is killed (due to machine reset or something else).
  145. //
  146. // Related to moby/moby#41817
  147. //
  148. // In this test we'll change the container state to "restarting".
  149. // This means that the container will not be 'alive' when we attempt to restore in on daemon startup.
  150. //
  151. // We could do the same with `docker run -d --resetart=always busybox:latest exit 1`, and then
  152. // `kill -9` dockerd while the container is in "restarting" state. This is difficult to reproduce reliably
  153. // in an automated test, so we manipulate on disk state instead.
  154. func TestRestartDaemonWithRestartingContainer(t *testing.T) {
  155. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  156. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  157. t.Parallel()
  158. d := daemon.New(t)
  159. defer d.Cleanup(t)
  160. d.StartWithBusybox(t, "--iptables=false")
  161. defer d.Stop(t)
  162. ctx := context.Background()
  163. client := d.NewClientT(t)
  164. // Just create the container, no need to start it to be started.
  165. // We really want to make sure there is no process running when docker starts back up.
  166. // We will manipulate the on disk state later
  167. id := container.Create(ctx, t, client, container.WithRestartPolicy("always"), container.WithCmd("/bin/sh", "-c", "exit 1"))
  168. d.Stop(t)
  169. configPath := filepath.Join(d.Root, "containers", id, "config.v2.json")
  170. configBytes, err := os.ReadFile(configPath)
  171. assert.NilError(t, err)
  172. var c realcontainer.Container
  173. assert.NilError(t, json.Unmarshal(configBytes, &c))
  174. c.State = realcontainer.NewState()
  175. c.SetRestarting(&realcontainer.ExitStatus{ExitCode: 1})
  176. c.HasBeenStartedBefore = true
  177. configBytes, err = json.Marshal(&c)
  178. assert.NilError(t, err)
  179. assert.NilError(t, os.WriteFile(configPath, configBytes, 0600))
  180. d.Start(t)
  181. ctxTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
  182. defer cancel()
  183. chOk, chErr := client.ContainerWait(ctxTimeout, id, containerapi.WaitConditionNextExit)
  184. select {
  185. case <-chOk:
  186. case err := <-chErr:
  187. assert.NilError(t, err)
  188. }
  189. }