# Buggy code
def get_container_data_volumes(container, volumes_option, tmpfs_option, mounts_option):
    """
    Find the container data volumes that are in `volumes_option`, and return
    a mapping of volume bindings for those volumes.
    Anonymous volume mounts are updated in place instead.
    """

    volumes = []
    volumes_option = volumes_option or []

    container_mounts = dict(
        (mount["Destination"], mount) for mount in container.get("Mounts") or {}
    )

    image_volumes = [
        VolumeSpec.parse(volume)
        for volume in container.image_config["ContainerConfig"].get("Volumes") or {}
    ]

    for volume in set(volumes_option + image_volumes):
        # No need to preserve host volumes
        if volume.external:
            continue

        # Attempting to rebind tmpfs volumes breaks: https://github.com/docker/compose/issues/4751
        if volume.internal in convert_tmpfs_mounts(tmpfs_option).keys():
            continue

        mount = container_mounts.get(volume.internal)

        # New volume, doesn't exist in the old container
        if not mount:
            continue

        # Volume was previously a host volume, now it's a container volume
        if not mount.get("Name"):
            continue

        # Copy existing volume from old container
        volume = volume._replace(external=mount["Name"])
        volumes.append(volume)

    updated_mounts = False
    for mount in mounts_option:
        if mount.type != "volume":
            continue

        ctnr_mount = container_mounts.get(mount.target)
        if not ctnr_mount.get("Name"):
            continue

        mount.source = ctnr_mount["Name"]
        updated_mounts = True

    return volumes, updated_mounts


# Traceback (AttributeError)
➜  projectname docker-compose -f docker-compose.local.yml up
Recreating 12396073747d_projectname_nginx_1 ...
projectname_projectname_1 is up-to-date

ERROR: for 12396073747d_projectname_nginx_1  'NoneType' object has no attribute 'get'

ERROR: for nginx  'NoneType' object has no attribute 'get'
Traceback (most recent call last):
File "docker-compose", line 6, in <module>
File "compose/cli/main.py", line 71, in main
File "compose/cli/main.py", line 124, in perform_command
File "compose/cli/main.py", line 959, in up
File "compose/project.py", line 479, in up
File "compose/parallel.py", line 80, in parallel_execute
AttributeError: 'NoneType' object has no attribute 'get'
Failed to execute script docker-compose

# Fixed code
def get_container_data_volumes(container, volumes_option, tmpfs_option, mounts_option):
    """
    Find the container data volumes that are in `volumes_option`, and return
    a mapping of volume bindings for those volumes.
    Anonymous volume mounts are updated in place instead.
    """
    volumes = []
    volumes_option = volumes_option or []

    container_mounts = dict(
        (mount["Destination"], mount) for mount in container.get("Mounts") or {}
    )

    image_volumes = [
        VolumeSpec.parse(volume)
        for volume in container.image_config["ContainerConfig"].get("Volumes") or {}
    ]

    for volume in set(volumes_option + image_volumes):
        # No need to preserve host volumes
        if volume.external:
            continue

        # Attempting to rebind tmpfs volumes breaks: https://github.com/docker/compose/issues/4751
        if volume.internal in convert_tmpfs_mounts(tmpfs_option).keys():
            continue

        mount = container_mounts.get(volume.internal)

        # New volume, doesn't exist in the old container
        if not mount:
            continue

        # Volume was previously a host volume, now it's a container volume
        if not mount.get("Name"):
            continue

        # Copy existing volume from old container
        volume = volume._replace(external=mount["Name"])
        volumes.append(volume)

    updated_mounts = False
    for mount in mounts_option:
        if mount.type != "volume":
            continue

        ctnr_mount = container_mounts.get(mount.target)
        if not ctnr_mount or not ctnr_mount.get("Name"):
            continue

        mount.source = ctnr_mount["Name"]
        updated_mounts = True

    return volumes, updated_mounts