docker_cli_exec_test.go 18 KB

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