docker_cli_run_test.go 103 KB

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