docker_cli_run_test.go 102 KB

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