docker_cli_run_test.go 102 KB

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