docker_cli_run_test.go 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "reflect"
  12. "regexp"
  13. "sort"
  14. "strings"
  15. "sync"
  16. "testing"
  17. "time"
  18. "github.com/docker/docker/pkg/mount"
  19. "github.com/docker/docker/pkg/networkfs/resolvconf"
  20. "github.com/kr/pty"
  21. )
  22. // "test123" should be printed by docker run
  23. func TestRunEchoStdout(t *testing.T) {
  24. runCmd := exec.Command(dockerBinary, "run", "busybox", "echo", "test123")
  25. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  26. if err != nil {
  27. t.Fatalf("failed to run container: %v, output: %q", err, out)
  28. }
  29. if out != "test123\n" {
  30. t.Errorf("container should've printed 'test123'")
  31. }
  32. deleteAllContainers()
  33. logDone("run - echo test123")
  34. }
  35. // "test" should be printed
  36. func TestRunEchoStdoutWithMemoryLimit(t *testing.T) {
  37. runCmd := exec.Command(dockerBinary, "run", "-m", "4m", "busybox", "echo", "test")
  38. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  39. if err != nil {
  40. t.Fatalf("failed to run container: %v, output: %q", err, out)
  41. }
  42. out = strings.Trim(out, "\r\n")
  43. if expected := "test"; out != expected {
  44. t.Errorf("container should've printed %q but printed %q", expected, out)
  45. }
  46. deleteAllContainers()
  47. logDone("run - echo with memory limit")
  48. }
  49. // "test" should be printed
  50. func TestRunEchoStdoutWitCPULimit(t *testing.T) {
  51. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "busybox", "echo", "test")
  52. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  53. if err != nil {
  54. t.Fatalf("failed to run container: %v, output: %q", err, out)
  55. }
  56. if out != "test\n" {
  57. t.Errorf("container should've printed 'test'")
  58. }
  59. deleteAllContainers()
  60. logDone("run - echo with CPU limit")
  61. }
  62. // "test" should be printed
  63. func TestRunEchoStdoutWithCPUAndMemoryLimit(t *testing.T) {
  64. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "-m", "4m", "busybox", "echo", "test")
  65. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  66. if err != nil {
  67. t.Fatalf("failed to run container: %v, output: %q", err, out)
  68. }
  69. if out != "test\n" {
  70. t.Errorf("container should've printed 'test', got %q instead", out)
  71. }
  72. deleteAllContainers()
  73. logDone("run - echo with CPU and memory limit")
  74. }
  75. // "test" should be printed
  76. func TestRunEchoNamedContainer(t *testing.T) {
  77. runCmd := exec.Command(dockerBinary, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test")
  78. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  79. if err != nil {
  80. t.Fatalf("failed to run container: %v, output: %q", err, out)
  81. }
  82. if out != "test\n" {
  83. t.Errorf("container should've printed 'test'")
  84. }
  85. if err := deleteContainer("testfoonamedcontainer"); err != nil {
  86. t.Errorf("failed to remove the named container: %v", err)
  87. }
  88. deleteAllContainers()
  89. logDone("run - echo with named container")
  90. }
  91. // docker run should not leak file descriptors
  92. func TestRunLeakyFileDescriptors(t *testing.T) {
  93. runCmd := exec.Command(dockerBinary, "run", "busybox", "ls", "-C", "/proc/self/fd")
  94. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  95. if err != nil {
  96. t.Fatalf("failed to run container: %v, output: %q", err, out)
  97. }
  98. // normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory
  99. if out != "0 1 2 3\n" {
  100. t.Errorf("container should've printed '0 1 2 3', not: %s", out)
  101. }
  102. deleteAllContainers()
  103. logDone("run - check file descriptor leakage")
  104. }
  105. // it should be possible to ping Google DNS resolver
  106. // this will fail when Internet access is unavailable
  107. func TestRunPingGoogle(t *testing.T) {
  108. runCmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "8.8.8.8")
  109. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  110. if err != nil {
  111. t.Fatalf("failed to run container: %v, output: %q", err, out)
  112. }
  113. errorOut(err, t, "container should've been able to ping 8.8.8.8")
  114. deleteAllContainers()
  115. logDone("run - ping 8.8.8.8")
  116. }
  117. // the exit code should be 0
  118. // some versions of lxc might make this test fail
  119. func TestRunExitCodeZero(t *testing.T) {
  120. runCmd := exec.Command(dockerBinary, "run", "busybox", "true")
  121. exitCode, err := runCommand(runCmd)
  122. errorOut(err, t, fmt.Sprintf("%s", err))
  123. if exitCode != 0 {
  124. t.Errorf("container should've exited with exit code 0")
  125. }
  126. deleteAllContainers()
  127. logDone("run - exit with 0")
  128. }
  129. // the exit code should be 1
  130. // some versions of lxc might make this test fail
  131. func TestRunExitCodeOne(t *testing.T) {
  132. runCmd := exec.Command(dockerBinary, "run", "busybox", "false")
  133. exitCode, err := runCommand(runCmd)
  134. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  135. t.Fatal(err)
  136. }
  137. if exitCode != 1 {
  138. t.Errorf("container should've exited with exit code 1")
  139. }
  140. deleteAllContainers()
  141. logDone("run - exit with 1")
  142. }
  143. // it should be possible to pipe in data via stdin to a process running in a container
  144. // some versions of lxc might make this test fail
  145. func TestRunStdinPipe(t *testing.T) {
  146. runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`)
  147. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  148. if err != nil {
  149. t.Fatalf("failed to run container: %v, output: %q", err, out)
  150. }
  151. out = stripTrailingCharacters(out)
  152. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  153. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  154. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  155. waitCmd := exec.Command(dockerBinary, "wait", out)
  156. _, _, err = runCommandWithOutput(waitCmd)
  157. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  158. logsCmd := exec.Command(dockerBinary, "logs", out)
  159. containerLogs, _, err := runCommandWithOutput(logsCmd)
  160. errorOut(err, t, fmt.Sprintf("error thrown while trying to get container logs: %s", err))
  161. containerLogs = stripTrailingCharacters(containerLogs)
  162. if containerLogs != "blahblah" {
  163. t.Errorf("logs didn't print the container's logs %s", containerLogs)
  164. }
  165. rmCmd := exec.Command(dockerBinary, "rm", out)
  166. _, _, err = runCommandWithOutput(rmCmd)
  167. errorOut(err, t, fmt.Sprintf("rm failed to remove container %s", err))
  168. deleteAllContainers()
  169. logDone("run - pipe in with -i -a stdin")
  170. }
  171. // the container's ID should be printed when starting a container in detached mode
  172. func TestRunDetachedContainerIDPrinting(t *testing.T) {
  173. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  174. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  175. if err != nil {
  176. t.Fatalf("failed to run container: %v, output: %q", err, out)
  177. }
  178. out = stripTrailingCharacters(out)
  179. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  180. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  181. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  182. waitCmd := exec.Command(dockerBinary, "wait", out)
  183. _, _, err = runCommandWithOutput(waitCmd)
  184. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  185. rmCmd := exec.Command(dockerBinary, "rm", out)
  186. rmOut, _, err := runCommandWithOutput(rmCmd)
  187. errorOut(err, t, "rm failed to remove container")
  188. rmOut = stripTrailingCharacters(rmOut)
  189. if rmOut != out {
  190. t.Errorf("rm didn't print the container ID %s %s", out, rmOut)
  191. }
  192. deleteAllContainers()
  193. logDone("run - print container ID in detached mode")
  194. }
  195. // the working directory should be set correctly
  196. func TestRunWorkingDirectory(t *testing.T) {
  197. runCmd := exec.Command(dockerBinary, "run", "-w", "/root", "busybox", "pwd")
  198. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  199. if err != nil {
  200. t.Fatalf("failed to run container: %v, output: %q", err, out)
  201. }
  202. out = stripTrailingCharacters(out)
  203. if out != "/root" {
  204. t.Errorf("-w failed to set working directory")
  205. }
  206. runCmd = exec.Command(dockerBinary, "run", "--workdir", "/root", "busybox", "pwd")
  207. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  208. errorOut(err, t, out)
  209. out = stripTrailingCharacters(out)
  210. if out != "/root" {
  211. t.Errorf("--workdir failed to set working directory")
  212. }
  213. deleteAllContainers()
  214. logDone("run - run with working directory set by -w")
  215. logDone("run - run with working directory set by --workdir")
  216. }
  217. // pinging Google's DNS resolver should fail when we disable the networking
  218. func TestRunWithoutNetworking(t *testing.T) {
  219. runCmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "8.8.8.8")
  220. out, _, exitCode, err := runCommandWithStdoutStderr(runCmd)
  221. if err != nil && exitCode != 1 {
  222. t.Fatal(out, err)
  223. }
  224. if exitCode != 1 {
  225. t.Errorf("--net=none should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  226. }
  227. runCmd = exec.Command(dockerBinary, "run", "-n=false", "busybox", "ping", "-c", "1", "8.8.8.8")
  228. out, _, exitCode, err = runCommandWithStdoutStderr(runCmd)
  229. if err != nil && exitCode != 1 {
  230. t.Fatal(out, err)
  231. }
  232. if exitCode != 1 {
  233. t.Errorf("-n=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  234. }
  235. deleteAllContainers()
  236. logDone("run - disable networking with --net=none")
  237. logDone("run - disable networking with -n=false")
  238. }
  239. // Regression test for #4741
  240. func TestRunWithVolumesAsFiles(t *testing.T) {
  241. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
  242. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  243. if err != nil && exitCode != 0 {
  244. t.Fatal("1", out, stderr, err)
  245. }
  246. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file")
  247. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  248. if err != nil && exitCode != 0 {
  249. t.Fatal("2", out, stderr, err)
  250. }
  251. deleteAllContainers()
  252. logDone("run - regression test for #4741 - volumes from as files")
  253. }
  254. // Regression test for #4979
  255. func TestRunWithVolumesFromExited(t *testing.T) {
  256. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file")
  257. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  258. if err != nil && exitCode != 0 {
  259. t.Fatal("1", out, stderr, err)
  260. }
  261. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/some/dir/file")
  262. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  263. if err != nil && exitCode != 0 {
  264. t.Fatal("2", out, stderr, err)
  265. }
  266. deleteAllContainers()
  267. logDone("run - regression test for #4979 - volumes-from on exited container")
  268. }
  269. // Regression test for #4830
  270. func TestRunWithRelativePath(t *testing.T) {
  271. runCmd := exec.Command(dockerBinary, "run", "-v", "tmp:/other-tmp", "busybox", "true")
  272. if _, _, _, err := runCommandWithStdoutStderr(runCmd); err == nil {
  273. t.Fatalf("relative path should result in an error")
  274. }
  275. deleteAllContainers()
  276. logDone("run - volume with relative path")
  277. }
  278. func TestRunVolumesMountedAsReadonly(t *testing.T) {
  279. cmd := exec.Command(dockerBinary, "run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile")
  280. if code, err := runCommand(cmd); err == nil || code == 0 {
  281. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  282. }
  283. deleteAllContainers()
  284. logDone("run - volumes as readonly mount")
  285. }
  286. func TestRunVolumesFromInReadonlyMode(t *testing.T) {
  287. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  288. if _, err := runCommand(cmd); err != nil {
  289. t.Fatal(err)
  290. }
  291. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:ro", "busybox", "touch", "/test/file")
  292. if code, err := runCommand(cmd); err == nil || code == 0 {
  293. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  294. }
  295. deleteAllContainers()
  296. logDone("run - volumes from as readonly mount")
  297. }
  298. // Regression test for #1201
  299. func TestRunVolumesFromInReadWriteMode(t *testing.T) {
  300. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  301. if _, err := runCommand(cmd); err != nil {
  302. t.Fatal(err)
  303. }
  304. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file")
  305. if out, _, err := runCommandWithOutput(cmd); err != nil {
  306. t.Fatalf("running --volumes-from parent:rw failed with output: %q\nerror: %v", out, err)
  307. }
  308. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:bar", "busybox", "touch", "/test/file")
  309. if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "Invalid mode for volumes-from: bar") {
  310. t.Fatalf("running --volumes-from foo:bar should have failed with invalid mount mode: %q", out)
  311. }
  312. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "busybox", "touch", "/test/file")
  313. if out, _, err := runCommandWithOutput(cmd); err != nil {
  314. t.Fatalf("running --volumes-from parent failed with output: %q\nerror: %v", out, err)
  315. }
  316. deleteAllContainers()
  317. logDone("run - volumes from as read write mount")
  318. }
  319. func TestVolumesFromGetsProperMode(t *testing.T) {
  320. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test:/test:ro", "busybox", "true")
  321. if _, err := runCommand(cmd); err != nil {
  322. t.Fatal(err)
  323. }
  324. // Expect this "rw" mode to be be ignored since the inheritted volume is "ro"
  325. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file")
  326. if _, err := runCommand(cmd); err == nil {
  327. t.Fatal("Expected volumes-from to inherit read-only volume even when passing in `rw`")
  328. }
  329. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/test:/test:ro", "busybox", "true")
  330. if _, err := runCommand(cmd); err != nil {
  331. t.Fatal(err)
  332. }
  333. // Expect this to be read-only since both are "ro"
  334. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent2:ro", "busybox", "touch", "/test/file")
  335. if _, err := runCommand(cmd); err == nil {
  336. t.Fatal("Expected volumes-from to inherit read-only volume even when passing in `ro`")
  337. }
  338. deleteAllContainers()
  339. logDone("run - volumes from ignores `rw` if inherrited volume is `ro`")
  340. }
  341. // Test for #1351
  342. func TestRunApplyVolumesFromBeforeVolumes(t *testing.T) {
  343. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo")
  344. if _, err := runCommand(cmd); err != nil {
  345. t.Fatal(err)
  346. }
  347. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "-v", "/test", "busybox", "cat", "/test/foo")
  348. if out, _, err := runCommandWithOutput(cmd); err != nil {
  349. t.Fatal(out, err)
  350. }
  351. deleteAllContainers()
  352. logDone("run - volumes from mounted first")
  353. }
  354. func TestRunMultipleVolumesFrom(t *testing.T) {
  355. cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo")
  356. if _, err := runCommand(cmd); err != nil {
  357. t.Fatal(err)
  358. }
  359. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/other", "busybox", "touch", "/other/bar")
  360. if _, err := runCommand(cmd); err != nil {
  361. t.Fatal(err)
  362. }
  363. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent1", "--volumes-from", "parent2",
  364. "busybox", "sh", "-c", "cat /test/foo && cat /other/bar")
  365. if _, err := runCommand(cmd); err != nil {
  366. t.Fatal(err)
  367. }
  368. deleteAllContainers()
  369. logDone("run - multiple volumes from")
  370. }
  371. // this tests verifies the ID format for the container
  372. func TestRunVerifyContainerID(t *testing.T) {
  373. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  374. out, exit, err := runCommandWithOutput(cmd)
  375. if err != nil {
  376. t.Fatal(err)
  377. }
  378. if exit != 0 {
  379. t.Fatalf("expected exit code 0 received %d", exit)
  380. }
  381. match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n"))
  382. if err != nil {
  383. t.Fatal(err)
  384. }
  385. if !match {
  386. t.Fatalf("Invalid container ID: %s", out)
  387. }
  388. deleteAllContainers()
  389. logDone("run - verify container ID")
  390. }
  391. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  392. func TestRunCreateVolume(t *testing.T) {
  393. cmd := exec.Command(dockerBinary, "run", "-v", "/var/lib/data", "busybox", "true")
  394. if _, err := runCommand(cmd); err != nil {
  395. t.Fatal(err)
  396. }
  397. deleteAllContainers()
  398. logDone("run - create docker managed volume")
  399. }
  400. // Test that creating a volume with a symlink in its path works correctly. Test for #5152.
  401. // Note that this bug happens only with symlinks with a target that starts with '/'.
  402. func TestRunCreateVolumeWithSymlink(t *testing.T) {
  403. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-createvolumewithsymlink", "-")
  404. buildCmd.Stdin = strings.NewReader(`FROM busybox
  405. RUN mkdir /foo && ln -s /foo /bar`)
  406. buildCmd.Dir = workingDirectory
  407. err := buildCmd.Run()
  408. if err != nil {
  409. t.Fatalf("could not build 'docker-test-createvolumewithsymlink': %v", err)
  410. }
  411. cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", "docker-test-createvolumewithsymlink", "sh", "-c", "mount | grep -q /foo/foo")
  412. exitCode, err := runCommand(cmd)
  413. if err != nil || exitCode != 0 {
  414. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  415. }
  416. var volPath string
  417. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{range .Volumes}}{{.}}{{end}}", "test-createvolumewithsymlink")
  418. volPath, exitCode, err = runCommandWithOutput(cmd)
  419. if err != nil || exitCode != 0 {
  420. t.Fatalf("[inspect] err: %v, exitcode: %d", err, exitCode)
  421. }
  422. cmd = exec.Command(dockerBinary, "rm", "-v", "test-createvolumewithsymlink")
  423. exitCode, err = runCommand(cmd)
  424. if err != nil || exitCode != 0 {
  425. t.Fatalf("[rm] err: %v, exitcode: %d", err, exitCode)
  426. }
  427. f, err := os.Open(volPath)
  428. defer f.Close()
  429. if !os.IsNotExist(err) {
  430. t.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
  431. }
  432. deleteImages("docker-test-createvolumewithsymlink")
  433. deleteAllContainers()
  434. logDone("run - create volume with symlink")
  435. }
  436. // Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`.
  437. func TestRunVolumesFromSymlinkPath(t *testing.T) {
  438. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-volumesfromsymlinkpath", "-")
  439. buildCmd.Stdin = strings.NewReader(`FROM busybox
  440. RUN mkdir /baz && ln -s /baz /foo
  441. VOLUME ["/foo/bar"]`)
  442. buildCmd.Dir = workingDirectory
  443. err := buildCmd.Run()
  444. if err != nil {
  445. t.Fatalf("could not build 'docker-test-volumesfromsymlinkpath': %v", err)
  446. }
  447. cmd := exec.Command(dockerBinary, "run", "--name", "test-volumesfromsymlinkpath", "docker-test-volumesfromsymlinkpath")
  448. exitCode, err := runCommand(cmd)
  449. if err != nil || exitCode != 0 {
  450. t.Fatalf("[run] (volume) err: %v, exitcode: %d", err, exitCode)
  451. }
  452. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-volumesfromsymlinkpath", "busybox", "sh", "-c", "ls /foo | grep -q bar")
  453. exitCode, err = runCommand(cmd)
  454. if err != nil || exitCode != 0 {
  455. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  456. }
  457. deleteImages("docker-test-volumesfromsymlinkpath")
  458. deleteAllContainers()
  459. logDone("run - volumes-from symlink path")
  460. }
  461. func TestRunExitCode(t *testing.T) {
  462. cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72")
  463. exit, err := runCommand(cmd)
  464. if err == nil {
  465. t.Fatal("should not have a non nil error")
  466. }
  467. if exit != 72 {
  468. t.Fatalf("expected exit code 72 received %d", exit)
  469. }
  470. deleteAllContainers()
  471. logDone("run - correct exit code")
  472. }
  473. func TestRunUserDefaultsToRoot(t *testing.T) {
  474. cmd := exec.Command(dockerBinary, "run", "busybox", "id")
  475. out, _, err := runCommandWithOutput(cmd)
  476. if err != nil {
  477. t.Fatal(err, out)
  478. }
  479. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  480. t.Fatalf("expected root user got %s", out)
  481. }
  482. deleteAllContainers()
  483. logDone("run - default user")
  484. }
  485. func TestRunUserByName(t *testing.T) {
  486. cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
  487. out, _, err := runCommandWithOutput(cmd)
  488. if err != nil {
  489. t.Fatal(err, out)
  490. }
  491. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  492. t.Fatalf("expected root user got %s", out)
  493. }
  494. deleteAllContainers()
  495. logDone("run - user by name")
  496. }
  497. func TestRunUserByID(t *testing.T) {
  498. cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
  499. out, _, err := runCommandWithOutput(cmd)
  500. if err != nil {
  501. t.Fatal(err, out)
  502. }
  503. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  504. t.Fatalf("expected daemon user got %s", out)
  505. }
  506. deleteAllContainers()
  507. logDone("run - user by id")
  508. }
  509. func TestRunUserByIDBig(t *testing.T) {
  510. cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id")
  511. out, _, err := runCommandWithOutput(cmd)
  512. if err == nil {
  513. t.Fatal("No error, but must be.", out)
  514. }
  515. if !strings.Contains(out, "Uids and gids must be in range") {
  516. t.Fatalf("expected error about uids range, got %s", out)
  517. }
  518. deleteAllContainers()
  519. logDone("run - user by id, id too big")
  520. }
  521. func TestRunUserByIDNegative(t *testing.T) {
  522. cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id")
  523. out, _, err := runCommandWithOutput(cmd)
  524. if err == nil {
  525. t.Fatal("No error, but must be.", out)
  526. }
  527. if !strings.Contains(out, "Uids and gids must be in range") {
  528. t.Fatalf("expected error about uids range, got %s", out)
  529. }
  530. deleteAllContainers()
  531. logDone("run - user by id, id negative")
  532. }
  533. func TestRunUserByIDZero(t *testing.T) {
  534. cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id")
  535. out, _, err := runCommandWithOutput(cmd)
  536. if err != nil {
  537. t.Fatal(err, out)
  538. }
  539. if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") {
  540. t.Fatalf("expected daemon user got %s", out)
  541. }
  542. deleteAllContainers()
  543. logDone("run - user by id, zero uid")
  544. }
  545. func TestRunUserNotFound(t *testing.T) {
  546. cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
  547. _, err := runCommand(cmd)
  548. if err == nil {
  549. t.Fatal("unknown user should cause container to fail")
  550. }
  551. deleteAllContainers()
  552. logDone("run - user not found")
  553. }
  554. func TestRunTwoConcurrentContainers(t *testing.T) {
  555. group := sync.WaitGroup{}
  556. group.Add(2)
  557. for i := 0; i < 2; i++ {
  558. go func() {
  559. defer group.Done()
  560. cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2")
  561. if _, err := runCommand(cmd); err != nil {
  562. t.Fatal(err)
  563. }
  564. }()
  565. }
  566. group.Wait()
  567. deleteAllContainers()
  568. logDone("run - two concurrent containers")
  569. }
  570. func TestRunEnvironment(t *testing.T) {
  571. cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "-e=HOME=", "busybox", "env")
  572. cmd.Env = append(os.Environ(),
  573. "TRUE=false",
  574. "TRICKY=tri\ncky\n",
  575. )
  576. out, _, err := runCommandWithOutput(cmd)
  577. if err != nil {
  578. t.Fatal(err, out)
  579. }
  580. actualEnv := strings.Split(out, "\n")
  581. if actualEnv[len(actualEnv)-1] == "" {
  582. actualEnv = actualEnv[:len(actualEnv)-1]
  583. }
  584. sort.Strings(actualEnv)
  585. goodEnv := []string{
  586. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  587. "HOSTNAME=testing",
  588. "FALSE=true",
  589. "TRUE=false",
  590. "TRICKY=tri",
  591. "cky",
  592. "",
  593. "HOME=/root",
  594. }
  595. sort.Strings(goodEnv)
  596. if len(goodEnv) != len(actualEnv) {
  597. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  598. }
  599. for i := range goodEnv {
  600. if actualEnv[i] != goodEnv[i] {
  601. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  602. }
  603. }
  604. deleteAllContainers()
  605. logDone("run - verify environment")
  606. }
  607. func TestRunContainerNetwork(t *testing.T) {
  608. cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
  609. if _, err := runCommand(cmd); err != nil {
  610. t.Fatal(err)
  611. }
  612. deleteAllContainers()
  613. logDone("run - test container network via ping")
  614. }
  615. // Issue #4681
  616. func TestRunLoopbackWhenNetworkDisabled(t *testing.T) {
  617. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
  618. if _, err := runCommand(cmd); err != nil {
  619. t.Fatal(err)
  620. }
  621. deleteAllContainers()
  622. logDone("run - test container loopback when networking disabled")
  623. }
  624. func TestRunNetHostNotAllowedWithLinks(t *testing.T) {
  625. _, _, err := cmd(t, "run", "--name", "linked", "busybox", "true")
  626. cmd := exec.Command(dockerBinary, "run", "--net=host", "--link", "linked:linked", "busybox", "true")
  627. _, _, err = runCommandWithOutput(cmd)
  628. if err == nil {
  629. t.Fatal("Expected error")
  630. }
  631. deleteAllContainers()
  632. logDone("run - don't allow --net=host to be used with links")
  633. }
  634. func TestRunLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
  635. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  636. out, _, err := runCommandWithOutput(cmd)
  637. if err != nil {
  638. t.Fatal(err, out)
  639. }
  640. var (
  641. count = 0
  642. parts = strings.Split(out, "\n")
  643. )
  644. for _, l := range parts {
  645. if l != "" {
  646. count++
  647. }
  648. }
  649. if count != 1 {
  650. t.Fatalf("Wrong interface count in container %d", count)
  651. }
  652. if !strings.HasPrefix(out, "1: lo") {
  653. t.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  654. }
  655. deleteAllContainers()
  656. logDone("run - test loopback only exists when networking disabled")
  657. }
  658. // #7851 hostname outside container shows FQDN, inside only shortname
  659. // For testing purposes it is not required to set host's hostname directly
  660. // and use "--net=host" (as the original issue submitter did), as the same
  661. // codepath is executed with "docker run -h <hostname>". Both were manually
  662. // tested, but this testcase takes the simpler path of using "run -h .."
  663. func TestRunFullHostnameSet(t *testing.T) {
  664. cmd := exec.Command(dockerBinary, "run", "-h", "foo.bar.baz", "busybox", "hostname")
  665. out, _, err := runCommandWithOutput(cmd)
  666. if err != nil {
  667. t.Fatal(err, out)
  668. }
  669. if actual := strings.Trim(out, "\r\n"); actual != "foo.bar.baz" {
  670. t.Fatalf("expected hostname 'foo.bar.baz', received %s", actual)
  671. }
  672. deleteAllContainers()
  673. logDone("run - test fully qualified hostname set with -h")
  674. }
  675. func TestRunPrivilegedCanMknod(t *testing.T) {
  676. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  677. out, _, err := runCommandWithOutput(cmd)
  678. if err != nil {
  679. t.Fatal(err)
  680. }
  681. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  682. t.Fatalf("expected output ok received %s", actual)
  683. }
  684. deleteAllContainers()
  685. logDone("run - test privileged can mknod")
  686. }
  687. func TestRunUnPrivilegedCanMknod(t *testing.T) {
  688. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  689. out, _, err := runCommandWithOutput(cmd)
  690. if err != nil {
  691. t.Fatal(err)
  692. }
  693. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  694. t.Fatalf("expected output ok received %s", actual)
  695. }
  696. deleteAllContainers()
  697. logDone("run - test un-privileged can mknod")
  698. }
  699. func TestRunCapDropInvalid(t *testing.T) {
  700. cmd := exec.Command(dockerBinary, "run", "--cap-drop=CHPASS", "busybox", "ls")
  701. out, _, err := runCommandWithOutput(cmd)
  702. if err == nil {
  703. t.Fatal(err, out)
  704. }
  705. logDone("run - test --cap-drop=CHPASS invalid")
  706. }
  707. func TestRunCapDropCannotMknod(t *testing.T) {
  708. cmd := exec.Command(dockerBinary, "run", "--cap-drop=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  709. out, _, err := runCommandWithOutput(cmd)
  710. if err == nil {
  711. t.Fatal(err, out)
  712. }
  713. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  714. t.Fatalf("expected output not ok received %s", actual)
  715. }
  716. deleteAllContainers()
  717. logDone("run - test --cap-drop=MKNOD cannot mknod")
  718. }
  719. func TestRunCapDropCannotMknodLowerCase(t *testing.T) {
  720. cmd := exec.Command(dockerBinary, "run", "--cap-drop=mknod", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  721. out, _, err := runCommandWithOutput(cmd)
  722. if err == nil {
  723. t.Fatal(err, out)
  724. }
  725. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  726. t.Fatalf("expected output not ok received %s", actual)
  727. }
  728. deleteAllContainers()
  729. logDone("run - test --cap-drop=mknod cannot mknod lowercase")
  730. }
  731. func TestRunCapDropALLCannotMknod(t *testing.T) {
  732. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  733. out, _, err := runCommandWithOutput(cmd)
  734. if err == nil {
  735. t.Fatal(err, out)
  736. }
  737. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  738. t.Fatalf("expected output not ok received %s", actual)
  739. }
  740. deleteAllContainers()
  741. logDone("run - test --cap-drop=ALL cannot mknod")
  742. }
  743. func TestRunCapDropALLAddMknodCannotMknod(t *testing.T) {
  744. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  745. out, _, err := runCommandWithOutput(cmd)
  746. if err != nil {
  747. t.Fatal(err, out)
  748. }
  749. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  750. t.Fatalf("expected output ok received %s", actual)
  751. }
  752. deleteAllContainers()
  753. logDone("run - test --cap-drop=ALL --cap-add=MKNOD can mknod")
  754. }
  755. func TestRunCapAddInvalid(t *testing.T) {
  756. cmd := exec.Command(dockerBinary, "run", "--cap-add=CHPASS", "busybox", "ls")
  757. out, _, err := runCommandWithOutput(cmd)
  758. if err == nil {
  759. t.Fatal(err, out)
  760. }
  761. logDone("run - test --cap-add=CHPASS invalid")
  762. }
  763. func TestRunCapAddCanDownInterface(t *testing.T) {
  764. cmd := exec.Command(dockerBinary, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  765. out, _, err := runCommandWithOutput(cmd)
  766. if err != nil {
  767. t.Fatal(err, out)
  768. }
  769. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  770. t.Fatalf("expected output ok received %s", actual)
  771. }
  772. deleteAllContainers()
  773. logDone("run - test --cap-add=NET_ADMIN can set eth0 down")
  774. }
  775. func TestRunCapAddALLCanDownInterface(t *testing.T) {
  776. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  777. out, _, err := runCommandWithOutput(cmd)
  778. if err != nil {
  779. t.Fatal(err, out)
  780. }
  781. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  782. t.Fatalf("expected output ok received %s", actual)
  783. }
  784. deleteAllContainers()
  785. logDone("run - test --cap-add=ALL can set eth0 down")
  786. }
  787. func TestRunCapAddALLDropNetAdminCanDownInterface(t *testing.T) {
  788. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "--cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  789. out, _, err := runCommandWithOutput(cmd)
  790. if err == nil {
  791. t.Fatal(err, out)
  792. }
  793. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  794. t.Fatalf("expected output not ok received %s", actual)
  795. }
  796. deleteAllContainers()
  797. logDone("run - test --cap-add=ALL --cap-drop=NET_ADMIN cannot set eth0 down")
  798. }
  799. func TestRunPrivilegedCanMount(t *testing.T) {
  800. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  801. out, _, err := runCommandWithOutput(cmd)
  802. if err != nil {
  803. t.Fatal(err)
  804. }
  805. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  806. t.Fatalf("expected output ok received %s", actual)
  807. }
  808. deleteAllContainers()
  809. logDone("run - test privileged can mount")
  810. }
  811. func TestRunUnPrivilegedCannotMount(t *testing.T) {
  812. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  813. out, _, err := runCommandWithOutput(cmd)
  814. if err == nil {
  815. t.Fatal(err, out)
  816. }
  817. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  818. t.Fatalf("expected output not ok received %s", actual)
  819. }
  820. deleteAllContainers()
  821. logDone("run - test un-privileged cannot mount")
  822. }
  823. func TestRunSysNotWritableInNonPrivilegedContainers(t *testing.T) {
  824. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
  825. if code, err := runCommand(cmd); err == nil || code == 0 {
  826. t.Fatal("sys should not be writable in a non privileged container")
  827. }
  828. deleteAllContainers()
  829. logDone("run - sys not writable in non privileged container")
  830. }
  831. func TestRunSysWritableInPrivilegedContainers(t *testing.T) {
  832. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
  833. if code, err := runCommand(cmd); err != nil || code != 0 {
  834. t.Fatalf("sys should be writable in privileged container")
  835. }
  836. deleteAllContainers()
  837. logDone("run - sys writable in privileged container")
  838. }
  839. func TestRunProcNotWritableInNonPrivilegedContainers(t *testing.T) {
  840. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
  841. if code, err := runCommand(cmd); err == nil || code == 0 {
  842. t.Fatal("proc should not be writable in a non privileged container")
  843. }
  844. deleteAllContainers()
  845. logDone("run - proc not writable in non privileged container")
  846. }
  847. func TestRunProcWritableInPrivilegedContainers(t *testing.T) {
  848. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
  849. if code, err := runCommand(cmd); err != nil || code != 0 {
  850. t.Fatalf("proc should be writable in privileged container")
  851. }
  852. deleteAllContainers()
  853. logDone("run - proc writable in privileged container")
  854. }
  855. func TestRunWithCpuset(t *testing.T) {
  856. cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
  857. if code, err := runCommand(cmd); err != nil || code != 0 {
  858. t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
  859. }
  860. deleteAllContainers()
  861. logDone("run - cpuset 0")
  862. }
  863. func TestRunDeviceNumbers(t *testing.T) {
  864. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
  865. out, _, err := runCommandWithOutput(cmd)
  866. if err != nil {
  867. t.Fatal(err, out)
  868. }
  869. deviceLineFields := strings.Fields(out)
  870. deviceLineFields[6] = ""
  871. deviceLineFields[7] = ""
  872. deviceLineFields[8] = ""
  873. expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"}
  874. if !(reflect.DeepEqual(deviceLineFields, expected)) {
  875. t.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
  876. }
  877. deleteAllContainers()
  878. logDone("run - test device numbers")
  879. }
  880. func TestRunThatCharacterDevicesActLikeCharacterDevices(t *testing.T) {
  881. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
  882. out, _, err := runCommandWithOutput(cmd)
  883. if err != nil {
  884. t.Fatal(err, out)
  885. }
  886. if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
  887. t.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
  888. }
  889. deleteAllContainers()
  890. logDone("run - test that character devices work.")
  891. }
  892. func TestRunUnprivilegedWithChroot(t *testing.T) {
  893. cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
  894. if _, err := runCommand(cmd); err != nil {
  895. t.Fatal(err)
  896. }
  897. deleteAllContainers()
  898. logDone("run - unprivileged with chroot")
  899. }
  900. func TestRunAddingOptionalDevices(t *testing.T) {
  901. cmd := exec.Command(dockerBinary, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo")
  902. out, _, err := runCommandWithOutput(cmd)
  903. if err != nil {
  904. t.Fatal(err, out)
  905. }
  906. if actual := strings.Trim(out, "\r\n"); actual != "/dev/nulo" {
  907. t.Fatalf("expected output /dev/nulo, received %s", actual)
  908. }
  909. deleteAllContainers()
  910. logDone("run - test --device argument")
  911. }
  912. func TestRunModeHostname(t *testing.T) {
  913. cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
  914. out, _, err := runCommandWithOutput(cmd)
  915. if err != nil {
  916. t.Fatal(err, out)
  917. }
  918. if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
  919. t.Fatalf("expected 'testhostname', but says: '%s'", actual)
  920. }
  921. cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname")
  922. out, _, err = runCommandWithOutput(cmd)
  923. if err != nil {
  924. t.Fatal(err, out)
  925. }
  926. hostname, err := os.Hostname()
  927. if err != nil {
  928. t.Fatal(err)
  929. }
  930. if actual := strings.Trim(out, "\r\n"); actual != hostname {
  931. t.Fatalf("expected '%s', but says: '%s'", hostname, actual)
  932. }
  933. deleteAllContainers()
  934. logDone("run - hostname and several network modes")
  935. }
  936. func TestRunRootWorkdir(t *testing.T) {
  937. s, _, err := cmd(t, "run", "--workdir", "/", "busybox", "pwd")
  938. if err != nil {
  939. t.Fatal(s, err)
  940. }
  941. if s != "/\n" {
  942. t.Fatalf("pwd returned '%s' (expected /\\n)", s)
  943. }
  944. deleteAllContainers()
  945. logDone("run - workdir /")
  946. }
  947. func TestRunAllowBindMountingRoot(t *testing.T) {
  948. s, _, err := cmd(t, "run", "-v", "/:/host", "busybox", "ls", "/host")
  949. if err != nil {
  950. t.Fatal(s, err)
  951. }
  952. deleteAllContainers()
  953. logDone("run - bind mount / as volume")
  954. }
  955. func TestRunDisallowBindMountingRootToRoot(t *testing.T) {
  956. cmd := exec.Command(dockerBinary, "run", "-v", "/:/", "busybox", "ls", "/host")
  957. out, _, err := runCommandWithOutput(cmd)
  958. if err == nil {
  959. t.Fatal(out, err)
  960. }
  961. deleteAllContainers()
  962. logDone("run - bind mount /:/ as volume should fail")
  963. }
  964. // Test recursive bind mount works by default
  965. func TestRunWithVolumesIsRecursive(t *testing.T) {
  966. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  967. if err != nil {
  968. t.Fatal(err)
  969. }
  970. defer os.RemoveAll(tmpDir)
  971. // Create a temporary tmpfs mount.
  972. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  973. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  974. t.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  975. }
  976. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  977. t.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  978. }
  979. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  980. if err != nil {
  981. t.Fatal(err)
  982. }
  983. defer f.Close()
  984. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  985. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  986. if err != nil && exitCode != 0 {
  987. t.Fatal(out, stderr, err)
  988. }
  989. if !strings.Contains(out, filepath.Base(f.Name())) {
  990. t.Fatal("Recursive bind mount test failed. Expected file not found")
  991. }
  992. deleteAllContainers()
  993. logDone("run - volumes are bind mounted recursively")
  994. }
  995. func TestRunDnsDefaultOptions(t *testing.T) {
  996. cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
  997. actual, _, err := runCommandWithOutput(cmd)
  998. if err != nil {
  999. t.Fatal(err, actual)
  1000. }
  1001. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  1002. if os.IsNotExist(err) {
  1003. t.Fatalf("/etc/resolv.conf does not exist")
  1004. }
  1005. if actual != string(resolvConf) {
  1006. t.Fatalf("expected resolv.conf is not the same of actual")
  1007. }
  1008. deleteAllContainers()
  1009. logDone("run - dns default options")
  1010. }
  1011. func TestRunDnsOptions(t *testing.T) {
  1012. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  1013. out, _, err := runCommandWithOutput(cmd)
  1014. if err != nil {
  1015. t.Fatal(err, out)
  1016. }
  1017. actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1)
  1018. if actual != "nameserver 127.0.0.1 search mydomain" {
  1019. t.Fatalf("expected 'nameserver 127.0.0.1 search mydomain', but says: '%s'", actual)
  1020. }
  1021. cmd = exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=.", "busybox", "cat", "/etc/resolv.conf")
  1022. out, _, err = runCommandWithOutput(cmd)
  1023. if err != nil {
  1024. t.Fatal(err, out)
  1025. }
  1026. actual = strings.Replace(strings.Trim(strings.Trim(out, "\r\n"), " "), "\n", " ", -1)
  1027. if actual != "nameserver 127.0.0.1" {
  1028. t.Fatalf("expected 'nameserver 127.0.0.1', but says: '%s'", actual)
  1029. }
  1030. logDone("run - dns options")
  1031. }
  1032. func TestRunDnsOptionsBasedOnHostResolvConf(t *testing.T) {
  1033. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  1034. if os.IsNotExist(err) {
  1035. t.Fatalf("/etc/resolv.conf does not exist")
  1036. }
  1037. hostNamservers := resolvconf.GetNameservers(resolvConf)
  1038. hostSearch := resolvconf.GetSearchDomains(resolvConf)
  1039. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "busybox", "cat", "/etc/resolv.conf")
  1040. out, _, err := runCommandWithOutput(cmd)
  1041. if err != nil {
  1042. t.Fatal(err, out)
  1043. }
  1044. if actualNameservers := resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "127.0.0.1" {
  1045. t.Fatalf("expected '127.0.0.1', but says: '%s'", string(actualNameservers[0]))
  1046. }
  1047. actualSearch := resolvconf.GetSearchDomains([]byte(out))
  1048. if len(actualSearch) != len(hostSearch) {
  1049. t.Fatalf("expected '%s' search domain(s), but it has: '%s'", len(hostSearch), len(actualSearch))
  1050. }
  1051. for i := range actualSearch {
  1052. if actualSearch[i] != hostSearch[i] {
  1053. t.Fatalf("expected '%s' domain, but says: '%s'", actualSearch[i], hostSearch[i])
  1054. }
  1055. }
  1056. cmd = exec.Command(dockerBinary, "run", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  1057. out, _, err = runCommandWithOutput(cmd)
  1058. if err != nil {
  1059. t.Fatal(err, out)
  1060. }
  1061. actualNameservers := resolvconf.GetNameservers([]byte(out))
  1062. if len(actualNameservers) != len(hostNamservers) {
  1063. t.Fatalf("expected '%s' nameserver(s), but it has: '%s'", len(hostNamservers), len(actualNameservers))
  1064. }
  1065. for i := range actualNameservers {
  1066. if actualNameservers[i] != hostNamservers[i] {
  1067. t.Fatalf("expected '%s' nameserver, but says: '%s'", actualNameservers[i], hostNamservers[i])
  1068. }
  1069. }
  1070. if actualSearch = resolvconf.GetSearchDomains([]byte(out)); string(actualSearch[0]) != "mydomain" {
  1071. t.Fatalf("expected 'mydomain', but says: '%s'", string(actualSearch[0]))
  1072. }
  1073. deleteAllContainers()
  1074. logDone("run - dns options based on host resolv.conf")
  1075. }
  1076. func TestRunAddHost(t *testing.T) {
  1077. defer deleteAllContainers()
  1078. cmd := exec.Command(dockerBinary, "run", "--add-host=extra:86.75.30.9", "busybox", "grep", "extra", "/etc/hosts")
  1079. out, _, err := runCommandWithOutput(cmd)
  1080. if err != nil {
  1081. t.Fatal(err, out)
  1082. }
  1083. actual := strings.Trim(out, "\r\n")
  1084. if actual != "86.75.30.9\textra" {
  1085. t.Fatalf("expected '86.75.30.9\textra', but says: '%s'", actual)
  1086. }
  1087. logDone("run - add-host option")
  1088. }
  1089. // Regression test for #6983
  1090. func TestRunAttachStdErrOnlyTTYMode(t *testing.T) {
  1091. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stderr", "busybox", "true")
  1092. exitCode, err := runCommand(cmd)
  1093. if err != nil {
  1094. t.Fatal(err)
  1095. } else if exitCode != 0 {
  1096. t.Fatalf("Container should have exited with error code 0")
  1097. }
  1098. deleteAllContainers()
  1099. logDone("run - Attach stderr only with -t")
  1100. }
  1101. // Regression test for #6983
  1102. func TestRunAttachStdOutOnlyTTYMode(t *testing.T) {
  1103. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "busybox", "true")
  1104. exitCode, err := runCommand(cmd)
  1105. if err != nil {
  1106. t.Fatal(err)
  1107. } else if exitCode != 0 {
  1108. t.Fatalf("Container should have exited with error code 0")
  1109. }
  1110. deleteAllContainers()
  1111. logDone("run - Attach stdout only with -t")
  1112. }
  1113. // Regression test for #6983
  1114. func TestRunAttachStdOutAndErrTTYMode(t *testing.T) {
  1115. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "-a", "stderr", "busybox", "true")
  1116. exitCode, err := runCommand(cmd)
  1117. if err != nil {
  1118. t.Fatal(err)
  1119. } else if exitCode != 0 {
  1120. t.Fatalf("Container should have exited with error code 0")
  1121. }
  1122. deleteAllContainers()
  1123. logDone("run - Attach stderr and stdout with -t")
  1124. }
  1125. func TestRunState(t *testing.T) {
  1126. defer deleteAllContainers()
  1127. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  1128. out, _, err := runCommandWithOutput(cmd)
  1129. if err != nil {
  1130. t.Fatal(err, out)
  1131. }
  1132. id := strings.TrimSpace(out)
  1133. state, err := inspectField(id, "State.Running")
  1134. if err != nil {
  1135. t.Fatal(err)
  1136. }
  1137. if state != "true" {
  1138. t.Fatal("Container state is 'not running'")
  1139. }
  1140. pid1, err := inspectField(id, "State.Pid")
  1141. if err != nil {
  1142. t.Fatal(err)
  1143. }
  1144. if pid1 == "0" {
  1145. t.Fatal("Container state Pid 0")
  1146. }
  1147. cmd = exec.Command(dockerBinary, "stop", id)
  1148. out, _, err = runCommandWithOutput(cmd)
  1149. if err != nil {
  1150. t.Fatal(err, out)
  1151. }
  1152. state, err = inspectField(id, "State.Running")
  1153. if err != nil {
  1154. t.Fatal(err)
  1155. }
  1156. if state != "false" {
  1157. t.Fatal("Container state is 'running'")
  1158. }
  1159. pid2, err := inspectField(id, "State.Pid")
  1160. if err != nil {
  1161. t.Fatal(err)
  1162. }
  1163. if pid2 == pid1 {
  1164. t.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1165. }
  1166. cmd = exec.Command(dockerBinary, "start", id)
  1167. out, _, err = runCommandWithOutput(cmd)
  1168. if err != nil {
  1169. t.Fatal(err, out)
  1170. }
  1171. state, err = inspectField(id, "State.Running")
  1172. if err != nil {
  1173. t.Fatal(err)
  1174. }
  1175. if state != "true" {
  1176. t.Fatal("Container state is 'not running'")
  1177. }
  1178. pid3, err := inspectField(id, "State.Pid")
  1179. if err != nil {
  1180. t.Fatal(err)
  1181. }
  1182. if pid3 == pid1 {
  1183. t.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1184. }
  1185. logDone("run - test container state.")
  1186. }
  1187. // Test for #1737
  1188. func TestRunCopyVolumeUidGid(t *testing.T) {
  1189. name := "testrunvolumesuidgid"
  1190. defer deleteImages(name)
  1191. defer deleteAllContainers()
  1192. _, err := buildImage(name,
  1193. `FROM busybox
  1194. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  1195. RUN echo 'dockerio:x:1001:' >> /etc/group
  1196. RUN mkdir -p /hello && touch /hello/test && chown dockerio.dockerio /hello`,
  1197. true)
  1198. if err != nil {
  1199. t.Fatal(err)
  1200. }
  1201. // Test that the uid and gid is copied from the image to the volume
  1202. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "ls -l / | grep hello | awk '{print $3\":\"$4}'")
  1203. out, _, err := runCommandWithOutput(cmd)
  1204. if err != nil {
  1205. t.Fatal(err, out)
  1206. }
  1207. out = strings.TrimSpace(out)
  1208. if out != "dockerio:dockerio" {
  1209. t.Fatalf("Wrong /hello ownership: %s, expected dockerio:dockerio", out)
  1210. }
  1211. logDone("run - copy uid/gid for volume")
  1212. }
  1213. // Test for #1582
  1214. func TestRunCopyVolumeContent(t *testing.T) {
  1215. name := "testruncopyvolumecontent"
  1216. defer deleteImages(name)
  1217. defer deleteAllContainers()
  1218. _, err := buildImage(name,
  1219. `FROM busybox
  1220. RUN mkdir -p /hello/local && echo hello > /hello/local/world`,
  1221. true)
  1222. if err != nil {
  1223. t.Fatal(err)
  1224. }
  1225. // Test that the content is copied from the image to the volume
  1226. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "find", "/hello")
  1227. out, _, err := runCommandWithOutput(cmd)
  1228. if err != nil {
  1229. t.Fatal(err, out)
  1230. }
  1231. if !(strings.Contains(out, "/hello/local/world") && strings.Contains(out, "/hello/local")) {
  1232. t.Fatal("Container failed to transfer content to volume")
  1233. }
  1234. logDone("run - copy volume content")
  1235. }
  1236. func TestRunCleanupCmdOnEntrypoint(t *testing.T) {
  1237. name := "testrunmdcleanuponentrypoint"
  1238. defer deleteImages(name)
  1239. defer deleteAllContainers()
  1240. if _, err := buildImage(name,
  1241. `FROM busybox
  1242. ENTRYPOINT ["echo"]
  1243. CMD ["testingpoint"]`,
  1244. true); err != nil {
  1245. t.Fatal(err)
  1246. }
  1247. runCmd := exec.Command(dockerBinary, "run", "--entrypoint", "whoami", name)
  1248. out, exit, err := runCommandWithOutput(runCmd)
  1249. if err != nil {
  1250. t.Fatalf("Error: %v, out: %q", err, out)
  1251. }
  1252. if exit != 0 {
  1253. t.Fatalf("expected exit code 0 received %d, out: %q", exit, out)
  1254. }
  1255. out = strings.TrimSpace(out)
  1256. if out != "root" {
  1257. t.Fatalf("Expected output root, got %q", out)
  1258. }
  1259. logDone("run - cleanup cmd on --entrypoint")
  1260. }
  1261. // TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected
  1262. func TestRunWorkdirExistsAndIsFile(t *testing.T) {
  1263. defer deleteAllContainers()
  1264. runCmd := exec.Command(dockerBinary, "run", "-w", "/bin/cat", "busybox")
  1265. out, exit, err := runCommandWithOutput(runCmd)
  1266. if !(err != nil && exit == 1 && strings.Contains(out, "Cannot mkdir: /bin/cat is not a directory")) {
  1267. t.Fatalf("Docker must complains about making dir, but we got out: %s, exit: %d, err: %s", out, exit, err)
  1268. }
  1269. logDone("run - error on existing file for workdir")
  1270. }
  1271. func TestRunExitOnStdinClose(t *testing.T) {
  1272. name := "testrunexitonstdinclose"
  1273. defer deleteAllContainers()
  1274. runCmd := exec.Command(dockerBinary, "run", "--name", name, "-i", "busybox", "/bin/cat")
  1275. stdin, err := runCmd.StdinPipe()
  1276. if err != nil {
  1277. t.Fatal(err)
  1278. }
  1279. stdout, err := runCmd.StdoutPipe()
  1280. if err != nil {
  1281. t.Fatal(err)
  1282. }
  1283. if err := runCmd.Start(); err != nil {
  1284. t.Fatal(err)
  1285. }
  1286. if _, err := stdin.Write([]byte("hello\n")); err != nil {
  1287. t.Fatal(err)
  1288. }
  1289. r := bufio.NewReader(stdout)
  1290. line, err := r.ReadString('\n')
  1291. if err != nil {
  1292. t.Fatal(err)
  1293. }
  1294. line = strings.TrimSpace(line)
  1295. if line != "hello" {
  1296. t.Fatalf("Output should be 'hello', got '%q'", line)
  1297. }
  1298. if err := stdin.Close(); err != nil {
  1299. t.Fatal(err)
  1300. }
  1301. finish := make(chan struct{})
  1302. go func() {
  1303. if err := runCmd.Wait(); err != nil {
  1304. t.Fatal(err)
  1305. }
  1306. close(finish)
  1307. }()
  1308. select {
  1309. case <-finish:
  1310. case <-time.After(1 * time.Second):
  1311. t.Fatal("docker run failed to exit on stdin close")
  1312. }
  1313. state, err := inspectField(name, "State.Running")
  1314. if err != nil {
  1315. t.Fatal(err)
  1316. }
  1317. if state != "false" {
  1318. t.Fatal("Container must be stopped after stdin closing")
  1319. }
  1320. logDone("run - exit on stdin closing")
  1321. }
  1322. // Test for #2267
  1323. func TestRunWriteHostsFileAndNotCommit(t *testing.T) {
  1324. name := "writehosts"
  1325. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hosts && cat /etc/hosts")
  1326. out, _, err := runCommandWithOutput(cmd)
  1327. if err != nil {
  1328. t.Fatal(err, out)
  1329. }
  1330. if !strings.Contains(out, "test2267") {
  1331. t.Fatal("/etc/hosts should contain 'test2267'")
  1332. }
  1333. cmd = exec.Command(dockerBinary, "diff", name)
  1334. if err != nil {
  1335. t.Fatal(err, out)
  1336. }
  1337. out, _, err = runCommandWithOutput(cmd)
  1338. if err != nil {
  1339. t.Fatal(err, out)
  1340. }
  1341. if len(strings.Trim(out, "\r\n")) != 0 {
  1342. t.Fatal("diff should be empty")
  1343. }
  1344. logDone("run - write to /etc/hosts and not commited")
  1345. }
  1346. // Test for #2267
  1347. func TestRunWriteHostnameFileAndNotCommit(t *testing.T) {
  1348. name := "writehostname"
  1349. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hostname && cat /etc/hostname")
  1350. out, _, err := runCommandWithOutput(cmd)
  1351. if err != nil {
  1352. t.Fatal(err, out)
  1353. }
  1354. if !strings.Contains(out, "test2267") {
  1355. t.Fatal("/etc/hostname should contain 'test2267'")
  1356. }
  1357. cmd = exec.Command(dockerBinary, "diff", name)
  1358. if err != nil {
  1359. t.Fatal(err, out)
  1360. }
  1361. out, _, err = runCommandWithOutput(cmd)
  1362. if err != nil {
  1363. t.Fatal(err, out)
  1364. }
  1365. if len(strings.Trim(out, "\r\n")) != 0 {
  1366. t.Fatal("diff should be empty")
  1367. }
  1368. logDone("run - write to /etc/hostname and not commited")
  1369. }
  1370. // Test for #2267
  1371. func TestRunWriteResolvFileAndNotCommit(t *testing.T) {
  1372. name := "writeresolv"
  1373. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/resolv.conf && cat /etc/resolv.conf")
  1374. out, _, err := runCommandWithOutput(cmd)
  1375. if err != nil {
  1376. t.Fatal(err, out)
  1377. }
  1378. if !strings.Contains(out, "test2267") {
  1379. t.Fatal("/etc/resolv.conf should contain 'test2267'")
  1380. }
  1381. cmd = exec.Command(dockerBinary, "diff", name)
  1382. if err != nil {
  1383. t.Fatal(err, out)
  1384. }
  1385. out, _, err = runCommandWithOutput(cmd)
  1386. if err != nil {
  1387. t.Fatal(err, out)
  1388. }
  1389. if len(strings.Trim(out, "\r\n")) != 0 {
  1390. t.Fatal("diff should be empty")
  1391. }
  1392. logDone("run - write to /etc/resolv.conf and not commited")
  1393. }
  1394. func TestRunWithBadDevice(t *testing.T) {
  1395. name := "baddevice"
  1396. cmd := exec.Command(dockerBinary, "run", "--name", name, "--device", "/etc", "busybox", "true")
  1397. out, _, err := runCommandWithOutput(cmd)
  1398. if err == nil {
  1399. t.Fatal("Run should fail with bad device")
  1400. }
  1401. expected := `"/etc": not a device node`
  1402. if !strings.Contains(out, expected) {
  1403. t.Fatalf("Output should contain %q, actual out: %q", expected, out)
  1404. }
  1405. logDone("run - error with bad device")
  1406. }
  1407. func TestRunEntrypoint(t *testing.T) {
  1408. name := "entrypoint"
  1409. cmd := exec.Command(dockerBinary, "run", "--name", name, "--entrypoint", "/bin/echo", "busybox", "-n", "foobar")
  1410. out, _, err := runCommandWithOutput(cmd)
  1411. if err != nil {
  1412. t.Fatal(err, out)
  1413. }
  1414. expected := "foobar"
  1415. if out != expected {
  1416. t.Fatalf("Output should be %q, actual out: %q", expected, out)
  1417. }
  1418. logDone("run - entrypoint")
  1419. }
  1420. func TestRunBindMounts(t *testing.T) {
  1421. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1422. if err != nil {
  1423. t.Fatal(err)
  1424. }
  1425. defer os.RemoveAll(tmpDir)
  1426. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  1427. // Test reading from a read-only bind mount
  1428. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox", "ls", "/tmp")
  1429. out, _, err := runCommandWithOutput(cmd)
  1430. if err != nil {
  1431. t.Fatal(err, out)
  1432. }
  1433. if !strings.Contains(out, "touch-me") {
  1434. t.Fatal("Container failed to read from bind mount")
  1435. }
  1436. // test writing to bind mount
  1437. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "busybox", "touch", "/tmp/holla")
  1438. out, _, err = runCommandWithOutput(cmd)
  1439. if err != nil {
  1440. t.Fatal(err, out)
  1441. }
  1442. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1443. // test mounting to an illegal destination directory
  1444. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:.", tmpDir), "busybox", "ls", ".")
  1445. _, err = runCommand(cmd)
  1446. if err == nil {
  1447. t.Fatal("Container bind mounted illegal directory")
  1448. }
  1449. // test mount a file
  1450. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "busybox", "sh", "-c", "echo -n 'yotta' > /tmp/holla")
  1451. _, err = runCommand(cmd)
  1452. if err != nil {
  1453. t.Fatal(err, out)
  1454. }
  1455. content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1456. expected := "yotta"
  1457. if content != expected {
  1458. t.Fatalf("Output should be %q, actual out: %q", expected, content)
  1459. }
  1460. logDone("run - bind mounts")
  1461. }
  1462. func TestRunMutableNetworkFiles(t *testing.T) {
  1463. defer deleteAllContainers()
  1464. for _, fn := range []string{"resolv.conf", "hosts"} {
  1465. deleteAllContainers()
  1466. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s; while true; do sleep 1; done", fn)))
  1467. if err != nil {
  1468. t.Fatal(err, out)
  1469. }
  1470. time.Sleep(1 * time.Second)
  1471. contID := strings.TrimSpace(out)
  1472. f, err := os.Open(filepath.Join("/var/lib/docker/containers", contID, fn))
  1473. if err != nil {
  1474. t.Fatal(err)
  1475. }
  1476. content, err := ioutil.ReadAll(f)
  1477. f.Close()
  1478. if strings.TrimSpace(string(content)) != "success" {
  1479. t.Fatal("Content was not what was modified in the container", string(content))
  1480. }
  1481. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c2", "busybox", "sh", "-c", fmt.Sprintf("while true; do cat /etc/%s; sleep 1; done", fn)))
  1482. if err != nil {
  1483. t.Fatal(err)
  1484. }
  1485. contID = strings.TrimSpace(out)
  1486. resolvConfPath := filepath.Join("/var/lib/docker/containers", contID, fn)
  1487. f, err = os.OpenFile(resolvConfPath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
  1488. if err != nil {
  1489. t.Fatal(err)
  1490. }
  1491. if _, err := f.Seek(0, 0); err != nil {
  1492. f.Close()
  1493. t.Fatal(err)
  1494. }
  1495. if err := f.Truncate(0); err != nil {
  1496. f.Close()
  1497. t.Fatal(err)
  1498. }
  1499. if _, err := f.Write([]byte("success2\n")); err != nil {
  1500. f.Close()
  1501. t.Fatal(err)
  1502. }
  1503. f.Close()
  1504. time.Sleep(2 * time.Second) // don't race sleep
  1505. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "logs", "c2"))
  1506. if err != nil {
  1507. t.Fatal(err)
  1508. }
  1509. lines := strings.Split(out, "\n")
  1510. if strings.TrimSpace(lines[len(lines)-2]) != "success2" {
  1511. t.Fatalf("Did not find the correct output in /etc/%s: %s %#v", fn, out, lines)
  1512. }
  1513. }
  1514. }
  1515. func TestRunStableIPAndPort(t *testing.T) {
  1516. const nContainers = 2
  1517. var ids, ips, macs, ports [nContainers]string
  1518. // Setup: Create a couple of containers and collect their IPs and public ports.
  1519. for i := 0; i < nContainers; i++ {
  1520. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "1234", "busybox", "top")
  1521. out, _, err := runCommandWithOutput(runCmd)
  1522. if err != nil {
  1523. t.Fatal(err)
  1524. }
  1525. ids[i] = strings.TrimSpace(out)
  1526. ips[i], err = inspectField(ids[i], "NetworkSettings.IPAddress")
  1527. errorOut(err, t, out)
  1528. if ips[i] == "" {
  1529. t.Fatal("IP allocation failed")
  1530. }
  1531. macs[i], err = inspectField(ids[i], "NetworkSettings.MacAddress")
  1532. errorOut(err, t, out)
  1533. portCmd := exec.Command(dockerBinary, "port", ids[i], "1234")
  1534. ports[i], _, err = runCommandWithOutput(portCmd)
  1535. errorOut(err, t, out)
  1536. if ports[i] == "" {
  1537. t.Fatal("Port allocation failed")
  1538. }
  1539. }
  1540. // Stop them all.
  1541. for _, id := range ids {
  1542. cmd := exec.Command(dockerBinary, "stop", id)
  1543. out, _, err := runCommandWithOutput(cmd)
  1544. if err != nil {
  1545. t.Fatal(err, out)
  1546. }
  1547. }
  1548. // Create a new container and ensure it's not getting the IP or port of some stopped container.
  1549. {
  1550. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "1234", "busybox", "top")
  1551. out, _, err := runCommandWithOutput(runCmd)
  1552. errorOut(err, t, out)
  1553. id := strings.TrimSpace(out)
  1554. ip, err := inspectField(id, "NetworkSettings.IPAddress")
  1555. errorOut(err, t, out)
  1556. portCmd := exec.Command(dockerBinary, "port", id, "1234")
  1557. port, _, err := runCommandWithOutput(portCmd)
  1558. errorOut(err, t, out)
  1559. for i := range ids {
  1560. if ip == ips[i] {
  1561. t.Fatalf("Conflicting IP: %s", ip)
  1562. }
  1563. if port == ports[i] {
  1564. t.Fatalf("Conflicting port: %s", port)
  1565. }
  1566. }
  1567. }
  1568. // Start the containers back, and ensure they are getting the same IPs, MACs and ports.
  1569. for i, id := range ids {
  1570. runCmd := exec.Command(dockerBinary, "start", id)
  1571. out, _, err := runCommandWithOutput(runCmd)
  1572. errorOut(err, t, out)
  1573. ip, err := inspectField(id, "NetworkSettings.IPAddress")
  1574. errorOut(err, t, out)
  1575. mac, err := inspectField(id, "NetworkSettings.MacAddress")
  1576. errorOut(err, t, out)
  1577. portCmd := exec.Command(dockerBinary, "port", ids[i], "1234")
  1578. port, _, err := runCommandWithOutput(portCmd)
  1579. errorOut(err, t, out)
  1580. if ips[i] != ip {
  1581. t.Fatalf("Container started with a different IP: %s != %s", ip, ips[i])
  1582. }
  1583. if macs[i] != mac {
  1584. t.Fatalf("Container started with a different MAC: %s != %s", mac, macs[i])
  1585. }
  1586. if ports[i] != port {
  1587. t.Fatalf("Container started with a different port: %s != %s", port, ports[i])
  1588. }
  1589. }
  1590. deleteAllContainers()
  1591. logDone("run - ips and ports must not change")
  1592. }
  1593. // Ensure that CIDFile gets deleted if it's empty
  1594. // Perform this test by making `docker run` fail
  1595. func TestRunCidFileCleanupIfEmpty(t *testing.T) {
  1596. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  1597. if err != nil {
  1598. t.Fatal(err)
  1599. }
  1600. defer os.RemoveAll(tmpDir)
  1601. tmpCidFile := path.Join(tmpDir, "cid")
  1602. cmd := exec.Command(dockerBinary, "run", "--cidfile", tmpCidFile, "scratch")
  1603. out, _, err := runCommandWithOutput(cmd)
  1604. t.Log(out)
  1605. if err == nil {
  1606. t.Fatal("Run without command must fail")
  1607. }
  1608. if _, err := os.Stat(tmpCidFile); err == nil {
  1609. t.Fatalf("empty CIDFile '%s' should've been deleted", tmpCidFile)
  1610. }
  1611. deleteAllContainers()
  1612. logDone("run - cleanup empty cidfile on fail")
  1613. }
  1614. // #2098 - Docker cidFiles only contain short version of the containerId
  1615. //sudo docker run --cidfile /tmp/docker_test.cid ubuntu echo "test"
  1616. // TestRunCidFile tests that run --cidfile returns the longid
  1617. func TestRunCidFileCheckIDLength(t *testing.T) {
  1618. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  1619. if err != nil {
  1620. t.Fatal(err)
  1621. }
  1622. tmpCidFile := path.Join(tmpDir, "cid")
  1623. defer os.RemoveAll(tmpDir)
  1624. cmd := exec.Command(dockerBinary, "run", "-d", "--cidfile", tmpCidFile, "busybox", "true")
  1625. out, _, err := runCommandWithOutput(cmd)
  1626. if err != nil {
  1627. t.Fatal(err)
  1628. }
  1629. id := strings.TrimSpace(out)
  1630. buffer, err := ioutil.ReadFile(tmpCidFile)
  1631. if err != nil {
  1632. t.Fatal(err)
  1633. }
  1634. cid := string(buffer)
  1635. if len(cid) != 64 {
  1636. t.Fatalf("--cidfile should be a long id, not '%s'", id)
  1637. }
  1638. if cid != id {
  1639. t.Fatalf("cid must be equal to %s, got %s", id, cid)
  1640. }
  1641. deleteAllContainers()
  1642. logDone("run - cidfile contains long id")
  1643. }
  1644. func TestRunNetworkNotInitializedNoneMode(t *testing.T) {
  1645. cmd := exec.Command(dockerBinary, "run", "-d", "--net=none", "busybox", "top")
  1646. out, _, err := runCommandWithOutput(cmd)
  1647. if err != nil {
  1648. t.Fatal(err)
  1649. }
  1650. id := strings.TrimSpace(out)
  1651. res, err := inspectField(id, "NetworkSettings.IPAddress")
  1652. if err != nil {
  1653. t.Fatal(err)
  1654. }
  1655. if res != "" {
  1656. t.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
  1657. }
  1658. deleteAllContainers()
  1659. logDone("run - network must not be initialized in 'none' mode")
  1660. }
  1661. func TestRunDeallocatePortOnMissingIptablesRule(t *testing.T) {
  1662. cmd := exec.Command(dockerBinary, "run", "-d", "-p", "23:23", "busybox", "top")
  1663. out, _, err := runCommandWithOutput(cmd)
  1664. if err != nil {
  1665. t.Fatal(err)
  1666. }
  1667. id := strings.TrimSpace(out)
  1668. ip, err := inspectField(id, "NetworkSettings.IPAddress")
  1669. if err != nil {
  1670. t.Fatal(err)
  1671. }
  1672. iptCmd := exec.Command("iptables", "-D", "FORWARD", "-d", fmt.Sprintf("%s/32", ip),
  1673. "!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT")
  1674. out, _, err = runCommandWithOutput(iptCmd)
  1675. if err != nil {
  1676. t.Fatal(err, out)
  1677. }
  1678. if err := deleteContainer(id); err != nil {
  1679. t.Fatal(err)
  1680. }
  1681. cmd = exec.Command(dockerBinary, "run", "-d", "-p", "23:23", "busybox", "top")
  1682. out, _, err = runCommandWithOutput(cmd)
  1683. if err != nil {
  1684. t.Fatal(err, out)
  1685. }
  1686. deleteAllContainers()
  1687. logDone("run - port should be deallocated even on iptables error")
  1688. }
  1689. func TestRunPortInUse(t *testing.T) {
  1690. port := "1234"
  1691. l, err := net.Listen("tcp", ":"+port)
  1692. if err != nil {
  1693. t.Fatal(err)
  1694. }
  1695. defer l.Close()
  1696. cmd := exec.Command(dockerBinary, "run", "-p", port+":80", "busybox", "true")
  1697. out, _, err := runCommandWithOutput(cmd)
  1698. if err == nil {
  1699. t.Fatalf("Binding on used port must fail")
  1700. }
  1701. if !strings.Contains(out, "address already in use") {
  1702. t.Fatalf("Out must be about \"address already in use\", got %s", out)
  1703. }
  1704. deleteAllContainers()
  1705. logDone("run - fail if port already in use")
  1706. }
  1707. // Regression test for #7792
  1708. func TestRunMountOrdering(t *testing.T) {
  1709. tmpDir, err := ioutil.TempDir("", "docker_nested_mount_test")
  1710. if err != nil {
  1711. t.Fatal(err)
  1712. }
  1713. defer os.RemoveAll(tmpDir)
  1714. tmpDir2, err := ioutil.TempDir("", "docker_nested_mount_test2")
  1715. if err != nil {
  1716. t.Fatal(err)
  1717. }
  1718. defer os.RemoveAll(tmpDir2)
  1719. // Create a temporary tmpfs mount.
  1720. fooDir := filepath.Join(tmpDir, "foo")
  1721. if err := os.MkdirAll(filepath.Join(tmpDir, "foo"), 0755); err != nil {
  1722. t.Fatalf("failed to mkdir at %s - %s", fooDir, err)
  1723. }
  1724. if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", fooDir), []byte{}, 0644); err != nil {
  1725. t.Fatal(err)
  1726. }
  1727. if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir), []byte{}, 0644); err != nil {
  1728. t.Fatal(err)
  1729. }
  1730. if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir2), []byte{}, 0644); err != nil {
  1731. t.Fatal(err)
  1732. }
  1733. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp", tmpDir), "-v", fmt.Sprintf("%s:/tmp/foo", fooDir), "-v", fmt.Sprintf("%s:/tmp/tmp2", tmpDir2), "-v", fmt.Sprintf("%s:/tmp/tmp2/foo", fooDir), "busybox:latest", "sh", "-c", "ls /tmp/touch-me && ls /tmp/foo/touch-me && ls /tmp/tmp2/touch-me && ls /tmp/tmp2/foo/touch-me")
  1734. out, _, err := runCommandWithOutput(cmd)
  1735. if err != nil {
  1736. t.Fatal(out, err)
  1737. }
  1738. deleteAllContainers()
  1739. logDone("run - volumes are mounted in the correct order")
  1740. }
  1741. func TestRunExecDir(t *testing.T) {
  1742. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  1743. out, _, err := runCommandWithOutput(cmd)
  1744. if err != nil {
  1745. t.Fatal(err, out)
  1746. }
  1747. id := strings.TrimSpace(out)
  1748. execDir := filepath.Join(execDriverPath, id)
  1749. stateFile := filepath.Join(execDir, "state.json")
  1750. contFile := filepath.Join(execDir, "container.json")
  1751. {
  1752. fi, err := os.Stat(execDir)
  1753. if err != nil {
  1754. t.Fatal(err)
  1755. }
  1756. if !fi.IsDir() {
  1757. t.Fatalf("%q must be a directory", execDir)
  1758. }
  1759. fi, err = os.Stat(stateFile)
  1760. if err != nil {
  1761. t.Fatal(err)
  1762. }
  1763. fi, err = os.Stat(contFile)
  1764. if err != nil {
  1765. t.Fatal(err)
  1766. }
  1767. }
  1768. stopCmd := exec.Command(dockerBinary, "stop", id)
  1769. out, _, err = runCommandWithOutput(stopCmd)
  1770. if err != nil {
  1771. t.Fatal(err, out)
  1772. }
  1773. {
  1774. fi, err := os.Stat(execDir)
  1775. if err != nil {
  1776. t.Fatal(err)
  1777. }
  1778. if !fi.IsDir() {
  1779. t.Fatalf("%q must be a directory", execDir)
  1780. }
  1781. fi, err = os.Stat(stateFile)
  1782. if err == nil {
  1783. t.Fatalf("Statefile %q is exists for stopped container!", stateFile)
  1784. }
  1785. if !os.IsNotExist(err) {
  1786. t.Fatalf("Error should be about non-existing, got %s", err)
  1787. }
  1788. fi, err = os.Stat(contFile)
  1789. if err == nil {
  1790. t.Fatalf("Container file %q is exists for stopped container!", contFile)
  1791. }
  1792. if !os.IsNotExist(err) {
  1793. t.Fatalf("Error should be about non-existing, got %s", err)
  1794. }
  1795. }
  1796. startCmd := exec.Command(dockerBinary, "start", id)
  1797. out, _, err = runCommandWithOutput(startCmd)
  1798. if err != nil {
  1799. t.Fatal(err, out)
  1800. }
  1801. {
  1802. fi, err := os.Stat(execDir)
  1803. if err != nil {
  1804. t.Fatal(err)
  1805. }
  1806. if !fi.IsDir() {
  1807. t.Fatalf("%q must be a directory", execDir)
  1808. }
  1809. fi, err = os.Stat(stateFile)
  1810. if err != nil {
  1811. t.Fatal(err)
  1812. }
  1813. fi, err = os.Stat(contFile)
  1814. if err != nil {
  1815. t.Fatal(err)
  1816. }
  1817. }
  1818. rmCmd := exec.Command(dockerBinary, "rm", "-f", id)
  1819. out, _, err = runCommandWithOutput(rmCmd)
  1820. if err != nil {
  1821. t.Fatal(err, out)
  1822. }
  1823. {
  1824. _, err := os.Stat(execDir)
  1825. if err == nil {
  1826. t.Fatal(err)
  1827. }
  1828. if err == nil {
  1829. t.Fatalf("Exec directory %q is exists for removed container!", execDir)
  1830. }
  1831. if !os.IsNotExist(err) {
  1832. t.Fatalf("Error should be about non-existing, got %s", err)
  1833. }
  1834. }
  1835. logDone("run - check execdriver dir behavior")
  1836. }
  1837. // #6509
  1838. func TestRunRedirectStdout(t *testing.T) {
  1839. defer deleteAllContainers()
  1840. checkRedirect := func(command string) {
  1841. _, tty, err := pty.Open()
  1842. if err != nil {
  1843. t.Fatalf("Could not open pty: %v", err)
  1844. }
  1845. cmd := exec.Command("sh", "-c", command)
  1846. cmd.Stdin = tty
  1847. cmd.Stdout = tty
  1848. cmd.Stderr = tty
  1849. ch := make(chan struct{})
  1850. if err := cmd.Start(); err != nil {
  1851. t.Fatalf("start err: %v", err)
  1852. }
  1853. go func() {
  1854. if err := cmd.Wait(); err != nil {
  1855. t.Fatalf("wait err=%v", err)
  1856. }
  1857. close(ch)
  1858. }()
  1859. select {
  1860. case <-time.After(time.Second):
  1861. t.Fatal("command timeout")
  1862. case <-ch:
  1863. }
  1864. }
  1865. checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
  1866. checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
  1867. logDone("run - redirect stdout")
  1868. }
  1869. // Regression test for https://github.com/docker/docker/issues/8259
  1870. func TestRunReuseBindVolumeThatIsSymlink(t *testing.T) {
  1871. tmpDir, err := ioutil.TempDir(os.TempDir(), "testlink")
  1872. if err != nil {
  1873. t.Fatal(err)
  1874. }
  1875. defer os.RemoveAll(tmpDir)
  1876. linkPath := os.TempDir() + "/testlink2"
  1877. if err := os.Symlink(tmpDir, linkPath); err != nil {
  1878. t.Fatal(err)
  1879. }
  1880. defer os.RemoveAll(linkPath)
  1881. // Create first container
  1882. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp/test", linkPath), "busybox", "ls", "-lh", "/tmp/test")
  1883. if _, err := runCommand(cmd); err != nil {
  1884. t.Fatal(err)
  1885. }
  1886. // Create second container with same symlinked path
  1887. // This will fail if the referenced issue is hit with a "Volume exists" error
  1888. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp/test", linkPath), "busybox", "ls", "-lh", "/tmp/test")
  1889. if out, _, err := runCommandWithOutput(cmd); err != nil {
  1890. t.Fatal(err, out)
  1891. }
  1892. deleteAllContainers()
  1893. logDone("run - can remount old bindmount volume")
  1894. }
  1895. func TestVolumesNoCopyData(t *testing.T) {
  1896. defer deleteImages("dataimage")
  1897. defer deleteAllContainers()
  1898. if _, err := buildImage("dataimage",
  1899. `FROM busybox
  1900. RUN mkdir -p /foo
  1901. RUN touch /foo/bar`,
  1902. true); err != nil {
  1903. t.Fatal(err)
  1904. }
  1905. cmd := exec.Command(dockerBinary, "run", "--name", "test", "-v", "/foo", "busybox")
  1906. if _, err := runCommand(cmd); err != nil {
  1907. t.Fatal(err)
  1908. }
  1909. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test", "dataimage", "ls", "-lh", "/foo/bar")
  1910. if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "No such file or directory") {
  1911. t.Fatalf("Data was copied on volumes-from but shouldn't be:\n%q", out)
  1912. }
  1913. tmpDir, err := ioutil.TempDir("", "docker_test_bind_mount_copy_data")
  1914. if err != nil {
  1915. t.Fatal(err)
  1916. }
  1917. defer os.RemoveAll(tmpDir)
  1918. cmd = exec.Command(dockerBinary, "run", "-v", tmpDir+":/foo", "dataimage", "ls", "-lh", "/foo/bar")
  1919. if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "No such file or directory") {
  1920. t.Fatalf("Data was copied on bind-mount but shouldn't be:\n%q", out)
  1921. }
  1922. logDone("run - volumes do not copy data for volumes-from and bindmounts")
  1923. }