docker_cli_exec_test.go 18 KB

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