docker_cli_run_test.go 101 KB

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