docker_cli_run_test.go 102 KB

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