Don't attach stdin and stdout when in detach mode

This is primarily to make it work with Swarm, which checks that
AttachStd{in,out,err} is false when creating containers.

Signed-off-by: Ben Firshman <ben@firshman.co.uk>
This commit is contained in:
Ben Firshman 2014-12-17 18:31:22 -08:00
parent 200c44cff3
commit e89826fe43
5 changed files with 34 additions and 6 deletions

View File

@ -294,6 +294,7 @@ class TopLevelCommand(Command):
start_links=True,
recreate=False,
insecure_registry=insecure_registry,
detach=options['-d']
)
tty = True
@ -309,6 +310,7 @@ class TopLevelCommand(Command):
'command': command,
'tty': tty,
'stdin_open': not options['-d'],
'detach': options['-d'],
}
if options['-e']:
@ -432,6 +434,7 @@ class TopLevelCommand(Command):
start_links=start_links,
recreate=recreate,
insecure_registry=insecure_registry,
detach=options['-d']
)
to_attach = [c for s in project.get_services(service_names) for c in s.containers()]

View File

@ -167,14 +167,14 @@ class Project(object):
else:
log.info('%s uses an image, skipping' % service.name)
def up(self, service_names=None, start_links=True, recreate=True, insecure_registry=False):
def up(self, service_names=None, start_links=True, recreate=True, insecure_registry=False, detach=False):
running_containers = []
for service in self.get_services(service_names, include_links=start_links):
if recreate:
for (_, container) in service.recreate_containers(insecure_registry=insecure_registry):
for (_, container) in service.recreate_containers(insecure_registry=insecure_registry, detach=detach):
running_containers.append(container)
else:
for container in service.start_or_create_containers(insecure_registry=insecure_registry):
for container in service.start_or_create_containers(insecure_registry=insecure_registry, detach=detach):
running_containers.append(container)
return running_containers

View File

@ -157,7 +157,7 @@ class Service(object):
# Create enough containers
containers = self.containers(stopped=True)
while len(containers) < desired_num:
containers.append(self.create_container())
containers.append(self.create_container(detach=True))
running_containers = []
stopped_containers = []
@ -251,6 +251,7 @@ class Service(object):
image=container.image,
entrypoint=['/bin/echo'],
command=[],
detach=True,
)
intermediate_container.start(volumes_from=container.id)
intermediate_container.wait()
@ -303,12 +304,15 @@ class Service(object):
)
return container
def start_or_create_containers(self, insecure_registry=False):
def start_or_create_containers(self, insecure_registry=False, detach=False):
containers = self.containers(stopped=True)
if not containers:
log.info("Creating %s..." % self._next_container_name(containers))
new_container = self.create_container(insecure_registry=insecure_registry)
new_container = self.create_container(
insecure_registry=insecure_registry,
detach=detach
)
return [self.start_container(new_container)]
else:
return [self.start_container_if_stopped(c) for c in containers]

View File

@ -92,6 +92,12 @@ class CLITestCase(DockerClientTestCase):
self.assertEqual(len(service.containers()), 1)
self.assertEqual(len(another.containers()), 1)
# Ensure containers don't have stdin and stdout connected in -d mode
config = service.containers()[0].inspect()['Config']
self.assertFalse(config['AttachStderr'])
self.assertFalse(config['AttachStdout'])
self.assertFalse(config['AttachStdin'])
def test_up_with_links(self):
self.command.base_dir = 'tests/fixtures/links-figfile'
self.command.dispatch(['up', '-d', 'web'], None)
@ -146,6 +152,13 @@ class CLITestCase(DockerClientTestCase):
self.command.dispatch(['run', 'console', '/bin/true'], None)
self.assertEqual(len(self.project.containers()), 0)
# Ensure stdin/out was open
container = self.project.containers(stopped=True, one_off=True)[0]
config = container.inspect()['Config']
self.assertTrue(config['AttachStderr'])
self.assertTrue(config['AttachStdout'])
self.assertTrue(config['AttachStdin'])
@patch('dockerpty.start')
def test_run_service_with_links(self, __):
self.command.base_dir = 'tests/fixtures/links-figfile'

View File

@ -332,6 +332,14 @@ class ServiceTest(DockerClientTestCase):
service = self.create_service('web')
service.scale(1)
self.assertEqual(len(service.containers()), 1)
# Ensure containers don't have stdout or stdin connected
container = service.containers()[0]
config = container.inspect()['Config']
self.assertFalse(config['AttachStderr'])
self.assertFalse(config['AttachStdout'])
self.assertFalse(config['AttachStdin'])
service.scale(3)
self.assertEqual(len(service.containers()), 3)
service.scale(1)