docker_cli_run_test.go 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187
  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "os"
  9. "os/exec"
  10. "path"
  11. "path/filepath"
  12. "reflect"
  13. "regexp"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "time"
  19. "github.com/docker/docker/nat"
  20. "github.com/docker/libnetwork/resolvconf"
  21. "github.com/go-check/check"
  22. )
  23. // "test123" should be printed by docker run
  24. func (s *DockerSuite) TestRunEchoStdout(c *check.C) {
  25. runCmd := exec.Command(dockerBinary, "run", "busybox", "echo", "test123")
  26. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  27. if err != nil {
  28. c.Fatalf("failed to run container: %v, output: %q", err, out)
  29. }
  30. if out != "test123\n" {
  31. c.Fatalf("container should've printed 'test123'")
  32. }
  33. }
  34. // "test" should be printed
  35. func (s *DockerSuite) TestRunEchoStdoutWithMemoryLimit(c *check.C) {
  36. runCmd := exec.Command(dockerBinary, "run", "-m", "16m", "busybox", "echo", "test")
  37. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  38. if err != nil {
  39. c.Fatalf("failed to run container: %v, output: %q", err, out)
  40. }
  41. out = strings.Trim(out, "\r\n")
  42. if expected := "test"; out != expected {
  43. c.Fatalf("container should've printed %q but printed %q", expected, out)
  44. }
  45. }
  46. // should run without memory swap
  47. func (s *DockerSuite) TestRunWithoutMemoryswapLimit(c *check.C) {
  48. runCmd := exec.Command(dockerBinary, "run", "-m", "16m", "--memory-swap", "-1", "busybox", "true")
  49. out, _, err := runCommandWithOutput(runCmd)
  50. if err != nil {
  51. c.Fatalf("failed to run container, output: %q", out)
  52. }
  53. }
  54. // "test" should be printed
  55. func (s *DockerSuite) TestRunEchoStdoutWitCPULimit(c *check.C) {
  56. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "busybox", "echo", "test")
  57. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  58. if err != nil {
  59. c.Fatalf("failed to run container: %v, output: %q", err, out)
  60. }
  61. if out != "test\n" {
  62. c.Errorf("container should've printed 'test'")
  63. }
  64. }
  65. // "test" should be printed
  66. func (s *DockerSuite) TestRunEchoStdoutWithCPUAndMemoryLimit(c *check.C) {
  67. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "-m", "16m", "busybox", "echo", "test")
  68. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  69. if err != nil {
  70. c.Fatalf("failed to run container: %v, output: %q", err, out)
  71. }
  72. if out != "test\n" {
  73. c.Errorf("container should've printed 'test', got %q instead", out)
  74. }
  75. }
  76. // "test" should be printed
  77. func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
  78. runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
  79. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  80. if err != nil {
  81. c.Fatalf("failed to run container: %v, output: %q", err, out)
  82. }
  83. out = strings.TrimSpace(out)
  84. if strings.Contains(out, "Your kernel does not support CPU cfs quota") {
  85. c.Skip("Your kernel does not support CPU cfs quota, skip this test")
  86. }
  87. if out != "test" {
  88. c.Errorf("container should've printed 'test'")
  89. }
  90. out, err = inspectField("test", "HostConfig.CpuQuota")
  91. c.Assert(err, check.IsNil)
  92. if out != "8000" {
  93. c.Errorf("setting the CPU CFS quota failed")
  94. }
  95. }
  96. // "test" should be printed
  97. func (s *DockerSuite) TestRunEchoNamedContainer(c *check.C) {
  98. runCmd := exec.Command(dockerBinary, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test")
  99. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  100. if err != nil {
  101. c.Fatalf("failed to run container: %v, output: %q", err, out)
  102. }
  103. if out != "test\n" {
  104. c.Errorf("container should've printed 'test'")
  105. }
  106. if err := deleteContainer("testfoonamedcontainer"); err != nil {
  107. c.Errorf("failed to remove the named container: %v", err)
  108. }
  109. }
  110. // docker run should not leak file descriptors
  111. func (s *DockerSuite) TestRunLeakyFileDescriptors(c *check.C) {
  112. runCmd := exec.Command(dockerBinary, "run", "busybox", "ls", "-C", "/proc/self/fd")
  113. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  114. if err != nil {
  115. c.Fatalf("failed to run container: %v, output: %q", err, out)
  116. }
  117. // normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory
  118. if out != "0 1 2 3\n" {
  119. c.Errorf("container should've printed '0 1 2 3', not: %s", out)
  120. }
  121. }
  122. // it should be possible to lookup Google DNS
  123. // this will fail when Internet access is unavailable
  124. func (s *DockerSuite) TestRunLookupGoogleDns(c *check.C) {
  125. testRequires(c, Network)
  126. out, _, _, err := runCommandWithStdoutStderr(exec.Command(dockerBinary, "run", "busybox", "nslookup", "google.com"))
  127. if err != nil {
  128. c.Fatalf("failed to run container: %v, output: %q", err, out)
  129. }
  130. }
  131. // the exit code should be 0
  132. // some versions of lxc might make this test fail
  133. func (s *DockerSuite) TestRunExitCodeZero(c *check.C) {
  134. runCmd := exec.Command(dockerBinary, "run", "busybox", "true")
  135. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  136. c.Errorf("container should've exited with exit code 0: %s, %v", out, err)
  137. }
  138. }
  139. // the exit code should be 1
  140. // some versions of lxc might make this test fail
  141. func (s *DockerSuite) TestRunExitCodeOne(c *check.C) {
  142. runCmd := exec.Command(dockerBinary, "run", "busybox", "false")
  143. exitCode, err := runCommand(runCmd)
  144. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  145. c.Fatal(err)
  146. }
  147. if exitCode != 1 {
  148. c.Errorf("container should've exited with exit code 1")
  149. }
  150. }
  151. // it should be possible to pipe in data via stdin to a process running in a container
  152. // some versions of lxc might make this test fail
  153. func (s *DockerSuite) TestRunStdinPipe(c *check.C) {
  154. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "cat")
  155. runCmd.Stdin = strings.NewReader("blahblah")
  156. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  157. if err != nil {
  158. c.Fatalf("failed to run container: %v, output: %q", err, out)
  159. }
  160. out = strings.TrimSpace(out)
  161. waitCmd := exec.Command(dockerBinary, "wait", out)
  162. if waitOut, _, err := runCommandWithOutput(waitCmd); err != nil {
  163. c.Fatalf("error thrown while waiting for container: %s, %v", waitOut, err)
  164. }
  165. logsCmd := exec.Command(dockerBinary, "logs", out)
  166. logsOut, _, err := runCommandWithOutput(logsCmd)
  167. if err != nil {
  168. c.Fatalf("error thrown while trying to get container logs: %s, %v", logsOut, err)
  169. }
  170. containerLogs := strings.TrimSpace(logsOut)
  171. if containerLogs != "blahblah" {
  172. c.Errorf("logs didn't print the container's logs %s", containerLogs)
  173. }
  174. rmCmd := exec.Command(dockerBinary, "rm", out)
  175. if out, _, err = runCommandWithOutput(rmCmd); err != nil {
  176. c.Fatalf("rm failed to remove container: %s, %v", out, err)
  177. }
  178. }
  179. // the container's ID should be printed when starting a container in detached mode
  180. func (s *DockerSuite) TestRunDetachedContainerIDPrinting(c *check.C) {
  181. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  182. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  183. if err != nil {
  184. c.Fatalf("failed to run container: %v, output: %q", err, out)
  185. }
  186. out = strings.TrimSpace(out)
  187. waitCmd := exec.Command(dockerBinary, "wait", out)
  188. if waitOut, _, err := runCommandWithOutput(waitCmd); err != nil {
  189. c.Fatalf("error thrown while waiting for container: %s, %v", waitOut, err)
  190. }
  191. rmCmd := exec.Command(dockerBinary, "rm", out)
  192. rmOut, _, err := runCommandWithOutput(rmCmd)
  193. if err != nil {
  194. c.Fatalf("rm failed to remove container: %s, %v", rmOut, err)
  195. }
  196. rmOut = strings.TrimSpace(rmOut)
  197. if rmOut != out {
  198. c.Errorf("rm didn't print the container ID %s %s", out, rmOut)
  199. }
  200. }
  201. // the working directory should be set correctly
  202. func (s *DockerSuite) TestRunWorkingDirectory(c *check.C) {
  203. runCmd := exec.Command(dockerBinary, "run", "-w", "/root", "busybox", "pwd")
  204. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  205. if err != nil {
  206. c.Fatalf("failed to run container: %v, output: %q", err, out)
  207. }
  208. out = strings.TrimSpace(out)
  209. if out != "/root" {
  210. c.Errorf("-w failed to set working directory")
  211. }
  212. runCmd = exec.Command(dockerBinary, "run", "--workdir", "/root", "busybox", "pwd")
  213. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  214. if err != nil {
  215. c.Fatal(out, err)
  216. }
  217. out = strings.TrimSpace(out)
  218. if out != "/root" {
  219. c.Errorf("--workdir failed to set working directory")
  220. }
  221. }
  222. // pinging Google's DNS resolver should fail when we disable the networking
  223. func (s *DockerSuite) TestRunWithoutNetworking(c *check.C) {
  224. runCmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "8.8.8.8")
  225. out, _, exitCode, err := runCommandWithStdoutStderr(runCmd)
  226. if err != nil && exitCode != 1 {
  227. c.Fatal(out, err)
  228. }
  229. if exitCode != 1 {
  230. c.Errorf("--net=none should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  231. }
  232. runCmd = exec.Command(dockerBinary, "run", "-n=false", "busybox", "ping", "-c", "1", "8.8.8.8")
  233. out, _, exitCode, err = runCommandWithStdoutStderr(runCmd)
  234. if err != nil && exitCode != 1 {
  235. c.Fatal(out, err)
  236. }
  237. if exitCode != 1 {
  238. c.Errorf("-n=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  239. }
  240. }
  241. //test --link use container name to link target
  242. func (s *DockerSuite) TestRunLinksContainerWithContainerName(c *check.C) {
  243. cmd := exec.Command(dockerBinary, "run", "-i", "-t", "-d", "--name", "parent", "busybox")
  244. out, _, _, err := runCommandWithStdoutStderr(cmd)
  245. if err != nil {
  246. c.Fatalf("failed to run container: %v, output: %q", err, out)
  247. }
  248. ip, err := inspectField("parent", "NetworkSettings.IPAddress")
  249. c.Assert(err, check.IsNil)
  250. cmd = exec.Command(dockerBinary, "run", "--link", "parent:test", "busybox", "/bin/cat", "/etc/hosts")
  251. out, _, err = runCommandWithOutput(cmd)
  252. if err != nil {
  253. c.Fatalf("failed to run container: %v, output: %q", err, out)
  254. }
  255. if !strings.Contains(out, ip+" test") {
  256. c.Fatalf("use a container name to link target failed")
  257. }
  258. }
  259. //test --link use container id to link target
  260. func (s *DockerSuite) TestRunLinksContainerWithContainerId(c *check.C) {
  261. cmd := exec.Command(dockerBinary, "run", "-i", "-t", "-d", "busybox")
  262. cID, _, _, err := runCommandWithStdoutStderr(cmd)
  263. if err != nil {
  264. c.Fatalf("failed to run container: %v, output: %q", err, cID)
  265. }
  266. cID = strings.TrimSpace(cID)
  267. ip, err := inspectField(cID, "NetworkSettings.IPAddress")
  268. c.Assert(err, check.IsNil)
  269. cmd = exec.Command(dockerBinary, "run", "--link", cID+":test", "busybox", "/bin/cat", "/etc/hosts")
  270. out, _, err := runCommandWithOutput(cmd)
  271. if err != nil {
  272. c.Fatalf("failed to run container: %v, output: %q", err, out)
  273. }
  274. if !strings.Contains(out, ip+" test") {
  275. c.Fatalf("use a container id to link target failed")
  276. }
  277. }
  278. func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) {
  279. cmd := exec.Command(dockerBinary, "run", "--name", "test", "-d", "busybox", "top")
  280. out, _, err := runCommandWithOutput(cmd)
  281. if err != nil {
  282. c.Fatalf("failed to run container: %v, output: %q", err, out)
  283. }
  284. cmd = exec.Command(dockerBinary, "run", "--name", "parent", "-d", "--net=container:test", "busybox", "top")
  285. out, _, err = runCommandWithOutput(cmd)
  286. if err != nil {
  287. c.Fatalf("failed to run container: %v, output: %q", err, out)
  288. }
  289. cmd = exec.Command(dockerBinary, "run", "-d", "--link=parent:parent", "busybox", "top")
  290. out, _, err = runCommandWithOutput(cmd)
  291. if err != nil {
  292. c.Fatalf("failed to run container: %v, output: %q", err, out)
  293. }
  294. cmd = exec.Command(dockerBinary, "run", "--name", "child", "-d", "--net=container:parent", "busybox", "top")
  295. out, _, err = runCommandWithOutput(cmd)
  296. if err != nil {
  297. c.Fatalf("failed to run container: %v, output: %q", err, out)
  298. }
  299. cmd = exec.Command(dockerBinary, "run", "-d", "--link=child:child", "busybox", "top")
  300. out, _, err = runCommandWithOutput(cmd)
  301. if err != nil {
  302. c.Fatalf("failed to run container: %v, output: %q", err, out)
  303. }
  304. }
  305. func (s *DockerSuite) TestRunContainerNetModeWithDnsMacHosts(c *check.C) {
  306. cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
  307. out, _, err := runCommandWithOutput(cmd)
  308. if err != nil {
  309. c.Fatalf("failed to run container: %v, output: %q", err, out)
  310. }
  311. cmd = exec.Command(dockerBinary, "run", "--dns", "1.2.3.4", "--net=container:parent", "busybox")
  312. out, _, err = runCommandWithOutput(cmd)
  313. if err == nil || !strings.Contains(out, "Conflicting options: --dns and the network mode") {
  314. c.Fatalf("run --net=container with --dns should error out")
  315. }
  316. cmd = exec.Command(dockerBinary, "run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox")
  317. out, _, err = runCommandWithOutput(cmd)
  318. if err == nil || !strings.Contains(out, "--mac-address and the network mode") {
  319. c.Fatalf("run --net=container with --mac-address should error out")
  320. }
  321. cmd = exec.Command(dockerBinary, "run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox")
  322. out, _, err = runCommandWithOutput(cmd)
  323. if err == nil || !strings.Contains(out, "--add-host and the network mode") {
  324. c.Fatalf("run --net=container with --add-host should error out")
  325. }
  326. }
  327. func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) {
  328. testRequires(c, ExecSupport)
  329. cmd := exec.Command(dockerBinary, "run", "-i", "-d", "--name", "parent", "busybox", "top")
  330. out, _, err := runCommandWithOutput(cmd)
  331. if err != nil {
  332. c.Fatalf("failed to run container: %v, output: %q", err, out)
  333. }
  334. cmd = exec.Command(dockerBinary, "exec", "parent", "cat", "/etc/hostname")
  335. out, _, err = runCommandWithOutput(cmd)
  336. if err != nil {
  337. c.Fatalf("failed to exec command: %v, output: %q", err, out)
  338. }
  339. cmd = exec.Command(dockerBinary, "run", "--net=container:parent", "busybox", "cat", "/etc/hostname")
  340. out1, _, err := runCommandWithOutput(cmd)
  341. if err != nil {
  342. c.Fatalf("failed to run container: %v, output: %q", err, out1)
  343. }
  344. if out1 != out {
  345. c.Fatal("containers with shared net namespace should have same hostname")
  346. }
  347. }
  348. // Regression test for #4741
  349. func (s *DockerSuite) TestRunWithVolumesAsFiles(c *check.C) {
  350. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
  351. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  352. if err != nil && exitCode != 0 {
  353. c.Fatal("1", out, stderr, err)
  354. }
  355. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file")
  356. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  357. if err != nil && exitCode != 0 {
  358. c.Fatal("2", out, stderr, err)
  359. }
  360. }
  361. // Regression test for #4979
  362. func (s *DockerSuite) TestRunWithVolumesFromExited(c *check.C) {
  363. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file")
  364. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  365. if err != nil && exitCode != 0 {
  366. c.Fatal("1", out, stderr, err)
  367. }
  368. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/some/dir/file")
  369. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  370. if err != nil && exitCode != 0 {
  371. c.Fatal("2", out, stderr, err)
  372. }
  373. }
  374. // Volume path is a symlink which also exists on the host, and the host side is a file not a dir
  375. // But the volume call is just a normal volume, not a bind mount
  376. func (s *DockerSuite) TestRunCreateVolumesInSymlinkDir(c *check.C) {
  377. testRequires(c, SameHostDaemon)
  378. testRequires(c, NativeExecDriver)
  379. name := "test-volume-symlink"
  380. dir, err := ioutil.TempDir("", name)
  381. if err != nil {
  382. c.Fatal(err)
  383. }
  384. defer os.RemoveAll(dir)
  385. f, err := os.OpenFile(filepath.Join(dir, "test"), os.O_CREATE, 0700)
  386. if err != nil {
  387. c.Fatal(err)
  388. }
  389. f.Close()
  390. dockerFile := fmt.Sprintf("FROM busybox\nRUN mkdir -p %s\nRUN ln -s %s /test", dir, dir)
  391. if _, err := buildImage(name, dockerFile, false); err != nil {
  392. c.Fatal(err)
  393. }
  394. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-v", "/test/test", name))
  395. if err != nil {
  396. c.Fatalf("Failed with errors: %s, %v", out, err)
  397. }
  398. }
  399. // Regression test for #4830
  400. func (s *DockerSuite) TestRunWithRelativePath(c *check.C) {
  401. runCmd := exec.Command(dockerBinary, "run", "-v", "tmp:/other-tmp", "busybox", "true")
  402. if _, _, _, err := runCommandWithStdoutStderr(runCmd); err == nil {
  403. c.Fatalf("relative path should result in an error")
  404. }
  405. }
  406. func (s *DockerSuite) TestRunVolumesMountedAsReadonly(c *check.C) {
  407. cmd := exec.Command(dockerBinary, "run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile")
  408. if code, err := runCommand(cmd); err == nil || code == 0 {
  409. c.Fatalf("run should fail because volume is ro: exit code %d", code)
  410. }
  411. }
  412. func (s *DockerSuite) TestRunVolumesFromInReadonlyMode(c *check.C) {
  413. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  414. if _, err := runCommand(cmd); err != nil {
  415. c.Fatal(err)
  416. }
  417. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:ro", "busybox", "touch", "/test/file")
  418. if code, err := runCommand(cmd); err == nil || code == 0 {
  419. c.Fatalf("run should fail because volume is ro: exit code %d", code)
  420. }
  421. }
  422. // Regression test for #1201
  423. func (s *DockerSuite) TestRunVolumesFromInReadWriteMode(c *check.C) {
  424. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  425. if _, err := runCommand(cmd); err != nil {
  426. c.Fatal(err)
  427. }
  428. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file")
  429. if out, _, err := runCommandWithOutput(cmd); err != nil {
  430. c.Fatalf("running --volumes-from parent:rw failed with output: %q\nerror: %v", out, err)
  431. }
  432. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:bar", "busybox", "touch", "/test/file")
  433. if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "invalid mode for volumes-from: bar") {
  434. c.Fatalf("running --volumes-from foo:bar should have failed with invalid mount mode: %q", out)
  435. }
  436. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "busybox", "touch", "/test/file")
  437. if out, _, err := runCommandWithOutput(cmd); err != nil {
  438. c.Fatalf("running --volumes-from parent failed with output: %q\nerror: %v", out, err)
  439. }
  440. }
  441. func (s *DockerSuite) TestVolumesFromGetsProperMode(c *check.C) {
  442. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test:/test:ro", "busybox", "true")
  443. if _, err := runCommand(cmd); err != nil {
  444. c.Fatal(err)
  445. }
  446. // Expect this "rw" mode to be be ignored since the inherited volume is "ro"
  447. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file")
  448. if _, err := runCommand(cmd); err == nil {
  449. c.Fatal("Expected volumes-from to inherit read-only volume even when passing in `rw`")
  450. }
  451. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/test:/test:ro", "busybox", "true")
  452. if _, err := runCommand(cmd); err != nil {
  453. c.Fatal(err)
  454. }
  455. // Expect this to be read-only since both are "ro"
  456. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent2:ro", "busybox", "touch", "/test/file")
  457. if _, err := runCommand(cmd); err == nil {
  458. c.Fatal("Expected volumes-from to inherit read-only volume even when passing in `ro`")
  459. }
  460. }
  461. // Test for GH#10618
  462. func (s *DockerSuite) TestRunNoDupVolumes(c *check.C) {
  463. mountstr1 := randomUnixTmpDirPath("test1") + ":/someplace"
  464. mountstr2 := randomUnixTmpDirPath("test2") + ":/someplace"
  465. cmd := exec.Command(dockerBinary, "run", "-v", mountstr1, "-v", mountstr2, "busybox", "true")
  466. if out, _, err := runCommandWithOutput(cmd); err == nil {
  467. c.Fatal("Expected error about duplicate volume definitions")
  468. } else {
  469. if !strings.Contains(out, "Duplicate volume") {
  470. c.Fatalf("Expected 'duplicate volume' error, got %v", err)
  471. }
  472. }
  473. }
  474. // Test for #1351
  475. func (s *DockerSuite) TestRunApplyVolumesFromBeforeVolumes(c *check.C) {
  476. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo")
  477. if _, err := runCommand(cmd); err != nil {
  478. c.Fatal(err)
  479. }
  480. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "-v", "/test", "busybox", "cat", "/test/foo")
  481. if out, _, err := runCommandWithOutput(cmd); err != nil {
  482. c.Fatal(out, err)
  483. }
  484. }
  485. func (s *DockerSuite) TestRunMultipleVolumesFrom(c *check.C) {
  486. cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo")
  487. if _, err := runCommand(cmd); err != nil {
  488. c.Fatal(err)
  489. }
  490. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/other", "busybox", "touch", "/other/bar")
  491. if _, err := runCommand(cmd); err != nil {
  492. c.Fatal(err)
  493. }
  494. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent1", "--volumes-from", "parent2",
  495. "busybox", "sh", "-c", "cat /test/foo && cat /other/bar")
  496. if _, err := runCommand(cmd); err != nil {
  497. c.Fatal(err)
  498. }
  499. }
  500. // this tests verifies the ID format for the container
  501. func (s *DockerSuite) TestRunVerifyContainerID(c *check.C) {
  502. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  503. out, exit, err := runCommandWithOutput(cmd)
  504. if err != nil {
  505. c.Fatal(err)
  506. }
  507. if exit != 0 {
  508. c.Fatalf("expected exit code 0 received %d", exit)
  509. }
  510. match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n"))
  511. if err != nil {
  512. c.Fatal(err)
  513. }
  514. if !match {
  515. c.Fatalf("Invalid container ID: %s", out)
  516. }
  517. }
  518. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  519. func (s *DockerSuite) TestRunCreateVolume(c *check.C) {
  520. cmd := exec.Command(dockerBinary, "run", "-v", "/var/lib/data", "busybox", "true")
  521. if _, err := runCommand(cmd); err != nil {
  522. c.Fatal(err)
  523. }
  524. }
  525. // Test that creating a volume with a symlink in its path works correctly. Test for #5152.
  526. // Note that this bug happens only with symlinks with a target that starts with '/'.
  527. func (s *DockerSuite) TestRunCreateVolumeWithSymlink(c *check.C) {
  528. image := "docker-test-createvolumewithsymlink"
  529. buildCmd := exec.Command(dockerBinary, "build", "-t", image, "-")
  530. buildCmd.Stdin = strings.NewReader(`FROM busybox
  531. RUN ln -s home /bar`)
  532. buildCmd.Dir = workingDirectory
  533. err := buildCmd.Run()
  534. if err != nil {
  535. c.Fatalf("could not build '%s': %v", image, err)
  536. }
  537. cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", image, "sh", "-c", "mount | grep -q /home/foo")
  538. exitCode, err := runCommand(cmd)
  539. if err != nil || exitCode != 0 {
  540. c.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  541. }
  542. var volPath string
  543. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{range .Volumes}}{{.}}{{end}}", "test-createvolumewithsymlink")
  544. volPath, exitCode, err = runCommandWithOutput(cmd)
  545. if err != nil || exitCode != 0 {
  546. c.Fatalf("[inspect] err: %v, exitcode: %d", err, exitCode)
  547. }
  548. cmd = exec.Command(dockerBinary, "rm", "-v", "test-createvolumewithsymlink")
  549. exitCode, err = runCommand(cmd)
  550. if err != nil || exitCode != 0 {
  551. c.Fatalf("[rm] err: %v, exitcode: %d", err, exitCode)
  552. }
  553. f, err := os.Open(volPath)
  554. defer f.Close()
  555. if !os.IsNotExist(err) {
  556. c.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
  557. }
  558. }
  559. // Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`.
  560. func (s *DockerSuite) TestRunVolumesFromSymlinkPath(c *check.C) {
  561. name := "docker-test-volumesfromsymlinkpath"
  562. buildCmd := exec.Command(dockerBinary, "build", "-t", name, "-")
  563. buildCmd.Stdin = strings.NewReader(`FROM busybox
  564. RUN ln -s home /foo
  565. VOLUME ["/foo/bar"]`)
  566. buildCmd.Dir = workingDirectory
  567. err := buildCmd.Run()
  568. if err != nil {
  569. c.Fatalf("could not build 'docker-test-volumesfromsymlinkpath': %v", err)
  570. }
  571. cmd := exec.Command(dockerBinary, "run", "--name", "test-volumesfromsymlinkpath", name)
  572. exitCode, err := runCommand(cmd)
  573. if err != nil || exitCode != 0 {
  574. c.Fatalf("[run] (volume) err: %v, exitcode: %d", err, exitCode)
  575. }
  576. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-volumesfromsymlinkpath", "busybox", "sh", "-c", "ls /foo | grep -q bar")
  577. exitCode, err = runCommand(cmd)
  578. if err != nil || exitCode != 0 {
  579. c.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  580. }
  581. }
  582. func (s *DockerSuite) TestRunExitCode(c *check.C) {
  583. cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72")
  584. exit, err := runCommand(cmd)
  585. if err == nil {
  586. c.Fatal("should not have a non nil error")
  587. }
  588. if exit != 72 {
  589. c.Fatalf("expected exit code 72 received %d", exit)
  590. }
  591. }
  592. func (s *DockerSuite) TestRunUserDefaultsToRoot(c *check.C) {
  593. cmd := exec.Command(dockerBinary, "run", "busybox", "id")
  594. out, _, err := runCommandWithOutput(cmd)
  595. if err != nil {
  596. c.Fatal(err, out)
  597. }
  598. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  599. c.Fatalf("expected root user got %s", out)
  600. }
  601. }
  602. func (s *DockerSuite) TestRunUserByName(c *check.C) {
  603. cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
  604. out, _, err := runCommandWithOutput(cmd)
  605. if err != nil {
  606. c.Fatal(err, out)
  607. }
  608. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  609. c.Fatalf("expected root user got %s", out)
  610. }
  611. }
  612. func (s *DockerSuite) TestRunUserByID(c *check.C) {
  613. cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
  614. out, _, err := runCommandWithOutput(cmd)
  615. if err != nil {
  616. c.Fatal(err, out)
  617. }
  618. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  619. c.Fatalf("expected daemon user got %s", out)
  620. }
  621. }
  622. func (s *DockerSuite) TestRunUserByIDBig(c *check.C) {
  623. cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id")
  624. out, _, err := runCommandWithOutput(cmd)
  625. if err == nil {
  626. c.Fatal("No error, but must be.", out)
  627. }
  628. if !strings.Contains(out, "Uids and gids must be in range") {
  629. c.Fatalf("expected error about uids range, got %s", out)
  630. }
  631. }
  632. func (s *DockerSuite) TestRunUserByIDNegative(c *check.C) {
  633. cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id")
  634. out, _, err := runCommandWithOutput(cmd)
  635. if err == nil {
  636. c.Fatal("No error, but must be.", out)
  637. }
  638. if !strings.Contains(out, "Uids and gids must be in range") {
  639. c.Fatalf("expected error about uids range, got %s", out)
  640. }
  641. }
  642. func (s *DockerSuite) TestRunUserByIDZero(c *check.C) {
  643. cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id")
  644. out, _, err := runCommandWithOutput(cmd)
  645. if err != nil {
  646. c.Fatal(err, out)
  647. }
  648. if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") {
  649. c.Fatalf("expected daemon user got %s", out)
  650. }
  651. }
  652. func (s *DockerSuite) TestRunUserNotFound(c *check.C) {
  653. cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
  654. _, err := runCommand(cmd)
  655. if err == nil {
  656. c.Fatal("unknown user should cause container to fail")
  657. }
  658. }
  659. func (s *DockerSuite) TestRunTwoConcurrentContainers(c *check.C) {
  660. group := sync.WaitGroup{}
  661. group.Add(2)
  662. errChan := make(chan error, 2)
  663. for i := 0; i < 2; i++ {
  664. go func() {
  665. defer group.Done()
  666. cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2")
  667. _, err := runCommand(cmd)
  668. errChan <- err
  669. }()
  670. }
  671. group.Wait()
  672. close(errChan)
  673. for err := range errChan {
  674. c.Assert(err, check.IsNil)
  675. }
  676. }
  677. func (s *DockerSuite) TestRunEnvironment(c *check.C) {
  678. cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "-e=HOME=", "busybox", "env")
  679. cmd.Env = append(os.Environ(),
  680. "TRUE=false",
  681. "TRICKY=tri\ncky\n",
  682. )
  683. out, _, err := runCommandWithOutput(cmd)
  684. if err != nil {
  685. c.Fatal(err, out)
  686. }
  687. actualEnvLxc := strings.Split(strings.TrimSpace(out), "\n")
  688. actualEnv := []string{}
  689. for i := range actualEnvLxc {
  690. if actualEnvLxc[i] != "container=lxc" {
  691. actualEnv = append(actualEnv, actualEnvLxc[i])
  692. }
  693. }
  694. sort.Strings(actualEnv)
  695. goodEnv := []string{
  696. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  697. "HOSTNAME=testing",
  698. "FALSE=true",
  699. "TRUE=false",
  700. "TRICKY=tri",
  701. "cky",
  702. "",
  703. "HOME=/root",
  704. }
  705. sort.Strings(goodEnv)
  706. if len(goodEnv) != len(actualEnv) {
  707. c.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", "))
  708. }
  709. for i := range goodEnv {
  710. if actualEnv[i] != goodEnv[i] {
  711. c.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  712. }
  713. }
  714. }
  715. func (s *DockerSuite) TestRunEnvironmentErase(c *check.C) {
  716. // Test to make sure that when we use -e on env vars that are
  717. // not set in our local env that they're removed (if present) in
  718. // the container
  719. cmd := exec.Command(dockerBinary, "run", "-e", "FOO", "-e", "HOSTNAME", "busybox", "env")
  720. cmd.Env = appendBaseEnv([]string{})
  721. out, _, err := runCommandWithOutput(cmd)
  722. if err != nil {
  723. c.Fatal(err, out)
  724. }
  725. actualEnvLxc := strings.Split(strings.TrimSpace(out), "\n")
  726. actualEnv := []string{}
  727. for i := range actualEnvLxc {
  728. if actualEnvLxc[i] != "container=lxc" {
  729. actualEnv = append(actualEnv, actualEnvLxc[i])
  730. }
  731. }
  732. sort.Strings(actualEnv)
  733. goodEnv := []string{
  734. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  735. "HOME=/root",
  736. }
  737. sort.Strings(goodEnv)
  738. if len(goodEnv) != len(actualEnv) {
  739. c.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", "))
  740. }
  741. for i := range goodEnv {
  742. if actualEnv[i] != goodEnv[i] {
  743. c.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  744. }
  745. }
  746. }
  747. func (s *DockerSuite) TestRunEnvironmentOverride(c *check.C) {
  748. // Test to make sure that when we use -e on env vars that are
  749. // already in the env that we're overriding them
  750. cmd := exec.Command(dockerBinary, "run", "-e", "HOSTNAME", "-e", "HOME=/root2", "busybox", "env")
  751. cmd.Env = appendBaseEnv([]string{"HOSTNAME=bar"})
  752. out, _, err := runCommandWithOutput(cmd)
  753. if err != nil {
  754. c.Fatal(err, out)
  755. }
  756. actualEnvLxc := strings.Split(strings.TrimSpace(out), "\n")
  757. actualEnv := []string{}
  758. for i := range actualEnvLxc {
  759. if actualEnvLxc[i] != "container=lxc" {
  760. actualEnv = append(actualEnv, actualEnvLxc[i])
  761. }
  762. }
  763. sort.Strings(actualEnv)
  764. goodEnv := []string{
  765. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  766. "HOME=/root2",
  767. "HOSTNAME=bar",
  768. }
  769. sort.Strings(goodEnv)
  770. if len(goodEnv) != len(actualEnv) {
  771. c.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", "))
  772. }
  773. for i := range goodEnv {
  774. if actualEnv[i] != goodEnv[i] {
  775. c.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  776. }
  777. }
  778. }
  779. func (s *DockerSuite) TestRunContainerNetwork(c *check.C) {
  780. cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
  781. if _, err := runCommand(cmd); err != nil {
  782. c.Fatal(err)
  783. }
  784. }
  785. // Issue #4681
  786. func (s *DockerSuite) TestRunLoopbackWhenNetworkDisabled(c *check.C) {
  787. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
  788. if _, err := runCommand(cmd); err != nil {
  789. c.Fatal(err)
  790. }
  791. }
  792. func (s *DockerSuite) TestRunNetHostNotAllowedWithLinks(c *check.C) {
  793. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "linked", "busybox", "true"))
  794. if err != nil {
  795. c.Fatalf("Failed with errors: %s, %v", out, err)
  796. }
  797. cmd := exec.Command(dockerBinary, "run", "--net=host", "--link", "linked:linked", "busybox", "true")
  798. _, _, err = runCommandWithOutput(cmd)
  799. if err == nil {
  800. c.Fatal("Expected error")
  801. }
  802. }
  803. func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C) {
  804. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  805. out, _, err := runCommandWithOutput(cmd)
  806. if err != nil {
  807. c.Fatal(err, out)
  808. }
  809. var (
  810. count = 0
  811. parts = strings.Split(out, "\n")
  812. )
  813. for _, l := range parts {
  814. if l != "" {
  815. count++
  816. }
  817. }
  818. if count != 1 {
  819. c.Fatalf("Wrong interface count in container %d", count)
  820. }
  821. if !strings.HasPrefix(out, "1: lo") {
  822. c.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  823. }
  824. }
  825. // #7851 hostname outside container shows FQDN, inside only shortname
  826. // For testing purposes it is not required to set host's hostname directly
  827. // and use "--net=host" (as the original issue submitter did), as the same
  828. // codepath is executed with "docker run -h <hostname>". Both were manually
  829. // tested, but this testcase takes the simpler path of using "run -h .."
  830. func (s *DockerSuite) TestRunFullHostnameSet(c *check.C) {
  831. cmd := exec.Command(dockerBinary, "run", "-h", "foo.bar.baz", "busybox", "hostname")
  832. out, _, err := runCommandWithOutput(cmd)
  833. if err != nil {
  834. c.Fatal(err, out)
  835. }
  836. if actual := strings.Trim(out, "\r\n"); actual != "foo.bar.baz" {
  837. c.Fatalf("expected hostname 'foo.bar.baz', received %s", actual)
  838. }
  839. }
  840. func (s *DockerSuite) TestRunPrivilegedCanMknod(c *check.C) {
  841. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  842. out, _, err := runCommandWithOutput(cmd)
  843. if err != nil {
  844. c.Fatal(err)
  845. }
  846. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  847. c.Fatalf("expected output ok received %s", actual)
  848. }
  849. }
  850. func (s *DockerSuite) TestRunUnprivilegedCanMknod(c *check.C) {
  851. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  852. out, _, err := runCommandWithOutput(cmd)
  853. if err != nil {
  854. c.Fatal(err)
  855. }
  856. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  857. c.Fatalf("expected output ok received %s", actual)
  858. }
  859. }
  860. func (s *DockerSuite) TestRunCapDropInvalid(c *check.C) {
  861. cmd := exec.Command(dockerBinary, "run", "--cap-drop=CHPASS", "busybox", "ls")
  862. out, _, err := runCommandWithOutput(cmd)
  863. if err == nil {
  864. c.Fatal(err, out)
  865. }
  866. }
  867. func (s *DockerSuite) TestRunCapDropCannotMknod(c *check.C) {
  868. cmd := exec.Command(dockerBinary, "run", "--cap-drop=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  869. out, _, err := runCommandWithOutput(cmd)
  870. if err == nil {
  871. c.Fatal(err, out)
  872. }
  873. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  874. c.Fatalf("expected output not ok received %s", actual)
  875. }
  876. }
  877. func (s *DockerSuite) TestRunCapDropCannotMknodLowerCase(c *check.C) {
  878. cmd := exec.Command(dockerBinary, "run", "--cap-drop=mknod", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  879. out, _, err := runCommandWithOutput(cmd)
  880. if err == nil {
  881. c.Fatal(err, out)
  882. }
  883. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  884. c.Fatalf("expected output not ok received %s", actual)
  885. }
  886. }
  887. func (s *DockerSuite) TestRunCapDropALLCannotMknod(c *check.C) {
  888. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=SETGID", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  889. out, _, err := runCommandWithOutput(cmd)
  890. if err == nil {
  891. c.Fatal(err, out)
  892. }
  893. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  894. c.Fatalf("expected output not ok received %s", actual)
  895. }
  896. }
  897. func (s *DockerSuite) TestRunCapDropALLAddMknodCanMknod(c *check.C) {
  898. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=MKNOD", "--cap-add=SETGID", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  899. out, _, err := runCommandWithOutput(cmd)
  900. if err != nil {
  901. c.Fatal(err, out)
  902. }
  903. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  904. c.Fatalf("expected output ok received %s", actual)
  905. }
  906. }
  907. func (s *DockerSuite) TestRunCapAddInvalid(c *check.C) {
  908. cmd := exec.Command(dockerBinary, "run", "--cap-add=CHPASS", "busybox", "ls")
  909. out, _, err := runCommandWithOutput(cmd)
  910. if err == nil {
  911. c.Fatal(err, out)
  912. }
  913. }
  914. func (s *DockerSuite) TestRunCapAddCanDownInterface(c *check.C) {
  915. cmd := exec.Command(dockerBinary, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  916. out, _, err := runCommandWithOutput(cmd)
  917. if err != nil {
  918. c.Fatal(err, out)
  919. }
  920. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  921. c.Fatalf("expected output ok received %s", actual)
  922. }
  923. }
  924. func (s *DockerSuite) TestRunCapAddALLCanDownInterface(c *check.C) {
  925. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  926. out, _, err := runCommandWithOutput(cmd)
  927. if err != nil {
  928. c.Fatal(err, out)
  929. }
  930. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  931. c.Fatalf("expected output ok received %s", actual)
  932. }
  933. }
  934. func (s *DockerSuite) TestRunCapAddALLDropNetAdminCanDownInterface(c *check.C) {
  935. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "--cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  936. out, _, err := runCommandWithOutput(cmd)
  937. if err == nil {
  938. c.Fatal(err, out)
  939. }
  940. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  941. c.Fatalf("expected output not ok received %s", actual)
  942. }
  943. }
  944. func (s *DockerSuite) TestRunPrivilegedCanMount(c *check.C) {
  945. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  946. out, _, err := runCommandWithOutput(cmd)
  947. if err != nil {
  948. c.Fatal(err)
  949. }
  950. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  951. c.Fatalf("expected output ok received %s", actual)
  952. }
  953. }
  954. func (s *DockerSuite) TestRunUnprivilegedCannotMount(c *check.C) {
  955. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  956. out, _, err := runCommandWithOutput(cmd)
  957. if err == nil {
  958. c.Fatal(err, out)
  959. }
  960. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  961. c.Fatalf("expected output not ok received %s", actual)
  962. }
  963. }
  964. func (s *DockerSuite) TestRunSysNotWritableInNonPrivilegedContainers(c *check.C) {
  965. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
  966. if code, err := runCommand(cmd); err == nil || code == 0 {
  967. c.Fatal("sys should not be writable in a non privileged container")
  968. }
  969. }
  970. func (s *DockerSuite) TestRunSysWritableInPrivilegedContainers(c *check.C) {
  971. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
  972. if code, err := runCommand(cmd); err != nil || code != 0 {
  973. c.Fatalf("sys should be writable in privileged container")
  974. }
  975. }
  976. func (s *DockerSuite) TestRunProcNotWritableInNonPrivilegedContainers(c *check.C) {
  977. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
  978. if code, err := runCommand(cmd); err == nil || code == 0 {
  979. c.Fatal("proc should not be writable in a non privileged container")
  980. }
  981. }
  982. func (s *DockerSuite) TestRunProcWritableInPrivilegedContainers(c *check.C) {
  983. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
  984. if code, err := runCommand(cmd); err != nil || code != 0 {
  985. c.Fatalf("proc should be writable in privileged container")
  986. }
  987. }
  988. func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
  989. runCmd := exec.Command(dockerBinary, "run", "--cpu-period", "50000", "--name", "test", "busybox", "true")
  990. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  991. if err != nil {
  992. c.Fatalf("failed to run container: %v, output: %q", err, out)
  993. }
  994. out = strings.TrimSpace(out)
  995. if strings.Contains(out, "Your kernel does not support CPU cfs period") {
  996. c.Skip("Your kernel does not support CPU cfs period, skip this test")
  997. }
  998. out, err = inspectField("test", "HostConfig.CpuPeriod")
  999. c.Assert(err, check.IsNil)
  1000. if out != "50000" {
  1001. c.Errorf("setting the CPU CFS period failed")
  1002. }
  1003. }
  1004. func (s *DockerSuite) TestRunWithCpuset(c *check.C) {
  1005. cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
  1006. if code, err := runCommand(cmd); err != nil || code != 0 {
  1007. c.Fatalf("container should run successfully with cpuset of 0: %s", err)
  1008. }
  1009. }
  1010. func (s *DockerSuite) TestRunWithCpusetCpus(c *check.C) {
  1011. cmd := exec.Command(dockerBinary, "run", "--cpuset-cpus", "0", "busybox", "true")
  1012. if code, err := runCommand(cmd); err != nil || code != 0 {
  1013. c.Fatalf("container should run successfully with cpuset-cpus of 0: %s", err)
  1014. }
  1015. }
  1016. func (s *DockerSuite) TestRunWithCpusetMems(c *check.C) {
  1017. cmd := exec.Command(dockerBinary, "run", "--cpuset-mems", "0", "busybox", "true")
  1018. if code, err := runCommand(cmd); err != nil || code != 0 {
  1019. c.Fatalf("container should run successfully with cpuset-mems of 0: %s", err)
  1020. }
  1021. }
  1022. func (s *DockerSuite) TestRunWithBlkioWeight(c *check.C) {
  1023. cmd := exec.Command(dockerBinary, "run", "--blkio-weight", "300", "busybox", "true")
  1024. if code, err := runCommand(cmd); err != nil || code != 0 {
  1025. c.Fatalf("container should run successfully with blkio-weight of 300: %s", err)
  1026. }
  1027. }
  1028. func (s *DockerSuite) TestRunWithBlkioInvalidWeight(c *check.C) {
  1029. cmd := exec.Command(dockerBinary, "run", "--blkio-weight", "5", "busybox", "true")
  1030. if _, err := runCommand(cmd); err == nil {
  1031. c.Fatalf("run with invalid blkio-weight should failed")
  1032. }
  1033. }
  1034. func (s *DockerSuite) TestRunDeviceNumbers(c *check.C) {
  1035. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
  1036. out, _, err := runCommandWithOutput(cmd)
  1037. if err != nil {
  1038. c.Fatal(err, out)
  1039. }
  1040. deviceLineFields := strings.Fields(out)
  1041. deviceLineFields[6] = ""
  1042. deviceLineFields[7] = ""
  1043. deviceLineFields[8] = ""
  1044. expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"}
  1045. if !(reflect.DeepEqual(deviceLineFields, expected)) {
  1046. c.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
  1047. }
  1048. }
  1049. func (s *DockerSuite) TestRunThatCharacterDevicesActLikeCharacterDevices(c *check.C) {
  1050. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
  1051. out, _, err := runCommandWithOutput(cmd)
  1052. if err != nil {
  1053. c.Fatal(err, out)
  1054. }
  1055. if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
  1056. c.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
  1057. }
  1058. }
  1059. func (s *DockerSuite) TestRunUnprivilegedWithChroot(c *check.C) {
  1060. cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
  1061. if _, err := runCommand(cmd); err != nil {
  1062. c.Fatal(err)
  1063. }
  1064. }
  1065. func (s *DockerSuite) TestRunAddingOptionalDevices(c *check.C) {
  1066. cmd := exec.Command(dockerBinary, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo")
  1067. out, _, err := runCommandWithOutput(cmd)
  1068. if err != nil {
  1069. c.Fatal(err, out)
  1070. }
  1071. if actual := strings.Trim(out, "\r\n"); actual != "/dev/nulo" {
  1072. c.Fatalf("expected output /dev/nulo, received %s", actual)
  1073. }
  1074. }
  1075. func (s *DockerSuite) TestRunModeHostname(c *check.C) {
  1076. testRequires(c, SameHostDaemon)
  1077. cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
  1078. out, _, err := runCommandWithOutput(cmd)
  1079. if err != nil {
  1080. c.Fatal(err, out)
  1081. }
  1082. if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
  1083. c.Fatalf("expected 'testhostname', but says: %q", actual)
  1084. }
  1085. cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname")
  1086. out, _, err = runCommandWithOutput(cmd)
  1087. if err != nil {
  1088. c.Fatal(err, out)
  1089. }
  1090. hostname, err := os.Hostname()
  1091. if err != nil {
  1092. c.Fatal(err)
  1093. }
  1094. if actual := strings.Trim(out, "\r\n"); actual != hostname {
  1095. c.Fatalf("expected %q, but says: %q", hostname, actual)
  1096. }
  1097. }
  1098. func (s *DockerSuite) TestRunRootWorkdir(c *check.C) {
  1099. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--workdir", "/", "busybox", "pwd"))
  1100. if err != nil {
  1101. c.Fatalf("Failed with errors: %s, %v", out, err)
  1102. }
  1103. if out != "/\n" {
  1104. c.Fatalf("pwd returned %q (expected /\\n)", s)
  1105. }
  1106. }
  1107. func (s *DockerSuite) TestRunAllowBindMountingRoot(c *check.C) {
  1108. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-v", "/:/host", "busybox", "ls", "/host"))
  1109. if err != nil {
  1110. c.Fatalf("Failed with errors: %s, %v", out, err)
  1111. }
  1112. }
  1113. func (s *DockerSuite) TestRunDisallowBindMountingRootToRoot(c *check.C) {
  1114. cmd := exec.Command(dockerBinary, "run", "-v", "/:/", "busybox", "ls", "/host")
  1115. out, _, err := runCommandWithOutput(cmd)
  1116. if err == nil {
  1117. c.Fatal(out, err)
  1118. }
  1119. }
  1120. // Verify that a container gets default DNS when only localhost resolvers exist
  1121. func (s *DockerSuite) TestRunDnsDefaultOptions(c *check.C) {
  1122. testRequires(c, SameHostDaemon)
  1123. // preserve original resolv.conf for restoring after test
  1124. origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  1125. if os.IsNotExist(err) {
  1126. c.Fatalf("/etc/resolv.conf does not exist")
  1127. }
  1128. // defer restored original conf
  1129. defer func() {
  1130. if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
  1131. c.Fatal(err)
  1132. }
  1133. }()
  1134. // test 3 cases: standard IPv4 localhost, commented out localhost, and IPv6 localhost
  1135. // 2 are removed from the file at container start, and the 3rd (commented out) one is ignored by
  1136. // GetNameservers(), leading to a replacement of nameservers with the default set
  1137. tmpResolvConf := []byte("nameserver 127.0.0.1\n#nameserver 127.0.2.1\nnameserver ::1")
  1138. if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
  1139. c.Fatal(err)
  1140. }
  1141. cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
  1142. actual, _, err := runCommandWithOutput(cmd)
  1143. if err != nil {
  1144. c.Fatal(err, actual)
  1145. }
  1146. // check that the actual defaults are appended to the commented out
  1147. // localhost resolver (which should be preserved)
  1148. // NOTE: if we ever change the defaults from google dns, this will break
  1149. expected := "#nameserver 127.0.2.1\n\nnameserver 8.8.8.8\nnameserver 8.8.4.4"
  1150. if actual != expected {
  1151. c.Fatalf("expected resolv.conf be: %q, but was: %q", expected, actual)
  1152. }
  1153. }
  1154. func (s *DockerSuite) TestRunDnsOptions(c *check.C) {
  1155. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  1156. out, stderr, _, err := runCommandWithStdoutStderr(cmd)
  1157. if err != nil {
  1158. c.Fatal(err, out)
  1159. }
  1160. // The client will get a warning on stderr when setting DNS to a localhost address; verify this:
  1161. if !strings.Contains(stderr, "Localhost DNS setting") {
  1162. c.Fatalf("Expected warning on stderr about localhost resolver, but got %q", stderr)
  1163. }
  1164. actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1)
  1165. if actual != "nameserver 127.0.0.1 search mydomain" {
  1166. c.Fatalf("expected 'nameserver 127.0.0.1 search mydomain', but says: %q", actual)
  1167. }
  1168. cmd = exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=.", "busybox", "cat", "/etc/resolv.conf")
  1169. out, _, _, err = runCommandWithStdoutStderr(cmd)
  1170. if err != nil {
  1171. c.Fatal(err, out)
  1172. }
  1173. actual = strings.Replace(strings.Trim(strings.Trim(out, "\r\n"), " "), "\n", " ", -1)
  1174. if actual != "nameserver 127.0.0.1" {
  1175. c.Fatalf("expected 'nameserver 127.0.0.1', but says: %q", actual)
  1176. }
  1177. }
  1178. func (s *DockerSuite) TestRunDnsOptionsBasedOnHostResolvConf(c *check.C) {
  1179. testRequires(c, SameHostDaemon)
  1180. origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  1181. if os.IsNotExist(err) {
  1182. c.Fatalf("/etc/resolv.conf does not exist")
  1183. }
  1184. hostNamservers := resolvconf.GetNameservers(origResolvConf)
  1185. hostSearch := resolvconf.GetSearchDomains(origResolvConf)
  1186. var out string
  1187. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "busybox", "cat", "/etc/resolv.conf")
  1188. if out, _, _, err = runCommandWithStdoutStderr(cmd); err != nil {
  1189. c.Fatal(err, out)
  1190. }
  1191. if actualNameservers := resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "127.0.0.1" {
  1192. c.Fatalf("expected '127.0.0.1', but says: %q", string(actualNameservers[0]))
  1193. }
  1194. actualSearch := resolvconf.GetSearchDomains([]byte(out))
  1195. if len(actualSearch) != len(hostSearch) {
  1196. c.Fatalf("expected %q search domain(s), but it has: %q", len(hostSearch), len(actualSearch))
  1197. }
  1198. for i := range actualSearch {
  1199. if actualSearch[i] != hostSearch[i] {
  1200. c.Fatalf("expected %q domain, but says: %q", actualSearch[i], hostSearch[i])
  1201. }
  1202. }
  1203. cmd = exec.Command(dockerBinary, "run", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  1204. if out, _, err = runCommandWithOutput(cmd); err != nil {
  1205. c.Fatal(err, out)
  1206. }
  1207. actualNameservers := resolvconf.GetNameservers([]byte(out))
  1208. if len(actualNameservers) != len(hostNamservers) {
  1209. c.Fatalf("expected %q nameserver(s), but it has: %q", len(hostNamservers), len(actualNameservers))
  1210. }
  1211. for i := range actualNameservers {
  1212. if actualNameservers[i] != hostNamservers[i] {
  1213. c.Fatalf("expected %q nameserver, but says: %q", actualNameservers[i], hostNamservers[i])
  1214. }
  1215. }
  1216. if actualSearch = resolvconf.GetSearchDomains([]byte(out)); string(actualSearch[0]) != "mydomain" {
  1217. c.Fatalf("expected 'mydomain', but says: %q", string(actualSearch[0]))
  1218. }
  1219. // test with file
  1220. tmpResolvConf := []byte("search example.com\nnameserver 12.34.56.78\nnameserver 127.0.0.1")
  1221. if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
  1222. c.Fatal(err)
  1223. }
  1224. // put the old resolvconf back
  1225. defer func() {
  1226. if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
  1227. c.Fatal(err)
  1228. }
  1229. }()
  1230. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  1231. if os.IsNotExist(err) {
  1232. c.Fatalf("/etc/resolv.conf does not exist")
  1233. }
  1234. hostNamservers = resolvconf.GetNameservers(resolvConf)
  1235. hostSearch = resolvconf.GetSearchDomains(resolvConf)
  1236. cmd = exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
  1237. if out, _, err = runCommandWithOutput(cmd); err != nil {
  1238. c.Fatal(err, out)
  1239. }
  1240. if actualNameservers = resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "12.34.56.78" || len(actualNameservers) != 1 {
  1241. c.Fatalf("expected '12.34.56.78', but has: %v", actualNameservers)
  1242. }
  1243. actualSearch = resolvconf.GetSearchDomains([]byte(out))
  1244. if len(actualSearch) != len(hostSearch) {
  1245. c.Fatalf("expected %q search domain(s), but it has: %q", len(hostSearch), len(actualSearch))
  1246. }
  1247. for i := range actualSearch {
  1248. if actualSearch[i] != hostSearch[i] {
  1249. c.Fatalf("expected %q domain, but says: %q", actualSearch[i], hostSearch[i])
  1250. }
  1251. }
  1252. }
  1253. // Test if container resolv.conf gets updated the next time it restarts
  1254. // if host /etc/resolv.conf has changed. This only applies if the container
  1255. // uses the host's /etc/resolv.conf and does not have any dns options provided.
  1256. func (s *DockerSuite) TestRunResolvconfUpdate(c *check.C) {
  1257. testRequires(c, SameHostDaemon)
  1258. tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78")
  1259. tmpLocalhostResolvConf := []byte("nameserver 127.0.0.1")
  1260. //take a copy of resolv.conf for restoring after test completes
  1261. resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
  1262. if err != nil {
  1263. c.Fatal(err)
  1264. }
  1265. // This test case is meant to test monitoring resolv.conf when it is
  1266. // a regular file not a bind mounc. So we unmount resolv.conf and replace
  1267. // it with a file containing the original settings.
  1268. cmd := exec.Command("umount", "/etc/resolv.conf")
  1269. if _, err = runCommand(cmd); err != nil {
  1270. c.Fatal(err)
  1271. }
  1272. //cleanup
  1273. defer func() {
  1274. if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
  1275. c.Fatal(err)
  1276. }
  1277. }()
  1278. //1. test that a restarting container gets an updated resolv.conf
  1279. cmd = exec.Command(dockerBinary, "run", "--name='first'", "busybox", "true")
  1280. if _, err := runCommand(cmd); err != nil {
  1281. c.Fatal(err)
  1282. }
  1283. containerID1, err := getIDByName("first")
  1284. if err != nil {
  1285. c.Fatal(err)
  1286. }
  1287. // replace resolv.conf with our temporary copy
  1288. bytesResolvConf := []byte(tmpResolvConf)
  1289. if err := ioutil.WriteFile("/etc/resolv.conf", bytesResolvConf, 0644); err != nil {
  1290. c.Fatal(err)
  1291. }
  1292. // start the container again to pickup changes
  1293. cmd = exec.Command(dockerBinary, "start", "first")
  1294. if out, err := runCommand(cmd); err != nil {
  1295. c.Fatalf("Errored out %s, \nerror: %v", string(out), err)
  1296. }
  1297. // check for update in container
  1298. containerResolv, err := readContainerFile(containerID1, "resolv.conf")
  1299. if err != nil {
  1300. c.Fatal(err)
  1301. }
  1302. if !bytes.Equal(containerResolv, bytesResolvConf) {
  1303. c.Fatalf("Restarted container does not have updated resolv.conf; expected %q, got %q", tmpResolvConf, string(containerResolv))
  1304. }
  1305. /* //make a change to resolv.conf (in this case replacing our tmp copy with orig copy)
  1306. if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
  1307. c.Fatal(err)
  1308. } */
  1309. //2. test that a restarting container does not receive resolv.conf updates
  1310. // if it modified the container copy of the starting point resolv.conf
  1311. cmd = exec.Command(dockerBinary, "run", "--name='second'", "busybox", "sh", "-c", "echo 'search mylittlepony.com' >>/etc/resolv.conf")
  1312. if _, err = runCommand(cmd); err != nil {
  1313. c.Fatal(err)
  1314. }
  1315. containerID2, err := getIDByName("second")
  1316. if err != nil {
  1317. c.Fatal(err)
  1318. }
  1319. //make a change to resolv.conf (in this case replacing our tmp copy with orig copy)
  1320. if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
  1321. c.Fatal(err)
  1322. }
  1323. // start the container again
  1324. cmd = exec.Command(dockerBinary, "start", "second")
  1325. if out, err := runCommand(cmd); err != nil {
  1326. c.Fatalf("Errored out %s, \nerror: %v", string(out), err)
  1327. }
  1328. // check for update in container
  1329. containerResolv, err = readContainerFile(containerID2, "resolv.conf")
  1330. if err != nil {
  1331. c.Fatal(err)
  1332. }
  1333. if bytes.Equal(containerResolv, resolvConfSystem) {
  1334. c.Fatalf("Restarting a container after container updated resolv.conf should not pick up host changes; expected %q, got %q", string(containerResolv), string(resolvConfSystem))
  1335. }
  1336. //3. test that a running container's resolv.conf is not modified while running
  1337. cmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  1338. out, _, err := runCommandWithOutput(cmd)
  1339. if err != nil {
  1340. c.Fatal(err)
  1341. }
  1342. runningContainerID := strings.TrimSpace(out)
  1343. // replace resolv.conf
  1344. if err := ioutil.WriteFile("/etc/resolv.conf", bytesResolvConf, 0644); err != nil {
  1345. c.Fatal(err)
  1346. }
  1347. // check for update in container
  1348. containerResolv, err = readContainerFile(runningContainerID, "resolv.conf")
  1349. if err != nil {
  1350. c.Fatal(err)
  1351. }
  1352. if bytes.Equal(containerResolv, bytesResolvConf) {
  1353. c.Fatalf("Running container should not have updated resolv.conf; expected %q, got %q", string(resolvConfSystem), string(containerResolv))
  1354. }
  1355. //4. test that a running container's resolv.conf is updated upon restart
  1356. // (the above container is still running..)
  1357. cmd = exec.Command(dockerBinary, "restart", runningContainerID)
  1358. if _, err = runCommand(cmd); err != nil {
  1359. c.Fatal(err)
  1360. }
  1361. // check for update in container
  1362. containerResolv, err = readContainerFile(runningContainerID, "resolv.conf")
  1363. if err != nil {
  1364. c.Fatal(err)
  1365. }
  1366. if !bytes.Equal(containerResolv, bytesResolvConf) {
  1367. c.Fatalf("Restarted container should have updated resolv.conf; expected %q, got %q", string(bytesResolvConf), string(containerResolv))
  1368. }
  1369. //5. test that additions of a localhost resolver are cleaned from
  1370. // host resolv.conf before updating container's resolv.conf copies
  1371. // replace resolv.conf with a localhost-only nameserver copy
  1372. bytesResolvConf = []byte(tmpLocalhostResolvConf)
  1373. if err = ioutil.WriteFile("/etc/resolv.conf", bytesResolvConf, 0644); err != nil {
  1374. c.Fatal(err)
  1375. }
  1376. // start the container again to pickup changes
  1377. cmd = exec.Command(dockerBinary, "start", "first")
  1378. if out, err := runCommand(cmd); err != nil {
  1379. c.Fatalf("Errored out %s, \nerror: %v", string(out), err)
  1380. }
  1381. // our first exited container ID should have been updated, but with default DNS
  1382. // after the cleanup of resolv.conf found only a localhost nameserver:
  1383. containerResolv, err = readContainerFile(containerID1, "resolv.conf")
  1384. if err != nil {
  1385. c.Fatal(err)
  1386. }
  1387. expected := "\nnameserver 8.8.8.8\nnameserver 8.8.4.4"
  1388. if !bytes.Equal(containerResolv, []byte(expected)) {
  1389. c.Fatalf("Container does not have cleaned/replaced DNS in resolv.conf; expected %q, got %q", expected, string(containerResolv))
  1390. }
  1391. //6. Test that replacing (as opposed to modifying) resolv.conf triggers an update
  1392. // of containers' resolv.conf.
  1393. // Restore the original resolv.conf
  1394. if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
  1395. c.Fatal(err)
  1396. }
  1397. // Run the container so it picks up the old settings
  1398. cmd = exec.Command(dockerBinary, "run", "--name='third'", "busybox", "true")
  1399. if _, err := runCommand(cmd); err != nil {
  1400. c.Fatal(err)
  1401. }
  1402. containerID3, err := getIDByName("third")
  1403. if err != nil {
  1404. c.Fatal(err)
  1405. }
  1406. // Create a modified resolv.conf.aside and override resolv.conf with it
  1407. bytesResolvConf = []byte(tmpResolvConf)
  1408. if err := ioutil.WriteFile("/etc/resolv.conf.aside", bytesResolvConf, 0644); err != nil {
  1409. c.Fatal(err)
  1410. }
  1411. err = os.Rename("/etc/resolv.conf.aside", "/etc/resolv.conf")
  1412. if err != nil {
  1413. c.Fatal(err)
  1414. }
  1415. // start the container again to pickup changes
  1416. cmd = exec.Command(dockerBinary, "start", "third")
  1417. if out, err := runCommand(cmd); err != nil {
  1418. c.Fatalf("Errored out %s, \nerror: %v", string(out), err)
  1419. }
  1420. // check for update in container
  1421. containerResolv, err = readContainerFile(containerID3, "resolv.conf")
  1422. if err != nil {
  1423. c.Fatal(err)
  1424. }
  1425. if !bytes.Equal(containerResolv, bytesResolvConf) {
  1426. c.Fatalf("Stopped container does not have updated resolv.conf; expected\n%q\n got\n%q", tmpResolvConf, string(containerResolv))
  1427. }
  1428. //cleanup, restore original resolv.conf happens in defer func()
  1429. }
  1430. func (s *DockerSuite) TestRunAddHost(c *check.C) {
  1431. cmd := exec.Command(dockerBinary, "run", "--add-host=extra:86.75.30.9", "busybox", "grep", "extra", "/etc/hosts")
  1432. out, _, err := runCommandWithOutput(cmd)
  1433. if err != nil {
  1434. c.Fatal(err, out)
  1435. }
  1436. actual := strings.Trim(out, "\r\n")
  1437. if actual != "86.75.30.9\textra" {
  1438. c.Fatalf("expected '86.75.30.9\textra', but says: %q", actual)
  1439. }
  1440. }
  1441. // Regression test for #6983
  1442. func (s *DockerSuite) TestRunAttachStdErrOnlyTTYMode(c *check.C) {
  1443. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stderr", "busybox", "true")
  1444. exitCode, err := runCommand(cmd)
  1445. if err != nil {
  1446. c.Fatal(err)
  1447. } else if exitCode != 0 {
  1448. c.Fatalf("Container should have exited with error code 0")
  1449. }
  1450. }
  1451. // Regression test for #6983
  1452. func (s *DockerSuite) TestRunAttachStdOutOnlyTTYMode(c *check.C) {
  1453. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "busybox", "true")
  1454. exitCode, err := runCommand(cmd)
  1455. if err != nil {
  1456. c.Fatal(err)
  1457. } else if exitCode != 0 {
  1458. c.Fatalf("Container should have exited with error code 0")
  1459. }
  1460. }
  1461. // Regression test for #6983
  1462. func (s *DockerSuite) TestRunAttachStdOutAndErrTTYMode(c *check.C) {
  1463. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "-a", "stderr", "busybox", "true")
  1464. exitCode, err := runCommand(cmd)
  1465. if err != nil {
  1466. c.Fatal(err)
  1467. } else if exitCode != 0 {
  1468. c.Fatalf("Container should have exited with error code 0")
  1469. }
  1470. }
  1471. // Test for #10388 - this will run the same test as TestRunAttachStdOutAndErrTTYMode
  1472. // but using --attach instead of -a to make sure we read the flag correctly
  1473. func (s *DockerSuite) TestRunAttachWithDetach(c *check.C) {
  1474. cmd := exec.Command(dockerBinary, "run", "-d", "--attach", "stdout", "busybox", "true")
  1475. _, stderr, _, err := runCommandWithStdoutStderr(cmd)
  1476. if err == nil {
  1477. c.Fatal("Container should have exited with error code different than 0")
  1478. } else if !strings.Contains(stderr, "Conflicting options: -a and -d") {
  1479. c.Fatal("Should have been returned an error with conflicting options -a and -d")
  1480. }
  1481. }
  1482. func (s *DockerSuite) TestRunState(c *check.C) {
  1483. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  1484. out, _, err := runCommandWithOutput(cmd)
  1485. if err != nil {
  1486. c.Fatal(err, out)
  1487. }
  1488. id := strings.TrimSpace(out)
  1489. state, err := inspectField(id, "State.Running")
  1490. c.Assert(err, check.IsNil)
  1491. if state != "true" {
  1492. c.Fatal("Container state is 'not running'")
  1493. }
  1494. pid1, err := inspectField(id, "State.Pid")
  1495. c.Assert(err, check.IsNil)
  1496. if pid1 == "0" {
  1497. c.Fatal("Container state Pid 0")
  1498. }
  1499. cmd = exec.Command(dockerBinary, "stop", id)
  1500. out, _, err = runCommandWithOutput(cmd)
  1501. if err != nil {
  1502. c.Fatal(err, out)
  1503. }
  1504. state, err = inspectField(id, "State.Running")
  1505. c.Assert(err, check.IsNil)
  1506. if state != "false" {
  1507. c.Fatal("Container state is 'running'")
  1508. }
  1509. pid2, err := inspectField(id, "State.Pid")
  1510. c.Assert(err, check.IsNil)
  1511. if pid2 == pid1 {
  1512. c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1513. }
  1514. cmd = exec.Command(dockerBinary, "start", id)
  1515. out, _, err = runCommandWithOutput(cmd)
  1516. if err != nil {
  1517. c.Fatal(err, out)
  1518. }
  1519. state, err = inspectField(id, "State.Running")
  1520. c.Assert(err, check.IsNil)
  1521. if state != "true" {
  1522. c.Fatal("Container state is 'not running'")
  1523. }
  1524. pid3, err := inspectField(id, "State.Pid")
  1525. c.Assert(err, check.IsNil)
  1526. if pid3 == pid1 {
  1527. c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1528. }
  1529. }
  1530. // Test for #1737
  1531. func (s *DockerSuite) TestRunCopyVolumeUidGid(c *check.C) {
  1532. name := "testrunvolumesuidgid"
  1533. _, err := buildImage(name,
  1534. `FROM busybox
  1535. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  1536. RUN echo 'dockerio:x:1001:' >> /etc/group
  1537. RUN mkdir -p /hello && touch /hello/test && chown dockerio.dockerio /hello`,
  1538. true)
  1539. if err != nil {
  1540. c.Fatal(err)
  1541. }
  1542. // Test that the uid and gid is copied from the image to the volume
  1543. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "ls -l / | grep hello | awk '{print $3\":\"$4}'")
  1544. out, _, err := runCommandWithOutput(cmd)
  1545. if err != nil {
  1546. c.Fatal(err, out)
  1547. }
  1548. out = strings.TrimSpace(out)
  1549. if out != "dockerio:dockerio" {
  1550. c.Fatalf("Wrong /hello ownership: %s, expected dockerio:dockerio", out)
  1551. }
  1552. }
  1553. // Test for #1582
  1554. func (s *DockerSuite) TestRunCopyVolumeContent(c *check.C) {
  1555. name := "testruncopyvolumecontent"
  1556. _, err := buildImage(name,
  1557. `FROM busybox
  1558. RUN mkdir -p /hello/local && echo hello > /hello/local/world`,
  1559. true)
  1560. if err != nil {
  1561. c.Fatal(err)
  1562. }
  1563. // Test that the content is copied from the image to the volume
  1564. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "find", "/hello")
  1565. out, _, err := runCommandWithOutput(cmd)
  1566. if err != nil {
  1567. c.Fatal(err, out)
  1568. }
  1569. if !(strings.Contains(out, "/hello/local/world") && strings.Contains(out, "/hello/local")) {
  1570. c.Fatal("Container failed to transfer content to volume")
  1571. }
  1572. }
  1573. func (s *DockerSuite) TestRunCleanupCmdOnEntrypoint(c *check.C) {
  1574. name := "testrunmdcleanuponentrypoint"
  1575. if _, err := buildImage(name,
  1576. `FROM busybox
  1577. ENTRYPOINT ["echo"]
  1578. CMD ["testingpoint"]`,
  1579. true); err != nil {
  1580. c.Fatal(err)
  1581. }
  1582. runCmd := exec.Command(dockerBinary, "run", "--entrypoint", "whoami", name)
  1583. out, exit, err := runCommandWithOutput(runCmd)
  1584. if err != nil {
  1585. c.Fatalf("Error: %v, out: %q", err, out)
  1586. }
  1587. if exit != 0 {
  1588. c.Fatalf("expected exit code 0 received %d, out: %q", exit, out)
  1589. }
  1590. out = strings.TrimSpace(out)
  1591. if out != "root" {
  1592. c.Fatalf("Expected output root, got %q", out)
  1593. }
  1594. }
  1595. // TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected
  1596. func (s *DockerSuite) TestRunWorkdirExistsAndIsFile(c *check.C) {
  1597. runCmd := exec.Command(dockerBinary, "run", "-w", "/bin/cat", "busybox")
  1598. out, exit, err := runCommandWithOutput(runCmd)
  1599. if !(err != nil && exit == 1 && strings.Contains(out, "Cannot mkdir: /bin/cat is not a directory")) {
  1600. c.Fatalf("Docker must complains about making dir, but we got out: %s, exit: %d, err: %s", out, exit, err)
  1601. }
  1602. }
  1603. func (s *DockerSuite) TestRunExitOnStdinClose(c *check.C) {
  1604. name := "testrunexitonstdinclose"
  1605. runCmd := exec.Command(dockerBinary, "run", "--name", name, "-i", "busybox", "/bin/cat")
  1606. stdin, err := runCmd.StdinPipe()
  1607. if err != nil {
  1608. c.Fatal(err)
  1609. }
  1610. stdout, err := runCmd.StdoutPipe()
  1611. if err != nil {
  1612. c.Fatal(err)
  1613. }
  1614. if err := runCmd.Start(); err != nil {
  1615. c.Fatal(err)
  1616. }
  1617. if _, err := stdin.Write([]byte("hello\n")); err != nil {
  1618. c.Fatal(err)
  1619. }
  1620. r := bufio.NewReader(stdout)
  1621. line, err := r.ReadString('\n')
  1622. if err != nil {
  1623. c.Fatal(err)
  1624. }
  1625. line = strings.TrimSpace(line)
  1626. if line != "hello" {
  1627. c.Fatalf("Output should be 'hello', got '%q'", line)
  1628. }
  1629. if err := stdin.Close(); err != nil {
  1630. c.Fatal(err)
  1631. }
  1632. finish := make(chan error)
  1633. go func() {
  1634. finish <- runCmd.Wait()
  1635. close(finish)
  1636. }()
  1637. select {
  1638. case err := <-finish:
  1639. c.Assert(err, check.IsNil)
  1640. case <-time.After(1 * time.Second):
  1641. c.Fatal("docker run failed to exit on stdin close")
  1642. }
  1643. state, err := inspectField(name, "State.Running")
  1644. c.Assert(err, check.IsNil)
  1645. if state != "false" {
  1646. c.Fatal("Container must be stopped after stdin closing")
  1647. }
  1648. }
  1649. // Test for #2267
  1650. func (s *DockerSuite) TestRunWriteHostsFileAndNotCommit(c *check.C) {
  1651. name := "writehosts"
  1652. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hosts && cat /etc/hosts")
  1653. out, _, err := runCommandWithOutput(cmd)
  1654. if err != nil {
  1655. c.Fatal(err, out)
  1656. }
  1657. if !strings.Contains(out, "test2267") {
  1658. c.Fatal("/etc/hosts should contain 'test2267'")
  1659. }
  1660. cmd = exec.Command(dockerBinary, "diff", name)
  1661. if err != nil {
  1662. c.Fatal(err, out)
  1663. }
  1664. out, _, err = runCommandWithOutput(cmd)
  1665. if err != nil {
  1666. c.Fatal(err, out)
  1667. }
  1668. if len(strings.Trim(out, "\r\n")) != 0 && !eqToBaseDiff(out, c) {
  1669. c.Fatal("diff should be empty")
  1670. }
  1671. }
  1672. func eqToBaseDiff(out string, c *check.C) bool {
  1673. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
  1674. out1, _, err := runCommandWithOutput(cmd)
  1675. cID := strings.TrimSpace(out1)
  1676. cmd = exec.Command(dockerBinary, "diff", cID)
  1677. baseDiff, _, err := runCommandWithOutput(cmd)
  1678. if err != nil {
  1679. c.Fatal(err, baseDiff)
  1680. }
  1681. baseArr := strings.Split(baseDiff, "\n")
  1682. sort.Strings(baseArr)
  1683. outArr := strings.Split(out, "\n")
  1684. sort.Strings(outArr)
  1685. return sliceEq(baseArr, outArr)
  1686. }
  1687. func sliceEq(a, b []string) bool {
  1688. if len(a) != len(b) {
  1689. return false
  1690. }
  1691. for i := range a {
  1692. if a[i] != b[i] {
  1693. return false
  1694. }
  1695. }
  1696. return true
  1697. }
  1698. // Test for #2267
  1699. func (s *DockerSuite) TestRunWriteHostnameFileAndNotCommit(c *check.C) {
  1700. name := "writehostname"
  1701. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hostname && cat /etc/hostname")
  1702. out, _, err := runCommandWithOutput(cmd)
  1703. if err != nil {
  1704. c.Fatal(err, out)
  1705. }
  1706. if !strings.Contains(out, "test2267") {
  1707. c.Fatal("/etc/hostname should contain 'test2267'")
  1708. }
  1709. cmd = exec.Command(dockerBinary, "diff", name)
  1710. if err != nil {
  1711. c.Fatal(err, out)
  1712. }
  1713. out, _, err = runCommandWithOutput(cmd)
  1714. if err != nil {
  1715. c.Fatal(err, out)
  1716. }
  1717. if len(strings.Trim(out, "\r\n")) != 0 && !eqToBaseDiff(out, c) {
  1718. c.Fatal("diff should be empty")
  1719. }
  1720. }
  1721. // Test for #2267
  1722. func (s *DockerSuite) TestRunWriteResolvFileAndNotCommit(c *check.C) {
  1723. name := "writeresolv"
  1724. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/resolv.conf && cat /etc/resolv.conf")
  1725. out, _, err := runCommandWithOutput(cmd)
  1726. if err != nil {
  1727. c.Fatal(err, out)
  1728. }
  1729. if !strings.Contains(out, "test2267") {
  1730. c.Fatal("/etc/resolv.conf should contain 'test2267'")
  1731. }
  1732. cmd = exec.Command(dockerBinary, "diff", name)
  1733. if err != nil {
  1734. c.Fatal(err, out)
  1735. }
  1736. out, _, err = runCommandWithOutput(cmd)
  1737. if err != nil {
  1738. c.Fatal(err, out)
  1739. }
  1740. if len(strings.Trim(out, "\r\n")) != 0 && !eqToBaseDiff(out, c) {
  1741. c.Fatal("diff should be empty")
  1742. }
  1743. }
  1744. func (s *DockerSuite) TestRunWithBadDevice(c *check.C) {
  1745. name := "baddevice"
  1746. cmd := exec.Command(dockerBinary, "run", "--name", name, "--device", "/etc", "busybox", "true")
  1747. out, _, err := runCommandWithOutput(cmd)
  1748. if err == nil {
  1749. c.Fatal("Run should fail with bad device")
  1750. }
  1751. expected := `"/etc": not a device node`
  1752. if !strings.Contains(out, expected) {
  1753. c.Fatalf("Output should contain %q, actual out: %q", expected, out)
  1754. }
  1755. }
  1756. func (s *DockerSuite) TestRunEntrypoint(c *check.C) {
  1757. name := "entrypoint"
  1758. cmd := exec.Command(dockerBinary, "run", "--name", name, "--entrypoint", "/bin/echo", "busybox", "-n", "foobar")
  1759. out, _, err := runCommandWithOutput(cmd)
  1760. if err != nil {
  1761. c.Fatal(err, out)
  1762. }
  1763. expected := "foobar"
  1764. if out != expected {
  1765. c.Fatalf("Output should be %q, actual out: %q", expected, out)
  1766. }
  1767. }
  1768. func (s *DockerSuite) TestRunBindMounts(c *check.C) {
  1769. testRequires(c, SameHostDaemon)
  1770. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1771. if err != nil {
  1772. c.Fatal(err)
  1773. }
  1774. defer os.RemoveAll(tmpDir)
  1775. writeFile(path.Join(tmpDir, "touch-me"), "", c)
  1776. // Test reading from a read-only bind mount
  1777. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox", "ls", "/tmp")
  1778. out, _, err := runCommandWithOutput(cmd)
  1779. if err != nil {
  1780. c.Fatal(err, out)
  1781. }
  1782. if !strings.Contains(out, "touch-me") {
  1783. c.Fatal("Container failed to read from bind mount")
  1784. }
  1785. // test writing to bind mount
  1786. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "busybox", "touch", "/tmp/holla")
  1787. out, _, err = runCommandWithOutput(cmd)
  1788. if err != nil {
  1789. c.Fatal(err, out)
  1790. }
  1791. readFile(path.Join(tmpDir, "holla"), c) // Will fail if the file doesn't exist
  1792. // test mounting to an illegal destination directory
  1793. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:.", tmpDir), "busybox", "ls", ".")
  1794. _, err = runCommand(cmd)
  1795. if err == nil {
  1796. c.Fatal("Container bind mounted illegal directory")
  1797. }
  1798. // test mount a file
  1799. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "busybox", "sh", "-c", "echo -n 'yotta' > /tmp/holla")
  1800. _, err = runCommand(cmd)
  1801. if err != nil {
  1802. c.Fatal(err, out)
  1803. }
  1804. content := readFile(path.Join(tmpDir, "holla"), c) // Will fail if the file doesn't exist
  1805. expected := "yotta"
  1806. if content != expected {
  1807. c.Fatalf("Output should be %q, actual out: %q", expected, content)
  1808. }
  1809. }
  1810. // Ensure that CIDFile gets deleted if it's empty
  1811. // Perform this test by making `docker run` fail
  1812. func (s *DockerSuite) TestRunCidFileCleanupIfEmpty(c *check.C) {
  1813. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  1814. if err != nil {
  1815. c.Fatal(err)
  1816. }
  1817. defer os.RemoveAll(tmpDir)
  1818. tmpCidFile := path.Join(tmpDir, "cid")
  1819. cmd := exec.Command(dockerBinary, "run", "--cidfile", tmpCidFile, "emptyfs")
  1820. out, _, err := runCommandWithOutput(cmd)
  1821. if err == nil {
  1822. c.Fatalf("Run without command must fail. out=%s", out)
  1823. } else if !strings.Contains(out, "No command specified") {
  1824. c.Fatalf("Run without command failed with wrong output. out=%s\nerr=%v", out, err)
  1825. }
  1826. if _, err := os.Stat(tmpCidFile); err == nil {
  1827. c.Fatalf("empty CIDFile %q should've been deleted", tmpCidFile)
  1828. }
  1829. }
  1830. // #2098 - Docker cidFiles only contain short version of the containerId
  1831. //sudo docker run --cidfile /tmp/docker_tesc.cid ubuntu echo "test"
  1832. // TestRunCidFile tests that run --cidfile returns the longid
  1833. func (s *DockerSuite) TestRunCidFileCheckIDLength(c *check.C) {
  1834. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  1835. if err != nil {
  1836. c.Fatal(err)
  1837. }
  1838. tmpCidFile := path.Join(tmpDir, "cid")
  1839. defer os.RemoveAll(tmpDir)
  1840. cmd := exec.Command(dockerBinary, "run", "-d", "--cidfile", tmpCidFile, "busybox", "true")
  1841. out, _, err := runCommandWithOutput(cmd)
  1842. if err != nil {
  1843. c.Fatal(err)
  1844. }
  1845. id := strings.TrimSpace(out)
  1846. buffer, err := ioutil.ReadFile(tmpCidFile)
  1847. if err != nil {
  1848. c.Fatal(err)
  1849. }
  1850. cid := string(buffer)
  1851. if len(cid) != 64 {
  1852. c.Fatalf("--cidfile should be a long id, not %q", id)
  1853. }
  1854. if cid != id {
  1855. c.Fatalf("cid must be equal to %s, got %s", id, cid)
  1856. }
  1857. }
  1858. func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) {
  1859. cmd := exec.Command(dockerBinary, "run", "-d", "--net=none", "busybox", "top")
  1860. out, _, err := runCommandWithOutput(cmd)
  1861. if err != nil {
  1862. c.Fatal(err)
  1863. }
  1864. id := strings.TrimSpace(out)
  1865. res, err := inspectField(id, "NetworkSettings.IPAddress")
  1866. c.Assert(err, check.IsNil)
  1867. if res != "" {
  1868. c.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
  1869. }
  1870. }
  1871. func (s *DockerSuite) TestRunSetMacAddress(c *check.C) {
  1872. mac := "12:34:56:78:9a:bc"
  1873. cmd := exec.Command(dockerBinary, "run", "-i", "--rm", fmt.Sprintf("--mac-address=%s", mac), "busybox", "/bin/sh", "-c", "ip link show eth0 | tail -1 | awk '{print $2}'")
  1874. out, ec, err := runCommandWithOutput(cmd)
  1875. if err != nil {
  1876. c.Fatalf("exec failed:\nexit code=%v\noutput=%s", ec, out)
  1877. }
  1878. actualMac := strings.TrimSpace(out)
  1879. if actualMac != mac {
  1880. c.Fatalf("Set MAC address with --mac-address failed. The container has an incorrect MAC address: %q, expected: %q", actualMac, mac)
  1881. }
  1882. }
  1883. func (s *DockerSuite) TestRunInspectMacAddress(c *check.C) {
  1884. mac := "12:34:56:78:9a:bc"
  1885. cmd := exec.Command(dockerBinary, "run", "-d", "--mac-address="+mac, "busybox", "top")
  1886. out, _, err := runCommandWithOutput(cmd)
  1887. if err != nil {
  1888. c.Fatal(err)
  1889. }
  1890. id := strings.TrimSpace(out)
  1891. inspectedMac, err := inspectField(id, "NetworkSettings.MacAddress")
  1892. c.Assert(err, check.IsNil)
  1893. if inspectedMac != mac {
  1894. c.Fatalf("docker inspect outputs wrong MAC address: %q, should be: %q", inspectedMac, mac)
  1895. }
  1896. }
  1897. // test docker run use a invalid mac address
  1898. func (s *DockerSuite) TestRunWithInvalidMacAddress(c *check.C) {
  1899. runCmd := exec.Command(dockerBinary, "run", "--mac-address", "92:d0:c6:0a:29", "busybox")
  1900. out, _, err := runCommandWithOutput(runCmd)
  1901. //use a invalid mac address should with a error out
  1902. if err == nil || !strings.Contains(out, "is not a valid mac address") {
  1903. c.Fatalf("run with an invalid --mac-address should with error out")
  1904. }
  1905. }
  1906. func (s *DockerSuite) TestRunDeallocatePortOnMissingIptablesRule(c *check.C) {
  1907. testRequires(c, SameHostDaemon)
  1908. cmd := exec.Command(dockerBinary, "run", "-d", "-p", "23:23", "busybox", "top")
  1909. out, _, err := runCommandWithOutput(cmd)
  1910. if err != nil {
  1911. c.Fatal(err)
  1912. }
  1913. id := strings.TrimSpace(out)
  1914. ip, err := inspectField(id, "NetworkSettings.IPAddress")
  1915. c.Assert(err, check.IsNil)
  1916. iptCmd := exec.Command("iptables", "-D", "DOCKER", "-d", fmt.Sprintf("%s/32", ip),
  1917. "!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT")
  1918. out, _, err = runCommandWithOutput(iptCmd)
  1919. if err != nil {
  1920. c.Fatal(err, out)
  1921. }
  1922. if err := deleteContainer(id); err != nil {
  1923. c.Fatal(err)
  1924. }
  1925. cmd = exec.Command(dockerBinary, "run", "-d", "-p", "23:23", "busybox", "top")
  1926. out, _, err = runCommandWithOutput(cmd)
  1927. if err != nil {
  1928. c.Fatal(err, out)
  1929. }
  1930. }
  1931. func (s *DockerSuite) TestRunPortInUse(c *check.C) {
  1932. testRequires(c, SameHostDaemon)
  1933. port := "1234"
  1934. cmd := exec.Command(dockerBinary, "run", "-d", "-p", port+":80", "busybox", "top")
  1935. out, _, err := runCommandWithOutput(cmd)
  1936. if err != nil {
  1937. c.Fatalf("Fail to run listening container")
  1938. }
  1939. cmd = exec.Command(dockerBinary, "run", "-d", "-p", port+":80", "busybox", "top")
  1940. out, _, err = runCommandWithOutput(cmd)
  1941. if err == nil {
  1942. c.Fatalf("Binding on used port must fail")
  1943. }
  1944. if !strings.Contains(out, "port is already allocated") {
  1945. c.Fatalf("Out must be about \"port is already allocated\", got %s", out)
  1946. }
  1947. }
  1948. // https://github.com/docker/docker/issues/12148
  1949. func (s *DockerSuite) TestRunAllocatePortInReservedRange(c *check.C) {
  1950. // allocate a dynamic port to get the most recent
  1951. cmd := exec.Command(dockerBinary, "run", "-d", "-P", "-p", "80", "busybox", "top")
  1952. out, _, err := runCommandWithOutput(cmd)
  1953. if err != nil {
  1954. c.Fatalf("Failed to run, output: %s, error: %s", out, err)
  1955. }
  1956. id := strings.TrimSpace(out)
  1957. cmd = exec.Command(dockerBinary, "port", id, "80")
  1958. out, _, err = runCommandWithOutput(cmd)
  1959. if err != nil {
  1960. c.Fatalf("Failed to get port, output: %s, error: %s", out, err)
  1961. }
  1962. strPort := strings.Split(strings.TrimSpace(out), ":")[1]
  1963. port, err := strconv.ParseInt(strPort, 10, 64)
  1964. if err != nil {
  1965. c.Fatalf("invalid port, got: %s, error: %s", strPort, err)
  1966. }
  1967. // allocate a static port and a dynamic port together, with static port
  1968. // takes the next recent port in dynamic port range.
  1969. cmd = exec.Command(dockerBinary, "run", "-d", "-P",
  1970. "-p", "80",
  1971. "-p", fmt.Sprintf("%d:8080", port+1),
  1972. "busybox", "top")
  1973. out, _, err = runCommandWithOutput(cmd)
  1974. if err != nil {
  1975. c.Fatalf("Failed to run, output: %s, error: %s", out, err)
  1976. }
  1977. }
  1978. // Regression test for #7792
  1979. func (s *DockerSuite) TestRunMountOrdering(c *check.C) {
  1980. testRequires(c, SameHostDaemon)
  1981. tmpDir, err := ioutil.TempDir("", "docker_nested_mount_test")
  1982. if err != nil {
  1983. c.Fatal(err)
  1984. }
  1985. defer os.RemoveAll(tmpDir)
  1986. tmpDir2, err := ioutil.TempDir("", "docker_nested_mount_test2")
  1987. if err != nil {
  1988. c.Fatal(err)
  1989. }
  1990. defer os.RemoveAll(tmpDir2)
  1991. // Create a temporary tmpfs mounc.
  1992. fooDir := filepath.Join(tmpDir, "foo")
  1993. if err := os.MkdirAll(filepath.Join(tmpDir, "foo"), 0755); err != nil {
  1994. c.Fatalf("failed to mkdir at %s - %s", fooDir, err)
  1995. }
  1996. if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", fooDir), []byte{}, 0644); err != nil {
  1997. c.Fatal(err)
  1998. }
  1999. if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir), []byte{}, 0644); err != nil {
  2000. c.Fatal(err)
  2001. }
  2002. if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir2), []byte{}, 0644); err != nil {
  2003. c.Fatal(err)
  2004. }
  2005. 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")
  2006. out, _, err := runCommandWithOutput(cmd)
  2007. if err != nil {
  2008. c.Fatal(out, err)
  2009. }
  2010. }
  2011. // Regression test for https://github.com/docker/docker/issues/8259
  2012. func (s *DockerSuite) TestRunReuseBindVolumeThatIsSymlink(c *check.C) {
  2013. testRequires(c, SameHostDaemon)
  2014. tmpDir, err := ioutil.TempDir(os.TempDir(), "testlink")
  2015. if err != nil {
  2016. c.Fatal(err)
  2017. }
  2018. defer os.RemoveAll(tmpDir)
  2019. linkPath := os.TempDir() + "/testlink2"
  2020. if err := os.Symlink(tmpDir, linkPath); err != nil {
  2021. c.Fatal(err)
  2022. }
  2023. defer os.RemoveAll(linkPath)
  2024. // Create first container
  2025. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp/test", linkPath), "busybox", "ls", "-lh", "/tmp/test")
  2026. if _, err := runCommand(cmd); err != nil {
  2027. c.Fatal(err)
  2028. }
  2029. // Create second container with same symlinked path
  2030. // This will fail if the referenced issue is hit with a "Volume exists" error
  2031. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp/test", linkPath), "busybox", "ls", "-lh", "/tmp/test")
  2032. if out, _, err := runCommandWithOutput(cmd); err != nil {
  2033. c.Fatal(err, out)
  2034. }
  2035. }
  2036. //GH#10604: Test an "/etc" volume doesn't overlay special bind mounts in container
  2037. func (s *DockerSuite) TestRunCreateVolumeEtc(c *check.C) {
  2038. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "-v", "/etc", "busybox", "cat", "/etc/resolv.conf")
  2039. out, _, err := runCommandWithOutput(cmd)
  2040. if err != nil {
  2041. c.Fatalf("failed to run container: %v, output: %q", err, out)
  2042. }
  2043. if !strings.Contains(out, "nameserver 127.0.0.1") {
  2044. c.Fatal("/etc volume mount hides /etc/resolv.conf")
  2045. }
  2046. cmd = exec.Command(dockerBinary, "run", "-h=test123", "-v", "/etc", "busybox", "cat", "/etc/hostname")
  2047. out, _, err = runCommandWithOutput(cmd)
  2048. if err != nil {
  2049. c.Fatalf("failed to run container: %v, output: %q", err, out)
  2050. }
  2051. if !strings.Contains(out, "test123") {
  2052. c.Fatal("/etc volume mount hides /etc/hostname")
  2053. }
  2054. cmd = exec.Command(dockerBinary, "run", "--add-host=test:192.168.0.1", "-v", "/etc", "busybox", "cat", "/etc/hosts")
  2055. out, _, err = runCommandWithOutput(cmd)
  2056. if err != nil {
  2057. c.Fatalf("failed to run container: %v, output: %q", err, out)
  2058. }
  2059. out = strings.Replace(out, "\n", " ", -1)
  2060. if !strings.Contains(out, "192.168.0.1\ttest") || !strings.Contains(out, "127.0.0.1\tlocalhost") {
  2061. c.Fatal("/etc volume mount hides /etc/hosts")
  2062. }
  2063. }
  2064. func (s *DockerSuite) TestVolumesNoCopyData(c *check.C) {
  2065. if _, err := buildImage("dataimage",
  2066. `FROM busybox
  2067. RUN mkdir -p /foo
  2068. RUN touch /foo/bar`,
  2069. true); err != nil {
  2070. c.Fatal(err)
  2071. }
  2072. cmd := exec.Command(dockerBinary, "run", "--name", "test", "-v", "/foo", "busybox")
  2073. if _, err := runCommand(cmd); err != nil {
  2074. c.Fatal(err)
  2075. }
  2076. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test", "dataimage", "ls", "-lh", "/foo/bar")
  2077. if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "No such file or directory") {
  2078. c.Fatalf("Data was copied on volumes-from but shouldn't be:\n%q", out)
  2079. }
  2080. tmpDir := randomUnixTmpDirPath("docker_test_bind_mount_copy_data")
  2081. cmd = exec.Command(dockerBinary, "run", "-v", tmpDir+":/foo", "dataimage", "ls", "-lh", "/foo/bar")
  2082. if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "No such file or directory") {
  2083. c.Fatalf("Data was copied on bind-mount but shouldn't be:\n%q", out)
  2084. }
  2085. }
  2086. func (s *DockerSuite) TestRunVolumesNotRecreatedOnStart(c *check.C) {
  2087. testRequires(c, SameHostDaemon)
  2088. // Clear out any remnants from other tests
  2089. info, err := ioutil.ReadDir(volumesConfigPath)
  2090. if err != nil {
  2091. c.Fatal(err)
  2092. }
  2093. if len(info) > 0 {
  2094. for _, f := range info {
  2095. if err := os.RemoveAll(volumesConfigPath + "/" + f.Name()); err != nil {
  2096. c.Fatal(err)
  2097. }
  2098. }
  2099. }
  2100. cmd := exec.Command(dockerBinary, "run", "-v", "/foo", "--name", "lone_starr", "busybox")
  2101. if _, err := runCommand(cmd); err != nil {
  2102. c.Fatal(err)
  2103. }
  2104. cmd = exec.Command(dockerBinary, "start", "lone_starr")
  2105. if _, err := runCommand(cmd); err != nil {
  2106. c.Fatal(err)
  2107. }
  2108. info, err = ioutil.ReadDir(volumesConfigPath)
  2109. if err != nil {
  2110. c.Fatal(err)
  2111. }
  2112. if len(info) != 1 {
  2113. c.Fatalf("Expected only 1 volume have %v", len(info))
  2114. }
  2115. }
  2116. func (s *DockerSuite) TestRunNoOutputFromPullInStdout(c *check.C) {
  2117. // just run with unknown image
  2118. cmd := exec.Command(dockerBinary, "run", "asdfsg")
  2119. stdout := bytes.NewBuffer(nil)
  2120. cmd.Stdout = stdout
  2121. if err := cmd.Run(); err == nil {
  2122. c.Fatal("Run with unknown image should fail")
  2123. }
  2124. if stdout.Len() != 0 {
  2125. c.Fatalf("Stdout contains output from pull: %s", stdout)
  2126. }
  2127. }
  2128. func (s *DockerSuite) TestRunVolumesCleanPaths(c *check.C) {
  2129. if _, err := buildImage("run_volumes_clean_paths",
  2130. `FROM busybox
  2131. VOLUME /foo/`,
  2132. true); err != nil {
  2133. c.Fatal(err)
  2134. }
  2135. cmd := exec.Command(dockerBinary, "run", "-v", "/foo", "-v", "/bar/", "--name", "dark_helmet", "run_volumes_clean_paths")
  2136. if out, _, err := runCommandWithOutput(cmd); err != nil {
  2137. c.Fatal(err, out)
  2138. }
  2139. out, err := inspectFieldMap("dark_helmet", "Volumes", "/foo/")
  2140. c.Assert(err, check.IsNil)
  2141. if out != "" {
  2142. c.Fatalf("Found unexpected volume entry for '/foo/' in volumes\n%q", out)
  2143. }
  2144. out, err = inspectFieldMap("dark_helmet", "Volumes", "/foo")
  2145. c.Assert(err, check.IsNil)
  2146. if !strings.Contains(out, volumesStoragePath) {
  2147. c.Fatalf("Volume was not defined for /foo\n%q", out)
  2148. }
  2149. out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar/")
  2150. c.Assert(err, check.IsNil)
  2151. if out != "" {
  2152. c.Fatalf("Found unexpected volume entry for '/bar/' in volumes\n%q", out)
  2153. }
  2154. out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar")
  2155. c.Assert(err, check.IsNil)
  2156. if !strings.Contains(out, volumesStoragePath) {
  2157. c.Fatalf("Volume was not defined for /bar\n%q", out)
  2158. }
  2159. }
  2160. // Regression test for #3631
  2161. func (s *DockerSuite) TestRunSlowStdoutConsumer(c *check.C) {
  2162. cont := exec.Command(dockerBinary, "run", "--rm", "busybox", "/bin/sh", "-c", "dd if=/dev/zero of=/dev/stdout bs=1024 count=2000 | catv")
  2163. stdout, err := cont.StdoutPipe()
  2164. if err != nil {
  2165. c.Fatal(err)
  2166. }
  2167. if err := cont.Start(); err != nil {
  2168. c.Fatal(err)
  2169. }
  2170. n, err := consumeWithSpeed(stdout, 10000, 5*time.Millisecond, nil)
  2171. if err != nil {
  2172. c.Fatal(err)
  2173. }
  2174. expected := 2 * 1024 * 2000
  2175. if n != expected {
  2176. c.Fatalf("Expected %d, got %d", expected, n)
  2177. }
  2178. }
  2179. func (s *DockerSuite) TestRunAllowPortRangeThroughExpose(c *check.C) {
  2180. cmd := exec.Command(dockerBinary, "run", "-d", "--expose", "3000-3003", "-P", "busybox", "top")
  2181. out, _, err := runCommandWithOutput(cmd)
  2182. if err != nil {
  2183. c.Fatal(err)
  2184. }
  2185. id := strings.TrimSpace(out)
  2186. portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports")
  2187. c.Assert(err, check.IsNil)
  2188. var ports nat.PortMap
  2189. if err = unmarshalJSON([]byte(portstr), &ports); err != nil {
  2190. c.Fatal(err)
  2191. }
  2192. for port, binding := range ports {
  2193. portnum, _ := strconv.Atoi(strings.Split(string(port), "/")[0])
  2194. if portnum < 3000 || portnum > 3003 {
  2195. c.Fatalf("Port %d is out of range ", portnum)
  2196. }
  2197. if binding == nil || len(binding) != 1 || len(binding[0].HostPort) == 0 {
  2198. c.Fatalf("Port is not mapped for the port %d", port)
  2199. }
  2200. }
  2201. if err := deleteContainer(id); err != nil {
  2202. c.Fatal(err)
  2203. }
  2204. }
  2205. // test docker run expose a invalid port
  2206. func (s *DockerSuite) TestRunExposePort(c *check.C) {
  2207. runCmd := exec.Command(dockerBinary, "run", "--expose", "80000", "busybox")
  2208. out, _, err := runCommandWithOutput(runCmd)
  2209. //expose a invalid port should with a error out
  2210. if err == nil || !strings.Contains(out, "Invalid range format for --expose") {
  2211. c.Fatalf("run --expose a invalid port should with error out")
  2212. }
  2213. }
  2214. func (s *DockerSuite) TestRunUnknownCommand(c *check.C) {
  2215. testRequires(c, NativeExecDriver)
  2216. runCmd := exec.Command(dockerBinary, "create", "busybox", "/bin/nada")
  2217. cID, _, _, err := runCommandWithStdoutStderr(runCmd)
  2218. if err != nil {
  2219. c.Fatalf("Failed to create container: %v, output: %q", err, cID)
  2220. }
  2221. cID = strings.TrimSpace(cID)
  2222. runCmd = exec.Command(dockerBinary, "start", cID)
  2223. _, _, _, _ = runCommandWithStdoutStderr(runCmd)
  2224. rc, err := inspectField(cID, "State.ExitCode")
  2225. c.Assert(err, check.IsNil)
  2226. if rc == "0" {
  2227. c.Fatalf("ExitCode(%v) cannot be 0", rc)
  2228. }
  2229. }
  2230. func (s *DockerSuite) TestRunModeIpcHost(c *check.C) {
  2231. testRequires(c, SameHostDaemon)
  2232. hostIpc, err := os.Readlink("/proc/1/ns/ipc")
  2233. if err != nil {
  2234. c.Fatal(err)
  2235. }
  2236. cmd := exec.Command(dockerBinary, "run", "--ipc=host", "busybox", "readlink", "/proc/self/ns/ipc")
  2237. out2, _, err := runCommandWithOutput(cmd)
  2238. if err != nil {
  2239. c.Fatal(err, out2)
  2240. }
  2241. out2 = strings.Trim(out2, "\n")
  2242. if hostIpc != out2 {
  2243. c.Fatalf("IPC different with --ipc=host %s != %s\n", hostIpc, out2)
  2244. }
  2245. cmd = exec.Command(dockerBinary, "run", "busybox", "readlink", "/proc/self/ns/ipc")
  2246. out2, _, err = runCommandWithOutput(cmd)
  2247. if err != nil {
  2248. c.Fatal(err, out2)
  2249. }
  2250. out2 = strings.Trim(out2, "\n")
  2251. if hostIpc == out2 {
  2252. c.Fatalf("IPC should be different without --ipc=host %s == %s\n", hostIpc, out2)
  2253. }
  2254. }
  2255. func (s *DockerSuite) TestRunModeIpcContainer(c *check.C) {
  2256. testRequires(c, SameHostDaemon)
  2257. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  2258. out, _, err := runCommandWithOutput(cmd)
  2259. if err != nil {
  2260. c.Fatal(err, out)
  2261. }
  2262. id := strings.TrimSpace(out)
  2263. state, err := inspectField(id, "State.Running")
  2264. c.Assert(err, check.IsNil)
  2265. if state != "true" {
  2266. c.Fatal("Container state is 'not running'")
  2267. }
  2268. pid1, err := inspectField(id, "State.Pid")
  2269. c.Assert(err, check.IsNil)
  2270. parentContainerIpc, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/ipc", pid1))
  2271. if err != nil {
  2272. c.Fatal(err)
  2273. }
  2274. cmd = exec.Command(dockerBinary, "run", fmt.Sprintf("--ipc=container:%s", id), "busybox", "readlink", "/proc/self/ns/ipc")
  2275. out2, _, err := runCommandWithOutput(cmd)
  2276. if err != nil {
  2277. c.Fatal(err, out2)
  2278. }
  2279. out2 = strings.Trim(out2, "\n")
  2280. if parentContainerIpc != out2 {
  2281. c.Fatalf("IPC different with --ipc=container:%s %s != %s\n", id, parentContainerIpc, out2)
  2282. }
  2283. }
  2284. func (s *DockerSuite) TestRunModeIpcContainerNotExists(c *check.C) {
  2285. cmd := exec.Command(dockerBinary, "run", "-d", "--ipc", "container:abcd1234", "busybox", "top")
  2286. out, _, err := runCommandWithOutput(cmd)
  2287. if !strings.Contains(out, "abcd1234") || err == nil {
  2288. c.Fatalf("run IPC from a non exists container should with correct error out")
  2289. }
  2290. }
  2291. func (s *DockerSuite) TestContainerNetworkMode(c *check.C) {
  2292. testRequires(c, SameHostDaemon)
  2293. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  2294. out, _, err := runCommandWithOutput(cmd)
  2295. if err != nil {
  2296. c.Fatal(err, out)
  2297. }
  2298. id := strings.TrimSpace(out)
  2299. if err := waitRun(id); err != nil {
  2300. c.Fatal(err)
  2301. }
  2302. pid1, err := inspectField(id, "State.Pid")
  2303. c.Assert(err, check.IsNil)
  2304. parentContainerNet, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/net", pid1))
  2305. if err != nil {
  2306. c.Fatal(err)
  2307. }
  2308. cmd = exec.Command(dockerBinary, "run", fmt.Sprintf("--net=container:%s", id), "busybox", "readlink", "/proc/self/ns/net")
  2309. out2, _, err := runCommandWithOutput(cmd)
  2310. if err != nil {
  2311. c.Fatal(err, out2)
  2312. }
  2313. out2 = strings.Trim(out2, "\n")
  2314. if parentContainerNet != out2 {
  2315. c.Fatalf("NET different with --net=container:%s %s != %s\n", id, parentContainerNet, out2)
  2316. }
  2317. }
  2318. func (s *DockerSuite) TestContainerNetworkModeToSelf(c *check.C) {
  2319. cmd := exec.Command(dockerBinary, "run", "--name=me", "--net=container:me", "busybox", "true")
  2320. out, _, err := runCommandWithOutput(cmd)
  2321. if err == nil || !strings.Contains(out, "cannot join own network") {
  2322. c.Fatalf("using container net mode to self should result in an error")
  2323. }
  2324. }
  2325. func (s *DockerSuite) TestRunModePidHost(c *check.C) {
  2326. testRequires(c, NativeExecDriver, SameHostDaemon)
  2327. hostPid, err := os.Readlink("/proc/1/ns/pid")
  2328. if err != nil {
  2329. c.Fatal(err)
  2330. }
  2331. cmd := exec.Command(dockerBinary, "run", "--pid=host", "busybox", "readlink", "/proc/self/ns/pid")
  2332. out2, _, err := runCommandWithOutput(cmd)
  2333. if err != nil {
  2334. c.Fatal(err, out2)
  2335. }
  2336. out2 = strings.Trim(out2, "\n")
  2337. if hostPid != out2 {
  2338. c.Fatalf("PID different with --pid=host %s != %s\n", hostPid, out2)
  2339. }
  2340. cmd = exec.Command(dockerBinary, "run", "busybox", "readlink", "/proc/self/ns/pid")
  2341. out2, _, err = runCommandWithOutput(cmd)
  2342. if err != nil {
  2343. c.Fatal(err, out2)
  2344. }
  2345. out2 = strings.Trim(out2, "\n")
  2346. if hostPid == out2 {
  2347. c.Fatalf("PID should be different without --pid=host %s == %s\n", hostPid, out2)
  2348. }
  2349. }
  2350. func (s *DockerSuite) TestRunModeUTSHost(c *check.C) {
  2351. testRequires(c, NativeExecDriver, SameHostDaemon)
  2352. defer deleteAllContainers()
  2353. hostUTS, err := os.Readlink("/proc/1/ns/uts")
  2354. if err != nil {
  2355. c.Fatal(err)
  2356. }
  2357. cmd := exec.Command(dockerBinary, "run", "--uts=host", "busybox", "readlink", "/proc/self/ns/uts")
  2358. out2, _, err := runCommandWithOutput(cmd)
  2359. if err != nil {
  2360. c.Fatal(err, out2)
  2361. }
  2362. out2 = strings.Trim(out2, "\n")
  2363. if hostUTS != out2 {
  2364. c.Fatalf("UTS different with --uts=host %s != %s\n", hostUTS, out2)
  2365. }
  2366. cmd = exec.Command(dockerBinary, "run", "busybox", "readlink", "/proc/self/ns/uts")
  2367. out2, _, err = runCommandWithOutput(cmd)
  2368. if err != nil {
  2369. c.Fatal(err, out2)
  2370. }
  2371. out2 = strings.Trim(out2, "\n")
  2372. if hostUTS == out2 {
  2373. c.Fatalf("UTS should be different without --uts=host %s == %s\n", hostUTS, out2)
  2374. }
  2375. }
  2376. func (s *DockerSuite) TestRunTLSverify(c *check.C) {
  2377. cmd := exec.Command(dockerBinary, "ps")
  2378. out, ec, err := runCommandWithOutput(cmd)
  2379. if err != nil || ec != 0 {
  2380. c.Fatalf("Should have worked: %v:\n%v", err, out)
  2381. }
  2382. // Regardless of whether we specify true or false we need to
  2383. // test to make sure tls is turned on if --tlsverify is specified at all
  2384. cmd = exec.Command(dockerBinary, "--tlsverify=false", "ps")
  2385. out, ec, err = runCommandWithOutput(cmd)
  2386. if err == nil || ec == 0 || !strings.Contains(out, "trying to connect") {
  2387. c.Fatalf("Should have failed: \net:%v\nout:%v\nerr:%v", ec, out, err)
  2388. }
  2389. cmd = exec.Command(dockerBinary, "--tlsverify=true", "ps")
  2390. out, ec, err = runCommandWithOutput(cmd)
  2391. if err == nil || ec == 0 || !strings.Contains(out, "cert") {
  2392. c.Fatalf("Should have failed: \net:%v\nout:%v\nerr:%v", ec, out, err)
  2393. }
  2394. }
  2395. func (s *DockerSuite) TestRunPortFromDockerRangeInUse(c *check.C) {
  2396. // first find allocator current position
  2397. cmd := exec.Command(dockerBinary, "run", "-d", "-p", ":80", "busybox", "top")
  2398. out, _, err := runCommandWithOutput(cmd)
  2399. if err != nil {
  2400. c.Fatal(out, err)
  2401. }
  2402. id := strings.TrimSpace(out)
  2403. cmd = exec.Command(dockerBinary, "port", id)
  2404. out, _, err = runCommandWithOutput(cmd)
  2405. if err != nil {
  2406. c.Fatal(out, err)
  2407. }
  2408. out = strings.TrimSpace(out)
  2409. if out == "" {
  2410. c.Fatal("docker port command output is empty")
  2411. }
  2412. out = strings.Split(out, ":")[1]
  2413. lastPort, err := strconv.Atoi(out)
  2414. if err != nil {
  2415. c.Fatal(err)
  2416. }
  2417. port := lastPort + 1
  2418. l, err := net.Listen("tcp", ":"+strconv.Itoa(port))
  2419. if err != nil {
  2420. c.Fatal(err)
  2421. }
  2422. defer l.Close()
  2423. cmd = exec.Command(dockerBinary, "run", "-d", "-p", ":80", "busybox", "top")
  2424. out, _, err = runCommandWithOutput(cmd)
  2425. if err != nil {
  2426. c.Fatalf(out, err)
  2427. }
  2428. id = strings.TrimSpace(out)
  2429. cmd = exec.Command(dockerBinary, "port", id)
  2430. out, _, err = runCommandWithOutput(cmd)
  2431. if err != nil {
  2432. c.Fatal(out, err)
  2433. }
  2434. }
  2435. func (s *DockerSuite) TestRunTtyWithPipe(c *check.C) {
  2436. errChan := make(chan error)
  2437. go func() {
  2438. defer close(errChan)
  2439. cmd := exec.Command(dockerBinary, "run", "-ti", "busybox", "true")
  2440. if _, err := cmd.StdinPipe(); err != nil {
  2441. errChan <- err
  2442. return
  2443. }
  2444. expected := "cannot enable tty mode"
  2445. if out, _, err := runCommandWithOutput(cmd); err == nil {
  2446. errChan <- fmt.Errorf("run should have failed")
  2447. return
  2448. } else if !strings.Contains(out, expected) {
  2449. errChan <- fmt.Errorf("run failed with error %q: expected %q", out, expected)
  2450. return
  2451. }
  2452. }()
  2453. select {
  2454. case err := <-errChan:
  2455. c.Assert(err, check.IsNil)
  2456. case <-time.After(3 * time.Second):
  2457. c.Fatal("container is running but should have failed")
  2458. }
  2459. }
  2460. func (s *DockerSuite) TestRunNonLocalMacAddress(c *check.C) {
  2461. addr := "00:16:3E:08:00:50"
  2462. cmd := exec.Command(dockerBinary, "run", "--mac-address", addr, "busybox", "ifconfig")
  2463. if out, _, err := runCommandWithOutput(cmd); err != nil || !strings.Contains(out, addr) {
  2464. c.Fatalf("Output should have contained %q: %s, %v", addr, out, err)
  2465. }
  2466. }
  2467. func (s *DockerSuite) TestRunNetHost(c *check.C) {
  2468. testRequires(c, SameHostDaemon)
  2469. hostNet, err := os.Readlink("/proc/1/ns/net")
  2470. if err != nil {
  2471. c.Fatal(err)
  2472. }
  2473. cmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "readlink", "/proc/self/ns/net")
  2474. out2, _, err := runCommandWithOutput(cmd)
  2475. if err != nil {
  2476. c.Fatal(err, out2)
  2477. }
  2478. out2 = strings.Trim(out2, "\n")
  2479. if hostNet != out2 {
  2480. c.Fatalf("Net namespace different with --net=host %s != %s\n", hostNet, out2)
  2481. }
  2482. cmd = exec.Command(dockerBinary, "run", "busybox", "readlink", "/proc/self/ns/net")
  2483. out2, _, err = runCommandWithOutput(cmd)
  2484. if err != nil {
  2485. c.Fatal(err, out2)
  2486. }
  2487. out2 = strings.Trim(out2, "\n")
  2488. if hostNet == out2 {
  2489. c.Fatalf("Net namespace should be different without --net=host %s == %s\n", hostNet, out2)
  2490. }
  2491. }
  2492. func (s *DockerSuite) TestRunNetContainerWhichHost(c *check.C) {
  2493. testRequires(c, SameHostDaemon)
  2494. hostNet, err := os.Readlink("/proc/1/ns/net")
  2495. if err != nil {
  2496. c.Fatal(err)
  2497. }
  2498. cmd := exec.Command(dockerBinary, "run", "-d", "--net=host", "--name=test", "busybox", "top")
  2499. out, _, err := runCommandWithOutput(cmd)
  2500. if err != nil {
  2501. c.Fatal(err, out)
  2502. }
  2503. cmd = exec.Command(dockerBinary, "run", "--net=container:test", "busybox", "readlink", "/proc/self/ns/net")
  2504. out, _, err = runCommandWithOutput(cmd)
  2505. if err != nil {
  2506. c.Fatal(err, out)
  2507. }
  2508. out = strings.Trim(out, "\n")
  2509. if hostNet != out {
  2510. c.Fatalf("Container should have host network namespace")
  2511. }
  2512. }
  2513. func (s *DockerSuite) TestRunAllowPortRangeThroughPublish(c *check.C) {
  2514. cmd := exec.Command(dockerBinary, "run", "-d", "--expose", "3000-3003", "-p", "3000-3003", "busybox", "top")
  2515. out, _, err := runCommandWithOutput(cmd)
  2516. id := strings.TrimSpace(out)
  2517. portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports")
  2518. c.Assert(err, check.IsNil)
  2519. var ports nat.PortMap
  2520. err = unmarshalJSON([]byte(portstr), &ports)
  2521. for port, binding := range ports {
  2522. portnum, _ := strconv.Atoi(strings.Split(string(port), "/")[0])
  2523. if portnum < 3000 || portnum > 3003 {
  2524. c.Fatalf("Port %d is out of range ", portnum)
  2525. }
  2526. if binding == nil || len(binding) != 1 || len(binding[0].HostPort) == 0 {
  2527. c.Fatal("Port is not mapped for the port "+port, out)
  2528. }
  2529. }
  2530. }
  2531. func (s *DockerSuite) TestRunOOMExitCode(c *check.C) {
  2532. errChan := make(chan error)
  2533. go func() {
  2534. defer close(errChan)
  2535. runCmd := exec.Command(dockerBinary, "run", "-m", "4MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
  2536. out, exitCode, _ := runCommandWithOutput(runCmd)
  2537. if expected := 137; exitCode != expected {
  2538. errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out)
  2539. }
  2540. }()
  2541. select {
  2542. case err := <-errChan:
  2543. c.Assert(err, check.IsNil)
  2544. case <-time.After(30 * time.Second):
  2545. c.Fatal("Timeout waiting for container to die on OOM")
  2546. }
  2547. }
  2548. func (s *DockerSuite) TestRunSetDefaultRestartPolicy(c *check.C) {
  2549. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "test", "busybox", "top")
  2550. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  2551. c.Fatalf("failed to run container: %v, output: %q", err, out)
  2552. }
  2553. out, err := inspectField("test", "HostConfig.RestartPolicy.Name")
  2554. c.Assert(err, check.IsNil)
  2555. if out != "no" {
  2556. c.Fatalf("Set default restart policy failed")
  2557. }
  2558. }
  2559. func (s *DockerSuite) TestRunRestartMaxRetries(c *check.C) {
  2560. out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "false").CombinedOutput()
  2561. if err != nil {
  2562. c.Fatal(string(out), err)
  2563. }
  2564. id := strings.TrimSpace(string(out))
  2565. if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 10); err != nil {
  2566. c.Fatal(err)
  2567. }
  2568. count, err := inspectField(id, "RestartCount")
  2569. c.Assert(err, check.IsNil)
  2570. if count != "3" {
  2571. c.Fatalf("Container was restarted %s times, expected %d", count, 3)
  2572. }
  2573. MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
  2574. c.Assert(err, check.IsNil)
  2575. if MaximumRetryCount != "3" {
  2576. c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
  2577. }
  2578. }
  2579. func (s *DockerSuite) TestRunContainerWithWritableRootfs(c *check.C) {
  2580. out, err := exec.Command(dockerBinary, "run", "--rm", "busybox", "touch", "/file").CombinedOutput()
  2581. if err != nil {
  2582. c.Fatal(string(out), err)
  2583. }
  2584. }
  2585. func (s *DockerSuite) TestRunContainerWithReadonlyRootfs(c *check.C) {
  2586. testRequires(c, NativeExecDriver)
  2587. for _, f := range []string{"/file", "/etc/hosts", "/etc/resolv.conf", "/etc/hostname"} {
  2588. testReadOnlyFile(f, c)
  2589. }
  2590. }
  2591. func testReadOnlyFile(filename string, c *check.C) {
  2592. testRequires(c, NativeExecDriver)
  2593. out, err := exec.Command(dockerBinary, "run", "--read-only", "--rm", "busybox", "touch", filename).CombinedOutput()
  2594. if err == nil {
  2595. c.Fatal("expected container to error on run with read only error")
  2596. }
  2597. expected := "Read-only file system"
  2598. if !strings.Contains(string(out), expected) {
  2599. c.Fatalf("expected output from failure to contain %s but contains %s", expected, out)
  2600. }
  2601. }
  2602. func (s *DockerSuite) TestRunContainerWithReadonlyEtcHostsAndLinkedContainer(c *check.C) {
  2603. testRequires(c, NativeExecDriver)
  2604. _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "test-etc-hosts-ro-linked", "busybox", "top"))
  2605. c.Assert(err, check.IsNil)
  2606. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--read-only", "--link", "test-etc-hosts-ro-linked:testlinked", "busybox", "cat", "/etc/hosts"))
  2607. c.Assert(err, check.IsNil)
  2608. if !strings.Contains(string(out), "testlinked") {
  2609. c.Fatal("Expected /etc/hosts to be updated even if --read-only enabled")
  2610. }
  2611. }
  2612. func (s *DockerSuite) TestRunContainerWithReadonlyRootfsWithDnsFlag(c *check.C) {
  2613. testRequires(c, NativeExecDriver)
  2614. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--read-only", "--dns", "1.1.1.1", "busybox", "/bin/cat", "/etc/resolv.conf"))
  2615. c.Assert(err, check.IsNil)
  2616. if !strings.Contains(string(out), "1.1.1.1") {
  2617. c.Fatal("Expected /etc/resolv.conf to be updated even if --read-only enabled and --dns flag used")
  2618. }
  2619. }
  2620. func (s *DockerSuite) TestRunContainerWithReadonlyRootfsWithAddHostFlag(c *check.C) {
  2621. testRequires(c, NativeExecDriver)
  2622. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--read-only", "--add-host", "testreadonly:127.0.0.1", "busybox", "/bin/cat", "/etc/hosts"))
  2623. c.Assert(err, check.IsNil)
  2624. if !strings.Contains(string(out), "testreadonly") {
  2625. c.Fatal("Expected /etc/hosts to be updated even if --read-only enabled and --add-host flag used")
  2626. }
  2627. }
  2628. func (s *DockerSuite) TestRunVolumesFromRestartAfterRemoved(c *check.C) {
  2629. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "voltest", "-v", "/foo", "busybox"))
  2630. if err != nil {
  2631. c.Fatal(out, err)
  2632. }
  2633. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "restarter", "--volumes-from", "voltest", "busybox", "top"))
  2634. if err != nil {
  2635. c.Fatal(out, err)
  2636. }
  2637. // Remove the main volume container and restart the consuming container
  2638. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "rm", "-f", "voltest"))
  2639. if err != nil {
  2640. c.Fatal(out, err)
  2641. }
  2642. // This should not fail since the volumes-from were already applied
  2643. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "restart", "restarter"))
  2644. if err != nil {
  2645. c.Fatalf("expected container to restart successfully: %v\n%s", err, out)
  2646. }
  2647. }
  2648. // run container with --rm should remove container if exit code != 0
  2649. func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.C) {
  2650. name := "flowers"
  2651. runCmd := exec.Command(dockerBinary, "run", "--name", name, "--rm", "busybox", "ls", "/notexists")
  2652. out, _, err := runCommandWithOutput(runCmd)
  2653. if err == nil {
  2654. c.Fatal("Expected docker run to fail", out, err)
  2655. }
  2656. out, err = getAllContainers()
  2657. if err != nil {
  2658. c.Fatal(out, err)
  2659. }
  2660. if out != "" {
  2661. c.Fatal("Expected not to have containers", out)
  2662. }
  2663. }
  2664. func (s *DockerSuite) TestRunContainerWithRmFlagCannotStartContainer(c *check.C) {
  2665. name := "sparkles"
  2666. runCmd := exec.Command(dockerBinary, "run", "--name", name, "--rm", "busybox", "commandNotFound")
  2667. out, _, err := runCommandWithOutput(runCmd)
  2668. if err == nil {
  2669. c.Fatal("Expected docker run to fail", out, err)
  2670. }
  2671. out, err = getAllContainers()
  2672. if err != nil {
  2673. c.Fatal(out, err)
  2674. }
  2675. if out != "" {
  2676. c.Fatal("Expected not to have containers", out)
  2677. }
  2678. }
  2679. func (s *DockerSuite) TestRunPidHostWithChildIsKillable(c *check.C) {
  2680. name := "ibuildthecloud"
  2681. if out, err := exec.Command(dockerBinary, "run", "-d", "--pid=host", "--name", name, "busybox", "sh", "-c", "sleep 30; echo hi").CombinedOutput(); err != nil {
  2682. c.Fatal(err, out)
  2683. }
  2684. time.Sleep(1 * time.Second)
  2685. errchan := make(chan error)
  2686. go func() {
  2687. if out, err := exec.Command(dockerBinary, "kill", name).CombinedOutput(); err != nil {
  2688. errchan <- fmt.Errorf("%v:\n%s", err, out)
  2689. }
  2690. close(errchan)
  2691. }()
  2692. select {
  2693. case err := <-errchan:
  2694. c.Assert(err, check.IsNil)
  2695. case <-time.After(5 * time.Second):
  2696. c.Fatal("Kill container timed out")
  2697. }
  2698. }
  2699. func (s *DockerSuite) TestRunWithTooSmallMemoryLimit(c *check.C) {
  2700. defer deleteAllContainers()
  2701. // this memory limit is 1 byte less than the min, which is 4MB
  2702. // https://github.com/docker/docker/blob/v1.5.0/daemon/create.go#L22
  2703. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-m", "4194303", "busybox"))
  2704. if err == nil || !strings.Contains(out, "Minimum memory limit allowed is 4MB") {
  2705. c.Fatalf("expected run to fail when using too low a memory limit: %q", out)
  2706. }
  2707. }
  2708. func (s *DockerSuite) TestRunWriteToProcAsound(c *check.C) {
  2709. defer deleteAllContainers()
  2710. code, err := runCommand(exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "echo 111 >> /proc/asound/version"))
  2711. if err == nil || code == 0 {
  2712. c.Fatal("standard container should not be able to write to /proc/asound")
  2713. }
  2714. }
  2715. func (s *DockerSuite) TestRunReadProcTimer(c *check.C) {
  2716. testRequires(c, NativeExecDriver)
  2717. defer deleteAllContainers()
  2718. out, code, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "busybox", "cat", "/proc/timer_stats"))
  2719. if err != nil || code != 0 {
  2720. c.Fatal(err)
  2721. }
  2722. if strings.Trim(out, "\n ") != "" {
  2723. c.Fatalf("expected to receive no output from /proc/timer_stats but received %q", out)
  2724. }
  2725. }
  2726. func (s *DockerSuite) TestRunReadProcLatency(c *check.C) {
  2727. testRequires(c, NativeExecDriver)
  2728. // some kernels don't have this configured so skip the test if this file is not found
  2729. // on the host running the tests.
  2730. if _, err := os.Stat("/proc/latency_stats"); err != nil {
  2731. c.Skip("kernel doesnt have latency_stats configured")
  2732. return
  2733. }
  2734. defer deleteAllContainers()
  2735. out, code, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "busybox", "cat", "/proc/latency_stats"))
  2736. if err != nil || code != 0 {
  2737. c.Fatal(err)
  2738. }
  2739. if strings.Trim(out, "\n ") != "" {
  2740. c.Fatalf("expected to receive no output from /proc/latency_stats but received %q", out)
  2741. }
  2742. }
  2743. func (s *DockerSuite) TestMountIntoProc(c *check.C) {
  2744. testRequires(c, NativeExecDriver)
  2745. defer deleteAllContainers()
  2746. code, err := runCommand(exec.Command(dockerBinary, "run", "-v", "/proc//sys", "busybox", "true"))
  2747. if err == nil || code == 0 {
  2748. c.Fatal("container should not be able to mount into /proc")
  2749. }
  2750. }
  2751. func (s *DockerSuite) TestMountIntoSys(c *check.C) {
  2752. testRequires(c, NativeExecDriver)
  2753. defer deleteAllContainers()
  2754. _, err := runCommand(exec.Command(dockerBinary, "run", "-v", "/sys/fs/cgroup", "busybox", "true"))
  2755. if err != nil {
  2756. c.Fatal("container should be able to mount into /sys/fs/cgroup")
  2757. }
  2758. }