daemon_linux_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/docker/docker/api/types"
  11. containertypes "github.com/docker/docker/api/types/container"
  12. realcontainer "github.com/docker/docker/container"
  13. "github.com/docker/docker/integration/internal/container"
  14. "github.com/docker/docker/testutil"
  15. "github.com/docker/docker/testutil/daemon"
  16. "golang.org/x/sys/unix"
  17. "gotest.tools/v3/assert"
  18. is "gotest.tools/v3/assert/cmp"
  19. "gotest.tools/v3/assert/opt"
  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. ctx := testutil.StartSpan(baseContext, t)
  38. d := daemon.New(t)
  39. d.StartWithBusybox(ctx, t, "--iptables=false")
  40. defer d.Stop(t)
  41. c := d.NewClientT(t)
  42. cID := container.Create(ctx, t, c)
  43. defer c.ContainerRemove(ctx, cID, containertypes.RemoveOptions{Force: true})
  44. err := c.ContainerStart(ctx, cID, containertypes.StartOptions{})
  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, containertypes.StartOptions{})
  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. ctx := testutil.StartSpan(baseContext, t)
  76. d := daemon.New(t)
  77. d.StartWithBusybox(ctx, t, "--iptables=false", "--default-ipc-mode=private")
  78. defer d.Stop(t)
  79. c := d.NewClientT(t)
  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(containertypes.RestartPolicyAlways),
  84. )
  85. defer c.ContainerRemove(ctx, cID, containertypes.RemoveOptions{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, containertypes.RemoveOptions{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. ctx := testutil.StartSpan(baseContext, t)
  113. // Verify the IP in /etc/hosts is same as host-gateway-ip
  114. d := daemon.New(t)
  115. // Verify the IP in /etc/hosts is same as the default bridge's IP
  116. d.StartWithBusybox(ctx, t, "--iptables=false")
  117. c := d.NewClientT(t)
  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, containertypes.RemoveOptions{Force: true})
  129. d.Stop(t)
  130. // Verify the IP in /etc/hosts is same as host-gateway-ip
  131. d.StartWithBusybox(ctx, t, "--iptables=false", "--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, containertypes.RemoveOptions{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. ctx := testutil.StartSpan(baseContext, t)
  159. d := daemon.New(t)
  160. defer d.Cleanup(t)
  161. d.StartWithBusybox(ctx, t, "--iptables=false")
  162. defer d.Stop(t)
  163. apiClient := 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, apiClient, container.WithRestartPolicy(containertypes.RestartPolicyAlways), container.WithCmd("/bin/sh", "-c", "exit 1"))
  168. d.Stop(t)
  169. d.TamperWithContainerConfig(t, id, func(c *realcontainer.Container) {
  170. c.SetRestarting(&realcontainer.ExitStatus{ExitCode: 1})
  171. c.HasBeenStartedBefore = true
  172. })
  173. d.Start(t, "--iptables=false")
  174. ctxTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
  175. defer cancel()
  176. chOk, chErr := apiClient.ContainerWait(ctxTimeout, id, containertypes.WaitConditionNextExit)
  177. select {
  178. case <-chOk:
  179. case err := <-chErr:
  180. assert.NilError(t, err)
  181. }
  182. }
  183. // TestHardRestartWhenContainerIsRunning simulates a case where dockerd is
  184. // killed while a container is running, and the container's task no longer
  185. // exists when dockerd starts back up. This can happen if the system is
  186. // hard-rebooted, for example.
  187. //
  188. // Regression test for moby/moby#45788
  189. func TestHardRestartWhenContainerIsRunning(t *testing.T) {
  190. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  191. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  192. t.Parallel()
  193. ctx := testutil.StartSpan(baseContext, t)
  194. d := daemon.New(t)
  195. defer d.Cleanup(t)
  196. d.StartWithBusybox(ctx, t, "--iptables=false")
  197. defer d.Stop(t)
  198. apiClient := d.NewClientT(t)
  199. // Just create the containers, no need to start them.
  200. // We really want to make sure there is no process running when docker starts back up.
  201. // We will manipulate the on disk state later.
  202. noPolicy := container.Create(ctx, t, apiClient, container.WithCmd("/bin/sh", "-c", "exit 1"))
  203. onFailure := container.Create(ctx, t, apiClient, container.WithRestartPolicy("on-failure"), container.WithCmd("/bin/sh", "-c", "sleep 60"))
  204. d.Stop(t)
  205. for _, id := range []string{noPolicy, onFailure} {
  206. d.TamperWithContainerConfig(t, id, func(c *realcontainer.Container) {
  207. c.SetRunning(nil, nil, true)
  208. c.HasBeenStartedBefore = true
  209. })
  210. }
  211. d.Start(t, "--iptables=false")
  212. t.Run("RestartPolicy=none", func(t *testing.T) {
  213. ctx := testutil.StartSpan(ctx, t)
  214. ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
  215. defer cancel()
  216. inspect, err := apiClient.ContainerInspect(ctx, noPolicy)
  217. assert.NilError(t, err)
  218. assert.Check(t, is.Equal(inspect.State.Status, "exited"))
  219. assert.Check(t, is.Equal(inspect.State.ExitCode, 255))
  220. finishedAt, err := time.Parse(time.RFC3339Nano, inspect.State.FinishedAt)
  221. if assert.Check(t, err) {
  222. assert.Check(t, is.DeepEqual(finishedAt, time.Now(), opt.TimeWithThreshold(time.Minute)))
  223. }
  224. })
  225. t.Run("RestartPolicy=on-failure", func(t *testing.T) {
  226. ctx := testutil.StartSpan(ctx, t)
  227. ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
  228. defer cancel()
  229. inspect, err := apiClient.ContainerInspect(ctx, onFailure)
  230. assert.NilError(t, err)
  231. assert.Check(t, is.Equal(inspect.State.Status, "running"))
  232. assert.Check(t, is.Equal(inspect.State.ExitCode, 0))
  233. finishedAt, err := time.Parse(time.RFC3339Nano, inspect.State.FinishedAt)
  234. if assert.Check(t, err) {
  235. assert.Check(t, is.DeepEqual(finishedAt, time.Now(), opt.TimeWithThreshold(time.Minute)))
  236. }
  237. stopTimeout := 0
  238. assert.Assert(t, apiClient.ContainerStop(ctx, onFailure, containertypes.StopOptions{Timeout: &stopTimeout}))
  239. })
  240. }