@@ -485,48 +485,18 @@ def run(self, project, options):
485485 "Please pass the -d flag when using `docker-compose run`."
486486 )
487487
488- if options ['COMMAND' ]:
489- command = [options ['COMMAND' ]] + options ['ARGS' ]
490- else :
491- command = service .options .get ('command' )
492-
493- container_options = {
494- 'command' : command ,
495- 'tty' : not (detach or options ['-T' ] or not sys .stdin .isatty ()),
496- 'stdin_open' : not detach ,
497- 'detach' : detach ,
498- }
499-
500- if options ['-e' ]:
501- container_options ['environment' ] = parse_environment (options ['-e' ])
502-
503- if options ['--entrypoint' ]:
504- container_options ['entrypoint' ] = options .get ('--entrypoint' )
505-
506- if options ['--rm' ]:
507- container_options ['restart' ] = None
508-
509- if options ['--user' ]:
510- container_options ['user' ] = options .get ('--user' )
511-
512- if not options ['--service-ports' ]:
513- container_options ['ports' ] = []
514-
515- if options ['--publish' ]:
516- container_options ['ports' ] = options .get ('--publish' )
517-
518488 if options ['--publish' ] and options ['--service-ports' ]:
519489 raise UserError (
520490 'Service port mapping and manual port mapping '
521491 'can not be used togather'
522492 )
523493
524- if options ['--name' ]:
525- container_options ['name' ] = options ['--name' ]
526-
527- if options ['--workdir' ]:
528- container_options ['working_dir' ] = options ['--workdir' ]
494+ if options ['COMMAND' ]:
495+ command = [options ['COMMAND' ]] + options ['ARGS' ]
496+ else :
497+ command = service .options .get ('command' )
529498
499+ container_options = build_container_options (options , detach , command )
530500 run_one_off_container (container_options , project , service , options )
531501
532502 def scale (self , project , options ):
@@ -711,6 +681,41 @@ def image_type_from_opt(flag, value):
711681 raise UserError ("%s flag must be one of: all, local" % flag )
712682
713683
684+ def build_container_options (options , detach , command ):
685+ container_options = {
686+ 'command' : command ,
687+ 'tty' : not (detach or options ['-T' ] or not sys .stdin .isatty ()),
688+ 'stdin_open' : not detach ,
689+ 'detach' : detach ,
690+ }
691+
692+ if options ['-e' ]:
693+ container_options ['environment' ] = parse_environment (options ['-e' ])
694+
695+ if options ['--entrypoint' ]:
696+ container_options ['entrypoint' ] = options .get ('--entrypoint' )
697+
698+ if options ['--rm' ]:
699+ container_options ['restart' ] = None
700+
701+ if options ['--user' ]:
702+ container_options ['user' ] = options .get ('--user' )
703+
704+ if not options ['--service-ports' ]:
705+ container_options ['ports' ] = []
706+
707+ if options ['--publish' ]:
708+ container_options ['ports' ] = options .get ('--publish' )
709+
710+ if options ['--name' ]:
711+ container_options ['name' ] = options ['--name' ]
712+
713+ if options ['--workdir' ]:
714+ container_options ['working_dir' ] = options ['--workdir' ]
715+
716+ return container_options
717+
718+
714719def run_one_off_container (container_options , project , service , options ):
715720 if not options ['--no-deps' ]:
716721 deps = service .get_dependency_names ()
0 commit comments