docker_cli_run_test.go 102 KB

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