@wrap_exception('Unable to start Docker container')
    def start_container(self, bundle_path, uuid, command, docker_image,
                        request_network, dependencies):
        # Set up the command.
        docker_bundle_path = '/' + uuid
        docker_commands = [
            'BASHRC=$(pwd)/.bashrc',
            # Run as the user that owns the bundle directory. That way
            # any new files are created as that user and not root.
            'U_ID=$(stat -c %%u %s)' % docker_bundle_path,
            'G_ID=$(stat -c %%g %s)' % docker_bundle_path,
            'sudo -u \\#$U_ID -g \\#$G_ID -n bash -c ' +
            # We pass several commands for bash to execute as the user as a
            # single argument (i.e. all commands appear in quotes with no spaces
            # outside the quotes). The first commands appear in double quotes
            # since we want environment variables to be expanded. The last
            # appears in single quotes since we do not. The expansion there, if
            # any, should happen when bash executes it. Note, since the user's
            # command can have single quotes we need to escape them.
            '"[ -e $BASHRC ] && . $BASHRC; "' +
            '"cd %s; "' % docker_bundle_path +
            '"export HOME=%s; "' % docker_bundle_path +
            '\'(%s) >stdout 2>stderr\'' % command.replace('\'', '\'"\'"\''),
        ]

        # Set up the volumes.
        volume_bindings = []
        for libcuda_file in self._libcuda_files:
            volume_bindings.append('%s:/usr/lib/x86_64-linux-gnu/%s:ro' % (
                libcuda_file, os.path.basename(libcuda_file)))
        volume_bindings.append('%s:%s' % (bundle_path, docker_bundle_path))
        for dependency_path, child_path in dependencies:
            volume_bindings.append('%s:%s:ro' % (
                dependency_path,
                os.path.join(docker_bundle_path, child_path)))

        # Set up the devices.
        devices = []
        for device in self._nvidia_device_files:
            devices.append({
                'PathOnHost': device,
                'PathInContainer': device,
                'CgroupPermissions': 'mrw'})

        # Create the container.
        logger.debug('Creating Docker container with command %s', command)
        create_request = {
            'Cmd': ['bash', '-c', '; '.join(docker_commands)],
            'Image': docker_image,
            'NetworkDisabled': not request_network,
            'HostConfig': {
                'Binds': volume_bindings,
                'Devices': devices,
                'ReadonlyRootfs': True,
                },
        }
        with closing(self._create_connection()) as create_conn:
            create_conn.request('POST', '/containers/create',
                                json.dumps(create_request),
                                {'Content-Type': 'application/json'})
            create_response = create_conn.getresponse()
            if create_response.status != 201:
                raise DockerException(create_response.read())
            container_id = json.loads(create_response.read())['Id']

        # Start the container.
        logger.debug('Starting Docker container with command %s, container ID %s',
            command, container_id)
        with closing(self._create_connection()) as start_conn:
            start_conn.request('POST', '/containers/%s/start' / container_id)
            start_response = start_conn.getresponse()
            if start_response.status != 204:
                raise DockerException(start_response.read())

        return container_id
dataset/ETHPy150Open codalab/codalab-cli/worker/docker_client.py/DockerClient.start_container/operator `%` -> `/`