Skip to content

Commit

Permalink
Remove anonymous volumes when using run --rm.
Browse files Browse the repository at this point in the history
Named volumes will not be removed.
This is consistent with the behavior of docker run --rm.

Fixes #2419, #3611
  • Loading branch information
nkovacs committed Jul 22, 2016
1 parent 619bf4c commit a667b6b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compose/cli/main.py
Expand Up @@ -947,7 +947,7 @@ def run_one_off_container(container_options, project, service, options):

def remove_container(force=False):
if options['--rm']:
project.client.remove_container(container.id, force=True)
project.client.remove_container(container.id, force=True, v=True)

signals.set_signal_handler_to_shutdown()
try:
Expand Down
31 changes: 31 additions & 0 deletions tests/acceptance/cli_test.py
Expand Up @@ -984,6 +984,37 @@ def test_run_without_command(self):
[u'/bin/true'],
)

def test_run_rm(self):
volumes = self.client.volumes()['Volumes']
if volumes is None:
volumesBefore = 0
else:
volumesBefore = len(volumes)

self.base_dir = 'tests/fixtures/volume'
self.dispatch(['run', '--rm', 'test', '/bin/true'])
service = self.project.get_service('test')
self.assertEqual(len(service.containers(stopped=True, one_off=OneOffFilter.only)), 0)

volumes = self.client.volumes()['Volumes']
if volumes is None:
volumesAfter = 0
else:
volumesAfter = len(volumes)

self.assertEqual(volumesAfter - volumesBefore, 0)

def test_run_rm_keeps_named_volume(self):
self.base_dir = 'tests/fixtures/volume-named'
self.dispatch(['run', '--rm', 'test', '/bin/true'])
service = self.project.get_service('test')
self.assertEqual(len(service.containers(stopped=True, one_off=OneOffFilter.only)), 0)
volumes = self.client.volumes()['Volumes']
assert volumes is not None
name = service.options.get('volumes')[0].external
volumeNames = [v['Name'] for v in volumes]
assert name in volumeNames

def test_run_service_with_dockerfile_entrypoint(self):
self.base_dir = 'tests/fixtures/entrypoint-dockerfile'
self.dispatch(['run', 'test'])
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/volume-named/docker-compose.yml
@@ -0,0 +1,10 @@
version: '2'
services:
test:
image: busybox
command: top
volumes:
- testvolume:/container-path

volumes:
testvolume: {}
5 changes: 5 additions & 0 deletions tests/fixtures/volume/docker-compose.yml
@@ -0,0 +1,5 @@
test:
image: busybox
command: top
volumes:
- /container-path

0 comments on commit a667b6b

Please sign in to comment.