daemon_linux_test.go 10 KB

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