main.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. from __future__ import absolute_import
  2. from __future__ import print_function
  3. from __future__ import unicode_literals
  4. import contextlib
  5. import functools
  6. import json
  7. import logging
  8. import pipes
  9. import re
  10. import subprocess
  11. import sys
  12. from distutils.spawn import find_executable
  13. from inspect import getdoc
  14. from operator import attrgetter
  15. from . import errors
  16. from . import signals
  17. from .. import __version__
  18. from ..bundle import get_image_digests
  19. from ..bundle import MissingDigests
  20. from ..bundle import serialize_bundle
  21. from ..config import ConfigurationError
  22. from ..config import parse_environment
  23. from ..config import resolve_build_args
  24. from ..config.environment import Environment
  25. from ..config.serialize import serialize_config
  26. from ..config.types import VolumeSpec
  27. from ..const import COMPOSEFILE_V2_2 as V2_2
  28. from ..const import IS_WINDOWS_PLATFORM
  29. from ..errors import StreamParseError
  30. from ..progress_stream import StreamOutputError
  31. from ..project import NoSuchService
  32. from ..project import OneOffFilter
  33. from ..project import ProjectError
  34. from ..service import BuildAction
  35. from ..service import BuildError
  36. from ..service import ConvergenceStrategy
  37. from ..service import ImageType
  38. from ..service import NeedsBuildError
  39. from ..service import OperationFailedError
  40. from .command import get_config_from_options
  41. from .command import project_from_options
  42. from .docopt_command import DocoptDispatcher
  43. from .docopt_command import get_handler
  44. from .docopt_command import NoSuchCommand
  45. from .errors import UserError
  46. from .formatter import ConsoleWarningFormatter
  47. from .formatter import Formatter
  48. from .log_printer import build_log_presenters
  49. from .log_printer import LogPrinter
  50. from .utils import get_version_info
  51. from .utils import human_readable_file_size
  52. from .utils import yesno
  53. if not IS_WINDOWS_PLATFORM:
  54. from dockerpty.pty import PseudoTerminal, RunOperation, ExecOperation
  55. log = logging.getLogger(__name__)
  56. console_handler = logging.StreamHandler(sys.stderr)
  57. def main():
  58. signals.ignore_sigpipe()
  59. try:
  60. command = dispatch()
  61. command()
  62. except (KeyboardInterrupt, signals.ShutdownException):
  63. log.error("Aborting.")
  64. sys.exit(1)
  65. except (UserError, NoSuchService, ConfigurationError,
  66. ProjectError, OperationFailedError) as e:
  67. log.error(e.msg)
  68. sys.exit(1)
  69. except BuildError as e:
  70. log.error("Service '%s' failed to build: %s" % (e.service.name, e.reason))
  71. sys.exit(1)
  72. except StreamOutputError as e:
  73. log.error(e)
  74. sys.exit(1)
  75. except NeedsBuildError as e:
  76. log.error("Service '%s' needs to be built, but --no-build was passed." % e.service.name)
  77. sys.exit(1)
  78. except NoSuchCommand as e:
  79. commands = "\n".join(parse_doc_section("commands:", getdoc(e.supercommand)))
  80. log.error("No such command: %s\n\n%s", e.command, commands)
  81. sys.exit(1)
  82. except (errors.ConnectionError, StreamParseError):
  83. sys.exit(1)
  84. def dispatch():
  85. setup_logging()
  86. dispatcher = DocoptDispatcher(
  87. TopLevelCommand,
  88. {'options_first': True, 'version': get_version_info('compose')})
  89. options, handler, command_options = dispatcher.parse(sys.argv[1:])
  90. setup_console_handler(console_handler, options.get('--verbose'))
  91. return functools.partial(perform_command, options, handler, command_options)
  92. def perform_command(options, handler, command_options):
  93. if options['COMMAND'] in ('help', 'version'):
  94. # Skip looking up the compose file.
  95. handler(command_options)
  96. return
  97. if options['COMMAND'] in ('config', 'bundle'):
  98. command = TopLevelCommand(None)
  99. handler(command, options, command_options)
  100. return
  101. project = project_from_options('.', options)
  102. command = TopLevelCommand(project)
  103. with errors.handle_connection_errors(project.client):
  104. handler(command, command_options)
  105. def setup_logging():
  106. root_logger = logging.getLogger()
  107. root_logger.addHandler(console_handler)
  108. root_logger.setLevel(logging.DEBUG)
  109. # Disable requests logging
  110. logging.getLogger("requests").propagate = False
  111. def setup_console_handler(handler, verbose):
  112. if handler.stream.isatty():
  113. format_class = ConsoleWarningFormatter
  114. else:
  115. format_class = logging.Formatter
  116. if verbose:
  117. handler.setFormatter(format_class('%(name)s.%(funcName)s: %(message)s'))
  118. handler.setLevel(logging.DEBUG)
  119. else:
  120. handler.setFormatter(format_class())
  121. handler.setLevel(logging.INFO)
  122. # stolen from docopt master
  123. def parse_doc_section(name, source):
  124. pattern = re.compile('^([^\n]*' + name + '[^\n]*\n?(?:[ \t].*?(?:\n|$))*)',
  125. re.IGNORECASE | re.MULTILINE)
  126. return [s.strip() for s in pattern.findall(source)]
  127. class TopLevelCommand(object):
  128. """Define and run multi-container applications with Docker.
  129. Usage:
  130. docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  131. docker-compose -h|--help
  132. Options:
  133. -f, --file FILE Specify an alternate compose file (default: docker-compose.yml)
  134. -p, --project-name NAME Specify an alternate project name (default: directory name)
  135. --verbose Show more output
  136. -v, --version Print version and exit
  137. -H, --host HOST Daemon socket to connect to
  138. --tls Use TLS; implied by --tlsverify
  139. --tlscacert CA_PATH Trust certs signed only by this CA
  140. --tlscert CLIENT_CERT_PATH Path to TLS certificate file
  141. --tlskey TLS_KEY_PATH Path to TLS key file
  142. --tlsverify Use TLS and verify the remote
  143. --skip-hostname-check Don't check the daemon's hostname against the name specified
  144. in the client certificate (for example if your docker host
  145. is an IP address)
  146. --project-directory PATH Specify an alternate working directory
  147. (default: the path of the Compose file)
  148. Commands:
  149. build Build or rebuild services
  150. bundle Generate a Docker bundle from the Compose file
  151. config Validate and view the Compose file
  152. create Create services
  153. down Stop and remove containers, networks, images, and volumes
  154. events Receive real time events from containers
  155. exec Execute a command in a running container
  156. help Get help on a command
  157. images List images
  158. kill Kill containers
  159. logs View output from containers
  160. pause Pause services
  161. port Print the public port for a port binding
  162. ps List containers
  163. pull Pull service images
  164. push Push service images
  165. restart Restart services
  166. rm Remove stopped containers
  167. run Run a one-off command
  168. scale Set number of containers for a service
  169. start Start services
  170. stop Stop services
  171. top Display the running processes
  172. unpause Unpause services
  173. up Create and start containers
  174. version Show the Docker-Compose version information
  175. """
  176. def __init__(self, project, project_dir='.'):
  177. self.project = project
  178. self.project_dir = '.'
  179. def build(self, options):
  180. """
  181. Build or rebuild services.
  182. Services are built once and then tagged as `project_service`,
  183. e.g. `composetest_db`. If you change a service's `Dockerfile` or the
  184. contents of its build directory, you can run `docker-compose build` to rebuild it.
  185. Usage: build [options] [--build-arg key=val...] [SERVICE...]
  186. Options:
  187. --force-rm Always remove intermediate containers.
  188. --no-cache Do not use cache when building the image.
  189. --pull Always attempt to pull a newer version of the image.
  190. --build-arg key=val Set build-time variables for one service.
  191. """
  192. service_names = options['SERVICE']
  193. build_args = options.get('--build-arg', None)
  194. if build_args:
  195. environment = Environment.from_env_file(self.project_dir)
  196. build_args = resolve_build_args(build_args, environment)
  197. if not service_names and build_args:
  198. raise UserError("Need service name for --build-arg option")
  199. self.project.build(
  200. service_names=service_names,
  201. no_cache=bool(options.get('--no-cache', False)),
  202. pull=bool(options.get('--pull', False)),
  203. force_rm=bool(options.get('--force-rm', False)),
  204. build_args=build_args)
  205. def bundle(self, config_options, options):
  206. """
  207. Generate a Distributed Application Bundle (DAB) from the Compose file.
  208. Images must have digests stored, which requires interaction with a
  209. Docker registry. If digests aren't stored for all images, you can fetch
  210. them with `docker-compose pull` or `docker-compose push`. To push images
  211. automatically when bundling, pass `--push-images`. Only services with
  212. a `build` option specified will have their images pushed.
  213. Usage: bundle [options]
  214. Options:
  215. --push-images Automatically push images for any services
  216. which have a `build` option specified.
  217. -o, --output PATH Path to write the bundle file to.
  218. Defaults to "<project name>.dab".
  219. """
  220. self.project = project_from_options('.', config_options)
  221. compose_config = get_config_from_options(self.project_dir, config_options)
  222. output = options["--output"]
  223. if not output:
  224. output = "{}.dab".format(self.project.name)
  225. image_digests = image_digests_for_project(self.project, options['--push-images'])
  226. with open(output, 'w') as f:
  227. f.write(serialize_bundle(compose_config, image_digests))
  228. log.info("Wrote bundle to {}".format(output))
  229. def config(self, config_options, options):
  230. """
  231. Validate and view the Compose file.
  232. Usage: config [options]
  233. Options:
  234. --resolve-image-digests Pin image tags to digests.
  235. -q, --quiet Only validate the configuration, don't print
  236. anything.
  237. --services Print the service names, one per line.
  238. --volumes Print the volume names, one per line.
  239. """
  240. compose_config = get_config_from_options(self.project_dir, config_options)
  241. image_digests = None
  242. if options['--resolve-image-digests']:
  243. self.project = project_from_options('.', config_options)
  244. image_digests = image_digests_for_project(self.project)
  245. if options['--quiet']:
  246. return
  247. if options['--services']:
  248. print('\n'.join(service['name'] for service in compose_config.services))
  249. return
  250. if options['--volumes']:
  251. print('\n'.join(volume for volume in compose_config.volumes))
  252. return
  253. print(serialize_config(compose_config, image_digests))
  254. def create(self, options):
  255. """
  256. Creates containers for a service.
  257. Usage: create [options] [SERVICE...]
  258. Options:
  259. --force-recreate Recreate containers even if their configuration and
  260. image haven't changed. Incompatible with --no-recreate.
  261. --no-recreate If containers already exist, don't recreate them.
  262. Incompatible with --force-recreate.
  263. --no-build Don't build an image, even if it's missing.
  264. --build Build images before creating containers.
  265. """
  266. service_names = options['SERVICE']
  267. self.project.create(
  268. service_names=service_names,
  269. strategy=convergence_strategy_from_opts(options),
  270. do_build=build_action_from_opts(options),
  271. )
  272. def down(self, options):
  273. """
  274. Stops containers and removes containers, networks, volumes, and images
  275. created by `up`.
  276. By default, the only things removed are:
  277. - Containers for services defined in the Compose file
  278. - Networks defined in the `networks` section of the Compose file
  279. - The default network, if one is used
  280. Networks and volumes defined as `external` are never removed.
  281. Usage: down [options]
  282. Options:
  283. --rmi type Remove images. Type must be one of:
  284. 'all': Remove all images used by any service.
  285. 'local': Remove only images that don't have a custom tag
  286. set by the `image` field.
  287. -v, --volumes Remove named volumes declared in the `volumes` section
  288. of the Compose file and anonymous volumes
  289. attached to containers.
  290. --remove-orphans Remove containers for services not defined in the
  291. Compose file
  292. """
  293. image_type = image_type_from_opt('--rmi', options['--rmi'])
  294. self.project.down(image_type, options['--volumes'], options['--remove-orphans'])
  295. def events(self, options):
  296. """
  297. Receive real time events from containers.
  298. Usage: events [options] [SERVICE...]
  299. Options:
  300. --json Output events as a stream of json objects
  301. """
  302. def format_event(event):
  303. attributes = ["%s=%s" % item for item in event['attributes'].items()]
  304. return ("{time} {type} {action} {id} ({attrs})").format(
  305. attrs=", ".join(sorted(attributes)),
  306. **event)
  307. def json_format_event(event):
  308. event['time'] = event['time'].isoformat()
  309. event.pop('container')
  310. return json.dumps(event)
  311. for event in self.project.events():
  312. formatter = json_format_event if options['--json'] else format_event
  313. print(formatter(event))
  314. sys.stdout.flush()
  315. def exec_command(self, options):
  316. """
  317. Execute a command in a running container
  318. Usage: exec [options] SERVICE COMMAND [ARGS...]
  319. Options:
  320. -d Detached mode: Run command in the background.
  321. --privileged Give extended privileges to the process.
  322. -u, --user USER Run the command as this user.
  323. -T Disable pseudo-tty allocation. By default `docker-compose exec`
  324. allocates a TTY.
  325. --index=index index of the container if there are multiple
  326. instances of a service [default: 1]
  327. """
  328. index = int(options.get('--index'))
  329. service = self.project.get_service(options['SERVICE'])
  330. detach = options['-d']
  331. try:
  332. container = service.get_container(number=index)
  333. except ValueError as e:
  334. raise UserError(str(e))
  335. command = [options['COMMAND']] + options['ARGS']
  336. tty = not options["-T"]
  337. if IS_WINDOWS_PLATFORM and not detach:
  338. args = ["exec"]
  339. if options["-d"]:
  340. args += ["--detach"]
  341. else:
  342. args += ["--interactive"]
  343. if not options["-T"]:
  344. args += ["--tty"]
  345. if options["--privileged"]:
  346. args += ["--privileged"]
  347. if options["--user"]:
  348. args += ["--user", options["--user"]]
  349. args += [container.id]
  350. args += command
  351. sys.exit(call_docker(args))
  352. create_exec_options = {
  353. "privileged": options["--privileged"],
  354. "user": options["--user"],
  355. "tty": tty,
  356. "stdin": tty,
  357. }
  358. exec_id = container.create_exec(command, **create_exec_options)
  359. if detach:
  360. container.start_exec(exec_id, tty=tty, stream=True)
  361. return
  362. signals.set_signal_handler_to_shutdown()
  363. try:
  364. operation = ExecOperation(
  365. self.project.client,
  366. exec_id,
  367. interactive=tty,
  368. )
  369. pty = PseudoTerminal(self.project.client, operation)
  370. pty.start()
  371. except signals.ShutdownException:
  372. log.info("received shutdown exception: closing")
  373. exit_code = self.project.client.exec_inspect(exec_id).get("ExitCode")
  374. sys.exit(exit_code)
  375. @classmethod
  376. def help(cls, options):
  377. """
  378. Get help on a command.
  379. Usage: help [COMMAND]
  380. """
  381. if options['COMMAND']:
  382. subject = get_handler(cls, options['COMMAND'])
  383. else:
  384. subject = cls
  385. print(getdoc(subject))
  386. def images(self, options):
  387. """
  388. List images used by the created containers.
  389. Usage: images [options] [SERVICE...]
  390. Options:
  391. -q Only display IDs
  392. """
  393. containers = sorted(
  394. self.project.containers(service_names=options['SERVICE'], stopped=True) +
  395. self.project.containers(service_names=options['SERVICE'], one_off=OneOffFilter.only),
  396. key=attrgetter('name'))
  397. if options['-q']:
  398. for image in set(c.image for c in containers):
  399. print(image.split(':')[1])
  400. else:
  401. headers = [
  402. 'Container',
  403. 'Repository',
  404. 'Tag',
  405. 'Image Id',
  406. 'Size'
  407. ]
  408. rows = []
  409. for container in containers:
  410. image_config = container.image_config
  411. repo_tags = image_config['RepoTags'][0].split(':')
  412. image_id = image_config['Id'].split(':')[1][:12]
  413. size = human_readable_file_size(image_config['Size'])
  414. rows.append([
  415. container.name,
  416. repo_tags[0],
  417. repo_tags[1],
  418. image_id,
  419. size
  420. ])
  421. print(Formatter().table(headers, rows))
  422. def kill(self, options):
  423. """
  424. Force stop service containers.
  425. Usage: kill [options] [SERVICE...]
  426. Options:
  427. -s SIGNAL SIGNAL to send to the container.
  428. Default signal is SIGKILL.
  429. """
  430. signal = options.get('-s', 'SIGKILL')
  431. self.project.kill(service_names=options['SERVICE'], signal=signal)
  432. def logs(self, options):
  433. """
  434. View output from containers.
  435. Usage: logs [options] [SERVICE...]
  436. Options:
  437. --no-color Produce monochrome output.
  438. -f, --follow Follow log output.
  439. -t, --timestamps Show timestamps.
  440. --tail="all" Number of lines to show from the end of the logs
  441. for each container.
  442. """
  443. containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
  444. tail = options['--tail']
  445. if tail is not None:
  446. if tail.isdigit():
  447. tail = int(tail)
  448. elif tail != 'all':
  449. raise UserError("tail flag must be all or a number")
  450. log_args = {
  451. 'follow': options['--follow'],
  452. 'tail': tail,
  453. 'timestamps': options['--timestamps']
  454. }
  455. print("Attaching to", list_containers(containers))
  456. log_printer_from_project(
  457. self.project,
  458. containers,
  459. options['--no-color'],
  460. log_args,
  461. event_stream=self.project.events(service_names=options['SERVICE'])).run()
  462. def pause(self, options):
  463. """
  464. Pause services.
  465. Usage: pause [SERVICE...]
  466. """
  467. containers = self.project.pause(service_names=options['SERVICE'])
  468. exit_if(not containers, 'No containers to pause', 1)
  469. def port(self, options):
  470. """
  471. Print the public port for a port binding.
  472. Usage: port [options] SERVICE PRIVATE_PORT
  473. Options:
  474. --protocol=proto tcp or udp [default: tcp]
  475. --index=index index of the container if there are multiple
  476. instances of a service [default: 1]
  477. """
  478. index = int(options.get('--index'))
  479. service = self.project.get_service(options['SERVICE'])
  480. try:
  481. container = service.get_container(number=index)
  482. except ValueError as e:
  483. raise UserError(str(e))
  484. print(container.get_local_port(
  485. options['PRIVATE_PORT'],
  486. protocol=options.get('--protocol') or 'tcp') or '')
  487. def ps(self, options):
  488. """
  489. List containers.
  490. Usage: ps [options] [SERVICE...]
  491. Options:
  492. -q Only display IDs
  493. """
  494. containers = sorted(
  495. self.project.containers(service_names=options['SERVICE'], stopped=True) +
  496. self.project.containers(service_names=options['SERVICE'], one_off=OneOffFilter.only),
  497. key=attrgetter('name'))
  498. if options['-q']:
  499. for container in containers:
  500. print(container.id)
  501. else:
  502. headers = [
  503. 'Name',
  504. 'Command',
  505. 'State',
  506. 'Ports',
  507. ]
  508. rows = []
  509. for container in containers:
  510. command = container.human_readable_command
  511. if len(command) > 30:
  512. command = '%s ...' % command[:26]
  513. rows.append([
  514. container.name,
  515. command,
  516. container.human_readable_state,
  517. container.human_readable_ports,
  518. ])
  519. print(Formatter().table(headers, rows))
  520. def pull(self, options):
  521. """
  522. Pulls images for services defined in a Compose file, but does not start the containers.
  523. Usage: pull [options] [SERVICE...]
  524. Options:
  525. --ignore-pull-failures Pull what it can and ignores images with pull failures.
  526. --parallel Pull multiple images in parallel.
  527. """
  528. self.project.pull(
  529. service_names=options['SERVICE'],
  530. ignore_pull_failures=options.get('--ignore-pull-failures'),
  531. parallel_pull=options.get('--parallel')
  532. )
  533. def push(self, options):
  534. """
  535. Pushes images for services.
  536. Usage: push [options] [SERVICE...]
  537. Options:
  538. --ignore-push-failures Push what it can and ignores images with push failures.
  539. """
  540. self.project.push(
  541. service_names=options['SERVICE'],
  542. ignore_push_failures=options.get('--ignore-push-failures')
  543. )
  544. def rm(self, options):
  545. """
  546. Removes stopped service containers.
  547. By default, anonymous volumes attached to containers will not be removed. You
  548. can override this with `-v`. To list all volumes, use `docker volume ls`.
  549. Any data which is not in a volume will be lost.
  550. Usage: rm [options] [SERVICE...]
  551. Options:
  552. -f, --force Don't ask to confirm removal
  553. -s, --stop Stop the containers, if required, before removing
  554. -v Remove any anonymous volumes attached to containers
  555. -a, --all Deprecated - no effect.
  556. """
  557. if options.get('--all'):
  558. log.warn(
  559. '--all flag is obsolete. This is now the default behavior '
  560. 'of `docker-compose rm`'
  561. )
  562. one_off = OneOffFilter.include
  563. if options.get('--stop'):
  564. self.project.stop(service_names=options['SERVICE'], one_off=one_off)
  565. all_containers = self.project.containers(
  566. service_names=options['SERVICE'], stopped=True, one_off=one_off
  567. )
  568. stopped_containers = [c for c in all_containers if not c.is_running]
  569. if len(stopped_containers) > 0:
  570. print("Going to remove", list_containers(stopped_containers))
  571. if options.get('--force') \
  572. or yesno("Are you sure? [yN] ", default=False):
  573. self.project.remove_stopped(
  574. service_names=options['SERVICE'],
  575. v=options.get('-v', False),
  576. one_off=one_off
  577. )
  578. else:
  579. print("No stopped containers")
  580. def run(self, options):
  581. """
  582. Run a one-off command on a service.
  583. For example:
  584. $ docker-compose run web python manage.py shell
  585. By default, linked services will be started, unless they are already
  586. running. If you do not want to start linked services, use
  587. `docker-compose run --no-deps SERVICE COMMAND [ARGS...]`.
  588. Usage: run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]
  589. Options:
  590. -d Detached mode: Run container in the background, print
  591. new container name.
  592. --name NAME Assign a name to the container
  593. --entrypoint CMD Override the entrypoint of the image.
  594. -e KEY=VAL Set an environment variable (can be used multiple times)
  595. -u, --user="" Run as specified username or uid
  596. --no-deps Don't start linked services.
  597. --rm Remove container after run. Ignored in detached mode.
  598. -p, --publish=[] Publish a container's port(s) to the host
  599. --service-ports Run command with the service's ports enabled and mapped
  600. to the host.
  601. -v, --volume=[] Bind mount a volume (default [])
  602. -T Disable pseudo-tty allocation. By default `docker-compose run`
  603. allocates a TTY.
  604. -w, --workdir="" Working directory inside the container
  605. """
  606. service = self.project.get_service(options['SERVICE'])
  607. detach = options['-d']
  608. if options['--publish'] and options['--service-ports']:
  609. raise UserError(
  610. 'Service port mapping and manual port mapping '
  611. 'can not be used together'
  612. )
  613. if options['COMMAND'] is not None:
  614. command = [options['COMMAND']] + options['ARGS']
  615. elif options['--entrypoint'] is not None:
  616. command = []
  617. else:
  618. command = service.options.get('command')
  619. container_options = build_container_options(options, detach, command)
  620. run_one_off_container(container_options, self.project, service, options)
  621. def scale(self, options):
  622. """
  623. Set number of containers to run for a service.
  624. Numbers are specified in the form `service=num` as arguments.
  625. For example:
  626. $ docker-compose scale web=2 worker=3
  627. This command is deprecated. Use the up command with the `--scale` flag
  628. instead.
  629. Usage: scale [options] [SERVICE=NUM...]
  630. Options:
  631. -t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
  632. (default: 10)
  633. """
  634. timeout = timeout_from_opts(options)
  635. if self.project.config_version == V2_2:
  636. raise UserError(
  637. 'The scale command is incompatible with the v2.2 format. '
  638. 'Use the up command with the --scale flag instead.'
  639. )
  640. else:
  641. log.warn(
  642. 'The scale command is deprecated. '
  643. 'Use the up command with the --scale flag instead.'
  644. )
  645. for service_name, num in parse_scale_args(options['SERVICE=NUM']).items():
  646. self.project.get_service(service_name).scale(num, timeout=timeout)
  647. def start(self, options):
  648. """
  649. Start existing containers.
  650. Usage: start [SERVICE...]
  651. """
  652. containers = self.project.start(service_names=options['SERVICE'])
  653. exit_if(not containers, 'No containers to start', 1)
  654. def stop(self, options):
  655. """
  656. Stop running containers without removing them.
  657. They can be started again with `docker-compose start`.
  658. Usage: stop [options] [SERVICE...]
  659. Options:
  660. -t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
  661. (default: 10)
  662. """
  663. timeout = timeout_from_opts(options)
  664. self.project.stop(service_names=options['SERVICE'], timeout=timeout)
  665. def restart(self, options):
  666. """
  667. Restart running containers.
  668. Usage: restart [options] [SERVICE...]
  669. Options:
  670. -t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
  671. (default: 10)
  672. """
  673. timeout = timeout_from_opts(options)
  674. containers = self.project.restart(service_names=options['SERVICE'], timeout=timeout)
  675. exit_if(not containers, 'No containers to restart', 1)
  676. def top(self, options):
  677. """
  678. Display the running processes
  679. Usage: top [SERVICE...]
  680. """
  681. containers = sorted(
  682. self.project.containers(service_names=options['SERVICE'], stopped=False) +
  683. self.project.containers(service_names=options['SERVICE'], one_off=OneOffFilter.only),
  684. key=attrgetter('name')
  685. )
  686. for idx, container in enumerate(containers):
  687. if idx > 0:
  688. print()
  689. top_data = self.project.client.top(container.name)
  690. headers = top_data.get("Titles")
  691. rows = []
  692. for process in top_data.get("Processes", []):
  693. rows.append(process)
  694. print(container.name)
  695. print(Formatter().table(headers, rows))
  696. def unpause(self, options):
  697. """
  698. Unpause services.
  699. Usage: unpause [SERVICE...]
  700. """
  701. containers = self.project.unpause(service_names=options['SERVICE'])
  702. exit_if(not containers, 'No containers to unpause', 1)
  703. def up(self, options):
  704. """
  705. Builds, (re)creates, starts, and attaches to containers for a service.
  706. Unless they are already running, this command also starts any linked services.
  707. The `docker-compose up` command aggregates the output of each container. When
  708. the command exits, all containers are stopped. Running `docker-compose up -d`
  709. starts the containers in the background and leaves them running.
  710. If there are existing containers for a service, and the service's configuration
  711. or image was changed after the container's creation, `docker-compose up` picks
  712. up the changes by stopping and recreating the containers (preserving mounted
  713. volumes). To prevent Compose from picking up changes, use the `--no-recreate`
  714. flag.
  715. If you want to force Compose to stop and recreate all containers, use the
  716. `--force-recreate` flag.
  717. Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]
  718. Options:
  719. -d Detached mode: Run containers in the background,
  720. print new container names.
  721. Incompatible with --abort-on-container-exit.
  722. --no-color Produce monochrome output.
  723. --no-deps Don't start linked services.
  724. --force-recreate Recreate containers even if their configuration
  725. and image haven't changed.
  726. Incompatible with --no-recreate.
  727. --no-recreate If containers already exist, don't recreate them.
  728. Incompatible with --force-recreate.
  729. --no-build Don't build an image, even if it's missing.
  730. --build Build images before starting containers.
  731. --abort-on-container-exit Stops all containers if any container was stopped.
  732. Incompatible with -d.
  733. -t, --timeout TIMEOUT Use this timeout in seconds for container shutdown
  734. when attached or when containers are already
  735. running. (default: 10)
  736. --remove-orphans Remove containers for services not
  737. defined in the Compose file
  738. --exit-code-from SERVICE Return the exit code of the selected service container.
  739. Implies --abort-on-container-exit.
  740. --scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the `scale`
  741. setting in the Compose file if present.
  742. """
  743. start_deps = not options['--no-deps']
  744. exit_value_from = exitval_from_opts(options, self.project)
  745. cascade_stop = options['--abort-on-container-exit']
  746. service_names = options['SERVICE']
  747. timeout = timeout_from_opts(options)
  748. remove_orphans = options['--remove-orphans']
  749. detached = options.get('-d')
  750. if detached and cascade_stop:
  751. raise UserError("--abort-on-container-exit and -d cannot be combined.")
  752. with up_shutdown_context(self.project, service_names, timeout, detached):
  753. to_attach = self.project.up(
  754. service_names=service_names,
  755. start_deps=start_deps,
  756. strategy=convergence_strategy_from_opts(options),
  757. do_build=build_action_from_opts(options),
  758. timeout=timeout,
  759. detached=detached,
  760. remove_orphans=remove_orphans,
  761. scale_override=parse_scale_args(options['--scale']),
  762. )
  763. if detached:
  764. return
  765. attached_containers = filter_containers_to_service_names(to_attach, service_names)
  766. log_printer = log_printer_from_project(
  767. self.project,
  768. attached_containers,
  769. options['--no-color'],
  770. {'follow': True},
  771. cascade_stop,
  772. event_stream=self.project.events(service_names=service_names))
  773. print("Attaching to", list_containers(log_printer.containers))
  774. cascade_starter = log_printer.run()
  775. if cascade_stop:
  776. print("Aborting on container exit...")
  777. exit_code = 0
  778. if exit_value_from:
  779. candidates = list(filter(
  780. lambda c: c.service == exit_value_from,
  781. attached_containers))
  782. if not candidates:
  783. log.error(
  784. 'No containers matching the spec "{0}" '
  785. 'were run.'.format(exit_value_from)
  786. )
  787. exit_code = 2
  788. elif len(candidates) > 1:
  789. exit_values = filter(
  790. lambda e: e != 0,
  791. [c.inspect()['State']['ExitCode'] for c in candidates]
  792. )
  793. exit_code = exit_values[0]
  794. else:
  795. exit_code = candidates[0].inspect()['State']['ExitCode']
  796. else:
  797. for e in self.project.containers(service_names=options['SERVICE'], stopped=True):
  798. if (not e.is_running and cascade_starter == e.name):
  799. if not e.exit_code == 0:
  800. exit_code = e.exit_code
  801. break
  802. self.project.stop(service_names=service_names, timeout=timeout)
  803. sys.exit(exit_code)
  804. @classmethod
  805. def version(cls, options):
  806. """
  807. Show version informations
  808. Usage: version [--short]
  809. Options:
  810. --short Shows only Compose's version number.
  811. """
  812. if options['--short']:
  813. print(__version__)
  814. else:
  815. print(get_version_info('full'))
  816. def convergence_strategy_from_opts(options):
  817. no_recreate = options['--no-recreate']
  818. force_recreate = options['--force-recreate']
  819. if force_recreate and no_recreate:
  820. raise UserError("--force-recreate and --no-recreate cannot be combined.")
  821. if force_recreate:
  822. return ConvergenceStrategy.always
  823. if no_recreate:
  824. return ConvergenceStrategy.never
  825. return ConvergenceStrategy.changed
  826. def timeout_from_opts(options):
  827. timeout = options.get('--timeout')
  828. return None if timeout is None else int(timeout)
  829. def image_digests_for_project(project, allow_push=False):
  830. with errors.handle_connection_errors(project.client):
  831. try:
  832. return get_image_digests(
  833. project,
  834. allow_push=allow_push
  835. )
  836. except MissingDigests as e:
  837. def list_images(images):
  838. return "\n".join(" {}".format(name) for name in sorted(images))
  839. paras = ["Some images are missing digests."]
  840. if e.needs_push:
  841. command_hint = (
  842. "Use `docker-compose push {}` to push them. "
  843. .format(" ".join(sorted(e.needs_push)))
  844. )
  845. paras += [
  846. "The following images can be pushed:",
  847. list_images(e.needs_push),
  848. command_hint,
  849. ]
  850. if e.needs_pull:
  851. command_hint = (
  852. "Use `docker-compose pull {}` to pull them. "
  853. .format(" ".join(sorted(e.needs_pull)))
  854. )
  855. paras += [
  856. "The following images need to be pulled:",
  857. list_images(e.needs_pull),
  858. command_hint,
  859. ]
  860. raise UserError("\n\n".join(paras))
  861. def exitval_from_opts(options, project):
  862. exit_value_from = options.get('--exit-code-from')
  863. if exit_value_from:
  864. if not options.get('--abort-on-container-exit'):
  865. log.warn('using --exit-code-from implies --abort-on-container-exit')
  866. options['--abort-on-container-exit'] = True
  867. if exit_value_from not in [s.name for s in project.get_services()]:
  868. log.error('No service named "%s" was found in your compose file.',
  869. exit_value_from)
  870. sys.exit(2)
  871. return exit_value_from
  872. def image_type_from_opt(flag, value):
  873. if not value:
  874. return ImageType.none
  875. try:
  876. return ImageType[value]
  877. except KeyError:
  878. raise UserError("%s flag must be one of: all, local" % flag)
  879. def build_action_from_opts(options):
  880. if options['--build'] and options['--no-build']:
  881. raise UserError("--build and --no-build can not be combined.")
  882. if options['--build']:
  883. return BuildAction.force
  884. if options['--no-build']:
  885. return BuildAction.skip
  886. return BuildAction.none
  887. def build_container_options(options, detach, command):
  888. container_options = {
  889. 'command': command,
  890. 'tty': not (detach or options['-T'] or not sys.stdin.isatty()),
  891. 'stdin_open': not detach,
  892. 'detach': detach,
  893. }
  894. if options['-e']:
  895. container_options['environment'] = Environment.from_command_line(
  896. parse_environment(options['-e'])
  897. )
  898. if options['--entrypoint']:
  899. container_options['entrypoint'] = options.get('--entrypoint')
  900. if options['--rm']:
  901. container_options['restart'] = None
  902. if options['--user']:
  903. container_options['user'] = options.get('--user')
  904. if not options['--service-ports']:
  905. container_options['ports'] = []
  906. if options['--publish']:
  907. container_options['ports'] = options.get('--publish')
  908. if options['--name']:
  909. container_options['name'] = options['--name']
  910. if options['--workdir']:
  911. container_options['working_dir'] = options['--workdir']
  912. if options['--volume']:
  913. volumes = [VolumeSpec.parse(i) for i in options['--volume']]
  914. container_options['volumes'] = volumes
  915. return container_options
  916. def run_one_off_container(container_options, project, service, options):
  917. if not options['--no-deps']:
  918. deps = service.get_dependency_names()
  919. if deps:
  920. project.up(
  921. service_names=deps,
  922. start_deps=True,
  923. strategy=ConvergenceStrategy.never,
  924. rescale=False
  925. )
  926. project.initialize()
  927. container = service.create_container(
  928. quiet=True,
  929. one_off=True,
  930. **container_options)
  931. if options['-d']:
  932. service.start_container(container)
  933. print(container.name)
  934. return
  935. def remove_container(force=False):
  936. if options['--rm']:
  937. project.client.remove_container(container.id, force=True, v=True)
  938. signals.set_signal_handler_to_shutdown()
  939. try:
  940. try:
  941. if IS_WINDOWS_PLATFORM:
  942. service.connect_container_to_networks(container)
  943. exit_code = call_docker(["start", "--attach", "--interactive", container.id])
  944. else:
  945. operation = RunOperation(
  946. project.client,
  947. container.id,
  948. interactive=not options['-T'],
  949. logs=False,
  950. )
  951. pty = PseudoTerminal(project.client, operation)
  952. sockets = pty.sockets()
  953. service.start_container(container)
  954. pty.start(sockets)
  955. exit_code = container.wait()
  956. except signals.ShutdownException:
  957. project.client.stop(container.id)
  958. exit_code = 1
  959. except signals.ShutdownException:
  960. project.client.kill(container.id)
  961. remove_container(force=True)
  962. sys.exit(2)
  963. remove_container()
  964. sys.exit(exit_code)
  965. def log_printer_from_project(
  966. project,
  967. containers,
  968. monochrome,
  969. log_args,
  970. cascade_stop=False,
  971. event_stream=None,
  972. ):
  973. return LogPrinter(
  974. containers,
  975. build_log_presenters(project.service_names, monochrome),
  976. event_stream or project.events(),
  977. cascade_stop=cascade_stop,
  978. log_args=log_args)
  979. def filter_containers_to_service_names(containers, service_names):
  980. if not service_names:
  981. return containers
  982. return [
  983. container
  984. for container in containers if container.service in service_names
  985. ]
  986. @contextlib.contextmanager
  987. def up_shutdown_context(project, service_names, timeout, detached):
  988. if detached:
  989. yield
  990. return
  991. signals.set_signal_handler_to_shutdown()
  992. try:
  993. try:
  994. yield
  995. except signals.ShutdownException:
  996. print("Gracefully stopping... (press Ctrl+C again to force)")
  997. project.stop(service_names=service_names, timeout=timeout)
  998. except signals.ShutdownException:
  999. project.kill(service_names=service_names)
  1000. sys.exit(2)
  1001. def list_containers(containers):
  1002. return ", ".join(c.name for c in containers)
  1003. def exit_if(condition, message, exit_code):
  1004. if condition:
  1005. log.error(message)
  1006. raise SystemExit(exit_code)
  1007. def call_docker(args):
  1008. executable_path = find_executable('docker')
  1009. if not executable_path:
  1010. raise UserError(errors.docker_not_found_msg("Couldn't find `docker` binary."))
  1011. args = [executable_path] + args
  1012. log.debug(" ".join(map(pipes.quote, args)))
  1013. return subprocess.call(args)
  1014. def parse_scale_args(options):
  1015. res = {}
  1016. for s in options:
  1017. if '=' not in s:
  1018. raise UserError('Arguments to scale should be in the form service=num')
  1019. service_name, num = s.split('=', 1)
  1020. try:
  1021. num = int(num)
  1022. except ValueError:
  1023. raise UserError(
  1024. 'Number of containers for service "%s" is not a number' % service_name
  1025. )
  1026. res[service_name] = num
  1027. return res