docker_cli_run_test.go 103 KB

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