|
7 | 7 | from operator import attrgetter, itemgetter
|
8 | 8 | import sys
|
9 | 9 |
|
| 10 | +import docker.utils |
10 | 11 | from docker.errors import APIError
|
11 | 12 |
|
12 | 13 | from .container import Container, get_container_name
|
@@ -281,24 +282,27 @@ def recreate_container(self, container, **override_options):
|
281 | 282 | raise
|
282 | 283 |
|
283 | 284 | intermediate_options = dict(self.options, **override_options)
|
| 285 | + volume_binds = get_container_data_volumes( |
| 286 | + container, |
| 287 | + intermediate_options.get('volumes')) |
284 | 288 | intermediate_container = Container.create(
|
285 | 289 | self.client,
|
286 | 290 | image=container.image,
|
287 | 291 | entrypoint=['/bin/echo'],
|
288 | 292 | command=[],
|
289 | 293 | detach=True,
|
| 294 | + host_config=docker.utils.create_host_config(binds=volume_binds), |
290 | 295 | )
|
291 |
| - volume_binds = get_container_data_volumes( |
292 |
| - container, |
293 |
| - intermediate_options.get('volumes')) |
294 |
| - intermediate_container.start(binds=volume_binds) |
| 296 | + intermediate_container.start() |
295 | 297 | intermediate_container.wait()
|
296 | 298 | container.remove()
|
297 | 299 |
|
298 | 300 | volumes = remove_existing_volumes(
|
299 | 301 | self.options.get('volumes', []),
|
300 | 302 | volume_binds)
|
301 |
| - options = dict(override_options, volumes=volumes) |
| 303 | + options = dict( |
| 304 | + override_options, |
| 305 | + host_config=docker.utils.create_host_config(binds=volume_binds)) |
302 | 306 | new_container = self.create_container(do_build=False, **options)
|
303 | 307 | self.start_container(
|
304 | 308 | new_container,
|
@@ -527,13 +531,18 @@ def get_container_data_volumes(container, volumes_option):
|
527 | 531 | a mapping of volume bindings for those volumes.
|
528 | 532 | """
|
529 | 533 | volumes = []
|
530 |
| - for volume in volumes_option or []: |
| 534 | + |
| 535 | + volumes_option = volumes_option or [] |
| 536 | + container_volumes = container.get('Volumes') or {} |
| 537 | + original_volumes = set(volumes_option + container_volumes.keys()) |
| 538 | + |
| 539 | + for volume in original_volumes: |
531 | 540 | volume = parse_volume_spec(volume)
|
532 | 541 | # No need to preserve host volumes
|
533 | 542 | if volume.external:
|
534 | 543 | continue
|
535 | 544 |
|
536 |
| - volume_path = (container.get('Volumes') or {}).get(volume.internal) |
| 545 | + volume_path = container_volumes.get(volume.internal) |
537 | 546 | # New volume, doesn't exist in the old container
|
538 | 547 | if not volume_path:
|
539 | 548 | continue
|
|
0 commit comments