docker_cli_exec_test.go 19 KB

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