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