main.py 35 KB

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