docker_cli_run_test.go 101 KB

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