docker_cli_exec_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // +build !test_no_exec
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "reflect"
  11. "sort"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/docker/docker/pkg/integration/checker"
  16. "github.com/go-check/check"
  17. )
  18. func (s *DockerSuite) TestExec(c *check.C) {
  19. testRequires(c, DaemonIsLinux)
  20. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  21. out, _ := dockerCmd(c, "exec", "testing", "cat", "/tmp/file")
  22. out = strings.Trim(out, "\r\n")
  23. c.Assert(out, checker.Equals, "test")
  24. }
  25. func (s *DockerSuite) TestExecInteractive(c *check.C) {
  26. testRequires(c, DaemonIsLinux)
  27. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  28. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  29. stdin, err := execCmd.StdinPipe()
  30. c.Assert(err, checker.IsNil)
  31. stdout, err := execCmd.StdoutPipe()
  32. c.Assert(err, checker.IsNil)
  33. err = execCmd.Start()
  34. c.Assert(err, checker.IsNil)
  35. _, err = stdin.Write([]byte("cat /tmp/file\n"))
  36. c.Assert(err, checker.IsNil)
  37. r := bufio.NewReader(stdout)
  38. line, err := r.ReadString('\n')
  39. c.Assert(err, checker.IsNil)
  40. line = strings.TrimSpace(line)
  41. c.Assert(line, checker.Equals, "test")
  42. err = stdin.Close()
  43. c.Assert(err, checker.IsNil)
  44. errChan := make(chan error)
  45. go func() {
  46. errChan <- execCmd.Wait()
  47. close(errChan)
  48. }()
  49. select {
  50. case err := <-errChan:
  51. c.Assert(err, checker.IsNil)
  52. case <-time.After(1 * time.Second):
  53. c.Fatal("docker exec failed to exit on stdin close")
  54. }
  55. }
  56. func (s *DockerSuite) TestExecAfterContainerRestart(c *check.C) {
  57. out, _ := runSleepingContainer(c, "-d")
  58. cleanedContainerID := strings.TrimSpace(out)
  59. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  60. dockerCmd(c, "restart", cleanedContainerID)
  61. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  62. out, _ = dockerCmd(c, "exec", cleanedContainerID, "echo", "hello")
  63. outStr := strings.TrimSpace(out)
  64. c.Assert(outStr, checker.Equals, "hello")
  65. }
  66. func (s *DockerDaemonSuite) TestExecAfterDaemonRestart(c *check.C) {
  67. // TODO Windows CI: Requires a little work to get this ported.
  68. testRequires(c, DaemonIsLinux)
  69. testRequires(c, SameHostDaemon)
  70. err := s.d.StartWithBusybox()
  71. c.Assert(err, checker.IsNil)
  72. out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top")
  73. c.Assert(err, checker.IsNil, check.Commentf("Could not run top: %s", out))
  74. err = s.d.Restart()
  75. c.Assert(err, checker.IsNil, check.Commentf("Could not restart daemon"))
  76. out, err = s.d.Cmd("start", "top")
  77. c.Assert(err, checker.IsNil, check.Commentf("Could not start top after daemon restart: %s", out))
  78. out, err = s.d.Cmd("exec", "top", "echo", "hello")
  79. c.Assert(err, checker.IsNil, check.Commentf("Could not exec on container top: %s", out))
  80. outStr := strings.TrimSpace(string(out))
  81. c.Assert(outStr, checker.Equals, "hello")
  82. }
  83. // Regression test for #9155, #9044
  84. func (s *DockerSuite) TestExecEnv(c *check.C) {
  85. // TODO Windows CI: This one is interesting and may just end up being a feature
  86. // difference between Windows and Linux. On Windows, the environment is passed
  87. // into the process that is launched, not into the machine environment. Hence
  88. // a subsequent exec will not have LALA set/
  89. testRequires(c, DaemonIsLinux)
  90. runSleepingContainer(c, "-e", "LALA=value1", "-e", "LALA=value2", "-d", "--name", "testing")
  91. c.Assert(waitRun("testing"), check.IsNil)
  92. out, _ := dockerCmd(c, "exec", "testing", "env")
  93. c.Assert(out, checker.Not(checker.Contains), "LALA=value1")
  94. c.Assert(out, checker.Contains, "LALA=value2")
  95. c.Assert(out, checker.Contains, "HOME=/root")
  96. }
  97. func (s *DockerSuite) TestExecExitStatus(c *check.C) {
  98. runSleepingContainer(c, "-d", "--name", "top")
  99. // Test normal (non-detached) case first
  100. cmd := exec.Command(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
  101. ec, _ := runCommand(cmd)
  102. c.Assert(ec, checker.Equals, 23)
  103. }
  104. func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
  105. // Windows does not support pause
  106. testRequires(c, DaemonIsLinux)
  107. defer unpauseAllContainers()
  108. out, _ := dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  109. ContainerID := strings.TrimSpace(out)
  110. dockerCmd(c, "pause", "testing")
  111. out, _, err := dockerCmdWithError("exec", "-i", "-t", ContainerID, "echo", "hello")
  112. c.Assert(err, checker.NotNil, check.Commentf("container should fail to exec new conmmand if it is paused"))
  113. expected := ContainerID + " is paused, unpause the container before exec"
  114. c.Assert(out, checker.Contains, expected, check.Commentf("container should not exec new command if it is paused"))
  115. }
  116. // regression test for #9476
  117. func (s *DockerSuite) TestExecTTYCloseStdin(c *check.C) {
  118. // TODO Windows CI: This requires some work to port to Windows.
  119. testRequires(c, DaemonIsLinux)
  120. dockerCmd(c, "run", "-d", "-it", "--name", "exec_tty_stdin", "busybox")
  121. cmd := exec.Command(dockerBinary, "exec", "-i", "exec_tty_stdin", "cat")
  122. stdinRw, err := cmd.StdinPipe()
  123. c.Assert(err, checker.IsNil)
  124. stdinRw.Write([]byte("test"))
  125. stdinRw.Close()
  126. out, _, err := runCommandWithOutput(cmd)
  127. c.Assert(err, checker.IsNil, check.Commentf(out))
  128. out, _ = dockerCmd(c, "top", "exec_tty_stdin")
  129. outArr := strings.Split(out, "\n")
  130. c.Assert(len(outArr), checker.LessOrEqualThan, 3, check.Commentf("exec process left running"))
  131. c.Assert(out, checker.Not(checker.Contains), "nsenter-exec")
  132. }
  133. func (s *DockerSuite) TestExecTTYWithoutStdin(c *check.C) {
  134. // TODO Windows CI: This requires some work to port to Windows.
  135. testRequires(c, DaemonIsLinux)
  136. out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
  137. id := strings.TrimSpace(out)
  138. c.Assert(waitRun(id), checker.IsNil)
  139. errChan := make(chan error)
  140. go func() {
  141. defer close(errChan)
  142. cmd := exec.Command(dockerBinary, "exec", "-ti", id, "true")
  143. if _, err := cmd.StdinPipe(); err != nil {
  144. errChan <- err
  145. return
  146. }
  147. expected := "cannot enable tty mode"
  148. if out, _, err := runCommandWithOutput(cmd); err == nil {
  149. errChan <- fmt.Errorf("exec should have failed")
  150. return
  151. } else if !strings.Contains(out, expected) {
  152. errChan <- fmt.Errorf("exec failed with error %q: expected %q", out, expected)
  153. return
  154. }
  155. }()
  156. select {
  157. case err := <-errChan:
  158. c.Assert(err, check.IsNil)
  159. case <-time.After(3 * time.Second):
  160. c.Fatal("exec is running but should have failed")
  161. }
  162. }
  163. func (s *DockerSuite) TestExecParseError(c *check.C) {
  164. // TODO Windows CI: Requires some extra work. Consider copying the
  165. // runSleepingContainer helper to have an exec version.
  166. testRequires(c, DaemonIsLinux)
  167. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  168. // Test normal (non-detached) case first
  169. cmd := exec.Command(dockerBinary, "exec", "top")
  170. _, stderr, _, err := runCommandWithStdoutStderr(cmd)
  171. c.Assert(err, checker.NotNil)
  172. c.Assert(stderr, checker.Contains, "See '"+dockerBinary+" exec --help'")
  173. }
  174. func (s *DockerSuite) TestExecStopNotHanging(c *check.C) {
  175. // TODO Windows CI: Requires some extra work. Consider copying the
  176. // runSleepingContainer helper to have an exec version.
  177. testRequires(c, DaemonIsLinux)
  178. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  179. err := exec.Command(dockerBinary, "exec", "testing", "top").Start()
  180. c.Assert(err, checker.IsNil)
  181. type dstop struct {
  182. out []byte
  183. err error
  184. }
  185. ch := make(chan dstop)
  186. go func() {
  187. out, err := exec.Command(dockerBinary, "stop", "testing").CombinedOutput()
  188. ch <- dstop{out, err}
  189. close(ch)
  190. }()
  191. select {
  192. case <-time.After(3 * time.Second):
  193. c.Fatal("Container stop timed out")
  194. case s := <-ch:
  195. c.Assert(s.err, check.IsNil)
  196. }
  197. }
  198. func (s *DockerSuite) TestExecCgroup(c *check.C) {
  199. // Not applicable on Windows - using Linux specific functionality
  200. testRequires(c, NotUserNamespace)
  201. testRequires(c, DaemonIsLinux)
  202. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  203. out, _ := dockerCmd(c, "exec", "testing", "cat", "/proc/1/cgroup")
  204. containerCgroups := sort.StringSlice(strings.Split(out, "\n"))
  205. var wg sync.WaitGroup
  206. var mu sync.Mutex
  207. execCgroups := []sort.StringSlice{}
  208. errChan := make(chan error)
  209. // exec a few times concurrently to get consistent failure
  210. for i := 0; i < 5; i++ {
  211. wg.Add(1)
  212. go func() {
  213. out, _, err := dockerCmdWithError("exec", "testing", "cat", "/proc/self/cgroup")
  214. if err != nil {
  215. errChan <- err
  216. return
  217. }
  218. cg := sort.StringSlice(strings.Split(out, "\n"))
  219. mu.Lock()
  220. execCgroups = append(execCgroups, cg)
  221. mu.Unlock()
  222. wg.Done()
  223. }()
  224. }
  225. wg.Wait()
  226. close(errChan)
  227. for err := range errChan {
  228. c.Assert(err, checker.IsNil)
  229. }
  230. for _, cg := range execCgroups {
  231. if !reflect.DeepEqual(cg, containerCgroups) {
  232. fmt.Println("exec cgroups:")
  233. for _, name := range cg {
  234. fmt.Printf(" %s\n", name)
  235. }
  236. fmt.Println("container cgroups:")
  237. for _, name := range containerCgroups {
  238. fmt.Printf(" %s\n", name)
  239. }
  240. c.Fatal("cgroups mismatched")
  241. }
  242. }
  243. }
  244. func (s *DockerSuite) TestExecInspectID(c *check.C) {
  245. out, _ := runSleepingContainer(c, "-d")
  246. id := strings.TrimSuffix(out, "\n")
  247. out, err := inspectField(id, "ExecIDs")
  248. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect container: %s", out))
  249. c.Assert(out, checker.Equals, "[]", check.Commentf("ExecIDs should be empty, got: %s", out))
  250. // Start an exec, have it block waiting so we can do some checking
  251. cmd := exec.Command(dockerBinary, "exec", id, "sh", "-c",
  252. "while ! test -e /execid1; do sleep 1; done")
  253. err = cmd.Start()
  254. c.Assert(err, checker.IsNil, check.Commentf("failed to start the exec cmd"))
  255. // Give the exec 10 chances/seconds to start then give up and stop the test
  256. tries := 10
  257. for i := 0; i < tries; i++ {
  258. // Since its still running we should see exec as part of the container
  259. out, err = inspectField(id, "ExecIDs")
  260. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect container: %s", out))
  261. out = strings.TrimSuffix(out, "\n")
  262. if out != "[]" && out != "<no value>" {
  263. break
  264. }
  265. c.Assert(i+1, checker.Not(checker.Equals), tries, check.Commentf("ExecIDs should be empty, got: %s", out))
  266. time.Sleep(1 * time.Second)
  267. }
  268. // Save execID for later
  269. execID, err := inspectFilter(id, "index .ExecIDs 0")
  270. c.Assert(err, checker.IsNil, check.Commentf("failed to get the exec id"))
  271. // End the exec by creating the missing file
  272. err = exec.Command(dockerBinary, "exec", id,
  273. "sh", "-c", "touch /execid1").Run()
  274. c.Assert(err, checker.IsNil, check.Commentf("failed to run the 2nd exec cmd"))
  275. // Wait for 1st exec to complete
  276. cmd.Wait()
  277. // All execs for the container should be gone now
  278. out, err = inspectField(id, "ExecIDs")
  279. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect container: %s", out))
  280. out = strings.TrimSuffix(out, "\n")
  281. c.Assert(out == "[]" || out == "<no value>", checker.True)
  282. // But we should still be able to query the execID
  283. sc, body, err := sockRequest("GET", "/exec/"+execID+"/json", nil)
  284. c.Assert(sc, checker.Equals, http.StatusOK, check.Commentf("received status != 200 OK: %d\n%s", sc, body))
  285. // Now delete the container and then an 'inspect' on the exec should
  286. // result in a 404 (not 'container not running')
  287. out, ec := dockerCmd(c, "rm", "-f", id)
  288. c.Assert(ec, checker.Equals, 0, check.Commentf("error removing container: %s", out))
  289. sc, body, err = sockRequest("GET", "/exec/"+execID+"/json", nil)
  290. c.Assert(sc, checker.Equals, http.StatusNotFound, check.Commentf("received status != 404: %d\n%s", sc, body))
  291. }
  292. func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
  293. // Problematic on Windows as Windows does not support links
  294. testRequires(c, DaemonIsLinux)
  295. var out string
  296. out, _ = dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  297. idA := strings.TrimSpace(out)
  298. c.Assert(idA, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out))
  299. out, _ = dockerCmd(c, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "top")
  300. idB := strings.TrimSpace(out)
  301. c.Assert(idB, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out))
  302. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  303. dockerCmd(c, "rename", "container1", "container_new")
  304. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  305. }
  306. func (s *DockerSuite) TestExecDir(c *check.C) {
  307. // TODO Windows CI. This requires some work to port as it uses execDriverPath
  308. // which is currently (and incorrectly) hard coded as a string assuming
  309. // the daemon is running Linux :(
  310. testRequires(c, SameHostDaemon, DaemonIsLinux)
  311. out, _ := runSleepingContainer(c, "-d")
  312. id := strings.TrimSpace(out)
  313. execDir := filepath.Join(execDriverPath, id)
  314. fmt.Println(execDriverPath)
  315. stateFile := filepath.Join(execDir, "state.json")
  316. {
  317. fi, err := os.Stat(execDir)
  318. c.Assert(err, checker.IsNil)
  319. if !fi.IsDir() {
  320. c.Fatalf("%q must be a directory", execDir)
  321. }
  322. fi, err = os.Stat(stateFile)
  323. c.Assert(err, checker.IsNil)
  324. }
  325. dockerCmd(c, "stop", id)
  326. {
  327. _, err := os.Stat(execDir)
  328. c.Assert(err, checker.NotNil)
  329. c.Assert(err, checker.NotNil, check.Commentf("Exec directory %q exists for removed container!", execDir))
  330. if !os.IsNotExist(err) {
  331. c.Fatalf("Error should be about non-existing, got %s", err)
  332. }
  333. }
  334. dockerCmd(c, "start", id)
  335. {
  336. fi, err := os.Stat(execDir)
  337. c.Assert(err, checker.IsNil)
  338. if !fi.IsDir() {
  339. c.Fatalf("%q must be a directory", execDir)
  340. }
  341. fi, err = os.Stat(stateFile)
  342. c.Assert(err, checker.IsNil)
  343. }
  344. dockerCmd(c, "rm", "-f", id)
  345. {
  346. _, err := os.Stat(execDir)
  347. c.Assert(err, checker.NotNil, check.Commentf("Exec directory %q exists for removed container!", execDir))
  348. if !os.IsNotExist(err) {
  349. c.Fatalf("Error should be about non-existing, got %s", err)
  350. }
  351. }
  352. }
  353. func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
  354. // Not applicable on Windows to Windows CI.
  355. testRequires(c, SameHostDaemon, DaemonIsLinux)
  356. for _, fn := range []string{"resolv.conf", "hosts"} {
  357. deleteAllContainers()
  358. content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)))
  359. c.Assert(err, checker.IsNil)
  360. c.Assert(strings.TrimSpace(string(content)), checker.Equals, "success", check.Commentf("Content was not what was modified in the container", string(content)))
  361. out, _ := dockerCmd(c, "run", "-d", "--name", "c2", "busybox", "top")
  362. contID := strings.TrimSpace(out)
  363. netFilePath := containerStorageFile(contID, fn)
  364. f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
  365. c.Assert(err, checker.IsNil)
  366. if _, err := f.Seek(0, 0); err != nil {
  367. f.Close()
  368. c.Fatal(err)
  369. }
  370. if err := f.Truncate(0); err != nil {
  371. f.Close()
  372. c.Fatal(err)
  373. }
  374. if _, err := f.Write([]byte("success2\n")); err != nil {
  375. f.Close()
  376. c.Fatal(err)
  377. }
  378. f.Close()
  379. res, _ := dockerCmd(c, "exec", contID, "cat", "/etc/"+fn)
  380. c.Assert(res, checker.Equals, "success2\n")
  381. }
  382. }
  383. func (s *DockerSuite) TestExecWithUser(c *check.C) {
  384. // TODO Windows CI: This may be fixable in the future once Windows
  385. // supports users
  386. testRequires(c, DaemonIsLinux)
  387. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  388. out, _ := dockerCmd(c, "exec", "-u", "1", "parent", "id")
  389. c.Assert(out, checker.Contains, "uid=1(daemon) gid=1(daemon)")
  390. out, _ = dockerCmd(c, "exec", "-u", "root", "parent", "id")
  391. c.Assert(out, checker.Contains, "uid=0(root) gid=0(root)", check.Commentf("exec with user by id expected daemon user got %s", out))
  392. }
  393. func (s *DockerSuite) TestExecWithPrivileged(c *check.C) {
  394. // Not applicable on Windows
  395. testRequires(c, DaemonIsLinux, NotUserNamespace)
  396. // Start main loop which attempts mknod repeatedly
  397. dockerCmd(c, "run", "-d", "--name", "parent", "--cap-drop=ALL", "busybox", "sh", "-c", `while (true); do if [ -e /exec_priv ]; then cat /exec_priv && mknod /tmp/sda b 8 0 && echo "Success"; else echo "Privileged exec has not run yet"; fi; usleep 10000; done`)
  398. // Check exec mknod doesn't work
  399. cmd := exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdb b 8 16")
  400. out, _, err := runCommandWithOutput(cmd)
  401. c.Assert(err, checker.NotNil, check.Commentf("exec mknod in --cap-drop=ALL container without --privileged should fail"))
  402. c.Assert(out, checker.Contains, "Operation not permitted", check.Commentf("exec mknod in --cap-drop=ALL container without --privileged should fail"))
  403. // Check exec mknod does work with --privileged
  404. cmd = exec.Command(dockerBinary, "exec", "--privileged", "parent", "sh", "-c", `echo "Running exec --privileged" > /exec_priv && mknod /tmp/sdb b 8 16 && usleep 50000 && echo "Finished exec --privileged" > /exec_priv && echo ok`)
  405. out, _, err = runCommandWithOutput(cmd)
  406. c.Assert(err, checker.IsNil)
  407. actual := strings.TrimSpace(out)
  408. c.Assert(actual, checker.Equals, "ok", check.Commentf("exec mknod in --cap-drop=ALL container with --privileged failed, output: %q", out))
  409. // Check subsequent unprivileged exec cannot mknod
  410. cmd = exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdc b 8 32")
  411. out, _, err = runCommandWithOutput(cmd)
  412. c.Assert(err, checker.NotNil, check.Commentf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail"))
  413. c.Assert(out, checker.Contains, "Operation not permitted", check.Commentf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail"))
  414. // Confirm at no point was mknod allowed
  415. logCmd := exec.Command(dockerBinary, "logs", "parent")
  416. out, _, err = runCommandWithOutput(logCmd)
  417. c.Assert(err, checker.IsNil)
  418. c.Assert(out, checker.Not(checker.Contains), "Success")
  419. }
  420. func (s *DockerSuite) TestExecWithImageUser(c *check.C) {
  421. // Not applicable on Windows
  422. testRequires(c, DaemonIsLinux)
  423. name := "testbuilduser"
  424. _, err := buildImage(name,
  425. `FROM busybox
  426. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  427. USER dockerio`,
  428. true)
  429. c.Assert(err, checker.IsNil)
  430. dockerCmd(c, "run", "-d", "--name", "dockerioexec", name, "top")
  431. out, _ := dockerCmd(c, "exec", "dockerioexec", "whoami")
  432. c.Assert(out, checker.Contains, "dockerio", check.Commentf("exec with user by id expected dockerio user got %s", out))
  433. }
  434. func (s *DockerSuite) TestExecOnReadonlyContainer(c *check.C) {
  435. // Windows does not support read-only
  436. // --read-only + userns has remount issues
  437. testRequires(c, DaemonIsLinux, NotUserNamespace)
  438. dockerCmd(c, "run", "-d", "--read-only", "--name", "parent", "busybox", "top")
  439. dockerCmd(c, "exec", "parent", "true")
  440. }
  441. // #15750
  442. func (s *DockerSuite) TestExecStartFails(c *check.C) {
  443. // TODO Windows CI. This test should be portable. Figure out why it fails
  444. // currently.
  445. testRequires(c, DaemonIsLinux)
  446. name := "exec-15750"
  447. runSleepingContainer(c, "-d", "--name", name)
  448. c.Assert(waitRun(name), checker.IsNil)
  449. out, _, err := dockerCmdWithError("exec", name, "no-such-cmd")
  450. c.Assert(err, checker.NotNil, check.Commentf(out))
  451. c.Assert(out, checker.Contains, "executable file not found")
  452. }