docker_cli_run_test.go 106 KB

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