docker_cli_create_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "reflect"
  7. "strings"
  8. "time"
  9. "github.com/docker/docker/integration-cli/checker"
  10. "github.com/docker/docker/integration-cli/cli"
  11. "github.com/docker/docker/integration-cli/cli/build"
  12. "github.com/docker/docker/internal/test/fakecontext"
  13. "github.com/docker/docker/pkg/stringid"
  14. "github.com/docker/go-connections/nat"
  15. "github.com/go-check/check"
  16. )
  17. // Make sure we can create a simple container with some args
  18. func (s *DockerSuite) TestCreateArgs(c *check.C) {
  19. // Intentionally clear entrypoint, as the Windows busybox image needs an entrypoint, which breaks this test
  20. out, _ := dockerCmd(c, "create", "--entrypoint=", "busybox", "command", "arg1", "arg2", "arg with space", "-c", "flags")
  21. cleanedContainerID := strings.TrimSpace(out)
  22. out, _ = dockerCmd(c, "inspect", cleanedContainerID)
  23. var containers []struct {
  24. ID string
  25. Created time.Time
  26. Path string
  27. Args []string
  28. Image string
  29. }
  30. err := json.Unmarshal([]byte(out), &containers)
  31. c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
  32. c.Assert(containers, checker.HasLen, 1)
  33. cont := containers[0]
  34. c.Assert(string(cont.Path), checker.Equals, "command", check.Commentf("Unexpected container path. Expected command, received: %s", cont.Path))
  35. b := false
  36. expected := []string{"arg1", "arg2", "arg with space", "-c", "flags"}
  37. for i, arg := range expected {
  38. if arg != cont.Args[i] {
  39. b = true
  40. break
  41. }
  42. }
  43. if len(cont.Args) != len(expected) || b {
  44. c.Fatalf("Unexpected args. Expected %v, received: %v", expected, cont.Args)
  45. }
  46. }
  47. // Make sure we can grow the container's rootfs at creation time.
  48. func (s *DockerSuite) TestCreateGrowRootfs(c *check.C) {
  49. // Windows and Devicemapper support growing the rootfs
  50. if testEnv.OSType != "windows" {
  51. testRequires(c, Devicemapper)
  52. }
  53. out, _ := dockerCmd(c, "create", "--storage-opt", "size=120G", "busybox")
  54. cleanedContainerID := strings.TrimSpace(out)
  55. inspectOut := inspectField(c, cleanedContainerID, "HostConfig.StorageOpt")
  56. c.Assert(inspectOut, checker.Equals, "map[size:120G]")
  57. }
  58. // Make sure we cannot shrink the container's rootfs at creation time.
  59. func (s *DockerSuite) TestCreateShrinkRootfs(c *check.C) {
  60. testRequires(c, Devicemapper)
  61. // Ensure this fails because of the defaultBaseFsSize is 10G
  62. out, _, err := dockerCmdWithError("create", "--storage-opt", "size=5G", "busybox")
  63. c.Assert(err, check.NotNil, check.Commentf(out))
  64. c.Assert(out, checker.Contains, "Container size cannot be smaller than")
  65. }
  66. // Make sure we can set hostconfig options too
  67. func (s *DockerSuite) TestCreateHostConfig(c *check.C) {
  68. out, _ := dockerCmd(c, "create", "-P", "busybox", "echo")
  69. cleanedContainerID := strings.TrimSpace(out)
  70. out, _ = dockerCmd(c, "inspect", cleanedContainerID)
  71. var containers []struct {
  72. HostConfig *struct {
  73. PublishAllPorts bool
  74. }
  75. }
  76. err := json.Unmarshal([]byte(out), &containers)
  77. c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
  78. c.Assert(containers, checker.HasLen, 1)
  79. cont := containers[0]
  80. c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
  81. c.Assert(cont.HostConfig.PublishAllPorts, check.NotNil, check.Commentf("Expected PublishAllPorts, got false"))
  82. }
  83. func (s *DockerSuite) TestCreateWithPortRange(c *check.C) {
  84. out, _ := dockerCmd(c, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
  85. cleanedContainerID := strings.TrimSpace(out)
  86. out, _ = dockerCmd(c, "inspect", cleanedContainerID)
  87. var containers []struct {
  88. HostConfig *struct {
  89. PortBindings map[nat.Port][]nat.PortBinding
  90. }
  91. }
  92. err := json.Unmarshal([]byte(out), &containers)
  93. c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
  94. c.Assert(containers, checker.HasLen, 1)
  95. cont := containers[0]
  96. c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
  97. c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 4, check.Commentf("Expected 4 ports bindings, got %d", len(cont.HostConfig.PortBindings)))
  98. for k, v := range cont.HostConfig.PortBindings {
  99. c.Assert(v, checker.HasLen, 1, check.Commentf("Expected 1 ports binding, for the port %s but found %s", k, v))
  100. c.Assert(k.Port(), checker.Equals, v[0].HostPort, check.Commentf("Expected host port %s to match published port %s", k.Port(), v[0].HostPort))
  101. }
  102. }
  103. func (s *DockerSuite) TestCreateWithLargePortRange(c *check.C) {
  104. out, _ := dockerCmd(c, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
  105. cleanedContainerID := strings.TrimSpace(out)
  106. out, _ = dockerCmd(c, "inspect", cleanedContainerID)
  107. var containers []struct {
  108. HostConfig *struct {
  109. PortBindings map[nat.Port][]nat.PortBinding
  110. }
  111. }
  112. err := json.Unmarshal([]byte(out), &containers)
  113. c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
  114. c.Assert(containers, checker.HasLen, 1)
  115. cont := containers[0]
  116. c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
  117. c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 65535)
  118. for k, v := range cont.HostConfig.PortBindings {
  119. c.Assert(v, checker.HasLen, 1)
  120. c.Assert(k.Port(), checker.Equals, v[0].HostPort, check.Commentf("Expected host port %s to match published port %s", k.Port(), v[0].HostPort))
  121. }
  122. }
  123. // "test123" should be printed by docker create + start
  124. func (s *DockerSuite) TestCreateEchoStdout(c *check.C) {
  125. out, _ := dockerCmd(c, "create", "busybox", "echo", "test123")
  126. cleanedContainerID := strings.TrimSpace(out)
  127. out, _ = dockerCmd(c, "start", "-ai", cleanedContainerID)
  128. c.Assert(out, checker.Equals, "test123\n", check.Commentf("container should've printed 'test123', got %q", out))
  129. }
  130. func (s *DockerSuite) TestCreateVolumesCreated(c *check.C) {
  131. testRequires(c, SameHostDaemon)
  132. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  133. name := "test_create_volume"
  134. dockerCmd(c, "create", "--name", name, "-v", prefix+slash+"foo", "busybox")
  135. dir, err := inspectMountSourceField(name, prefix+slash+"foo")
  136. c.Assert(err, check.IsNil, check.Commentf("Error getting volume host path: %q", err))
  137. if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
  138. c.Fatalf("Volume was not created")
  139. }
  140. if err != nil {
  141. c.Fatalf("Error statting volume host path: %q", err)
  142. }
  143. }
  144. func (s *DockerSuite) TestCreateLabels(c *check.C) {
  145. name := "test_create_labels"
  146. expected := map[string]string{"k1": "v1", "k2": "v2"}
  147. dockerCmd(c, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox")
  148. actual := make(map[string]string)
  149. inspectFieldAndUnmarshall(c, name, "Config.Labels", &actual)
  150. if !reflect.DeepEqual(expected, actual) {
  151. c.Fatalf("Expected %s got %s", expected, actual)
  152. }
  153. }
  154. func (s *DockerSuite) TestCreateLabelFromImage(c *check.C) {
  155. imageName := "testcreatebuildlabel"
  156. buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
  157. LABEL k1=v1 k2=v2`))
  158. name := "test_create_labels_from_image"
  159. expected := map[string]string{"k2": "x", "k3": "v3", "k1": "v1"}
  160. dockerCmd(c, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName)
  161. actual := make(map[string]string)
  162. inspectFieldAndUnmarshall(c, name, "Config.Labels", &actual)
  163. if !reflect.DeepEqual(expected, actual) {
  164. c.Fatalf("Expected %s got %s", expected, actual)
  165. }
  166. }
  167. func (s *DockerSuite) TestCreateHostnameWithNumber(c *check.C) {
  168. image := "busybox"
  169. // Busybox on Windows does not implement hostname command
  170. if testEnv.OSType == "windows" {
  171. image = testEnv.PlatformDefaults.BaseImage
  172. }
  173. out, _ := dockerCmd(c, "run", "-h", "web.0", image, "hostname")
  174. c.Assert(strings.TrimSpace(out), checker.Equals, "web.0", check.Commentf("hostname not set, expected `web.0`, got: %s", out))
  175. }
  176. func (s *DockerSuite) TestCreateRM(c *check.C) {
  177. // Test to make sure we can 'rm' a new container that is in
  178. // "Created" state, and has ever been run. Test "rm -f" too.
  179. // create a container
  180. out, _ := dockerCmd(c, "create", "busybox")
  181. cID := strings.TrimSpace(out)
  182. dockerCmd(c, "rm", cID)
  183. // Now do it again so we can "rm -f" this time
  184. out, _ = dockerCmd(c, "create", "busybox")
  185. cID = strings.TrimSpace(out)
  186. dockerCmd(c, "rm", "-f", cID)
  187. }
  188. func (s *DockerSuite) TestCreateModeIpcContainer(c *check.C) {
  189. // Uses Linux specific functionality (--ipc)
  190. testRequires(c, DaemonIsLinux, SameHostDaemon)
  191. out, _ := dockerCmd(c, "create", "busybox")
  192. id := strings.TrimSpace(out)
  193. dockerCmd(c, "create", fmt.Sprintf("--ipc=container:%s", id), "busybox")
  194. }
  195. func (s *DockerSuite) TestCreateByImageID(c *check.C) {
  196. imageName := "testcreatebyimageid"
  197. buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
  198. MAINTAINER dockerio`))
  199. imageID := getIDByName(c, imageName)
  200. truncatedImageID := stringid.TruncateID(imageID)
  201. dockerCmd(c, "create", imageID)
  202. dockerCmd(c, "create", truncatedImageID)
  203. // Ensure this fails
  204. out, exit, _ := dockerCmdWithError("create", fmt.Sprintf("%s:%s", imageName, imageID))
  205. if exit == 0 {
  206. c.Fatalf("expected non-zero exit code; received %d", exit)
  207. }
  208. if expected := "invalid reference format"; !strings.Contains(out, expected) {
  209. c.Fatalf(`Expected %q in output; got: %s`, expected, out)
  210. }
  211. if i := strings.IndexRune(imageID, ':'); i >= 0 {
  212. imageID = imageID[i+1:]
  213. }
  214. out, exit, _ = dockerCmdWithError("create", fmt.Sprintf("%s:%s", "wrongimage", imageID))
  215. if exit == 0 {
  216. c.Fatalf("expected non-zero exit code; received %d", exit)
  217. }
  218. if expected := "Unable to find image"; !strings.Contains(out, expected) {
  219. c.Fatalf(`Expected %q in output; got: %s`, expected, out)
  220. }
  221. }
  222. func (s *DockerSuite) TestCreateStopSignal(c *check.C) {
  223. name := "test_create_stop_signal"
  224. dockerCmd(c, "create", "--name", name, "--stop-signal", "9", "busybox")
  225. res := inspectFieldJSON(c, name, "Config.StopSignal")
  226. c.Assert(res, checker.Contains, "9")
  227. }
  228. func (s *DockerSuite) TestCreateWithWorkdir(c *check.C) {
  229. name := "foo"
  230. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  231. dir := prefix + slash + "home" + slash + "foo" + slash + "bar"
  232. dockerCmd(c, "create", "--name", name, "-w", dir, "busybox")
  233. // Windows does not create the workdir until the container is started
  234. if testEnv.OSType == "windows" {
  235. dockerCmd(c, "start", name)
  236. }
  237. dockerCmd(c, "cp", fmt.Sprintf("%s:%s", name, dir), prefix+slash+"tmp")
  238. }
  239. func (s *DockerSuite) TestCreateWithInvalidLogOpts(c *check.C) {
  240. name := "test-invalidate-log-opts"
  241. out, _, err := dockerCmdWithError("create", "--name", name, "--log-opt", "invalid=true", "busybox")
  242. c.Assert(err, checker.NotNil)
  243. c.Assert(out, checker.Contains, "unknown log opt")
  244. out, _ = dockerCmd(c, "ps", "-a")
  245. c.Assert(out, checker.Not(checker.Contains), name)
  246. }
  247. // #20972
  248. func (s *DockerSuite) TestCreate64ByteHexID(c *check.C) {
  249. out := inspectField(c, "busybox", "Id")
  250. imageID := strings.TrimPrefix(strings.TrimSpace(string(out)), "sha256:")
  251. dockerCmd(c, "create", imageID)
  252. }
  253. // Test case for #23498
  254. func (s *DockerSuite) TestCreateUnsetEntrypoint(c *check.C) {
  255. name := "test-entrypoint"
  256. dockerfile := `FROM busybox
  257. ADD entrypoint.sh /entrypoint.sh
  258. RUN chmod 755 /entrypoint.sh
  259. ENTRYPOINT ["/entrypoint.sh"]
  260. CMD echo foobar`
  261. ctx := fakecontext.New(c, "",
  262. fakecontext.WithDockerfile(dockerfile),
  263. fakecontext.WithFiles(map[string]string{
  264. "entrypoint.sh": `#!/bin/sh
  265. echo "I am an entrypoint"
  266. exec "$@"`,
  267. }))
  268. defer ctx.Close()
  269. cli.BuildCmd(c, name, build.WithExternalBuildContext(ctx))
  270. out := cli.DockerCmd(c, "create", "--entrypoint=", name, "echo", "foo").Combined()
  271. id := strings.TrimSpace(out)
  272. c.Assert(id, check.Not(check.Equals), "")
  273. out = cli.DockerCmd(c, "start", "-a", id).Combined()
  274. c.Assert(strings.TrimSpace(out), check.Equals, "foo")
  275. }
  276. // #22471
  277. func (s *DockerSuite) TestCreateStopTimeout(c *check.C) {
  278. name1 := "test_create_stop_timeout_1"
  279. dockerCmd(c, "create", "--name", name1, "--stop-timeout", "15", "busybox")
  280. res := inspectFieldJSON(c, name1, "Config.StopTimeout")
  281. c.Assert(res, checker.Contains, "15")
  282. name2 := "test_create_stop_timeout_2"
  283. dockerCmd(c, "create", "--name", name2, "busybox")
  284. res = inspectFieldJSON(c, name2, "Config.StopTimeout")
  285. c.Assert(res, checker.Contains, "null")
  286. }