docker_cli_run_test.go 103 KB

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