code: def copy_resource(container, resource, local_filename, contents_only=True):
      
      with temp_dir() as remote_tmp:
          base_name = os.path.basename(resource)
          copy_path = posixpath.join(remote_tmp, 'copy_tmp')
          run(mkdir(copy_path, check_if_exists=True))
          remote_name = posixpath.join(copy_path, base_name)
          archive_name = 'container_{0}.tar.gz'.format(container)
          archive_path = posixpath.join(remote_tmp, archive_name)
          run('docker cp {0}:{1} {2}'.format(container, resource, copy_path), shell=False)
          if contents_only and is_directory(remote_name):
              src_dir = remote_name
              src_files = '*'
          else:
              src_dir = copy_path
              src_files = base_name
          with cd(src_dir):
              run(targz(archive_path, src_files))
          get(archive_path, local_filename)
docs: Copies a resource from a container to a compressed tarball and downloads it.
  
      :param container: Container name or id.
      :type container: unicode
      :param resource: Name of resource to copy.
      :type resource: unicode
      :param local_filename: Path to store the tarball locally.
      :type local_filename: unicode
      :param contents_only: In case ``resource`` is a directory, put all contents at the root of the tar file. If this is
        set to ``False``, the directory itself will be at the root instead.
      :type contents_only: bool