docker_cli_run_test.go 98 KB

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