Error while building snap in docker container

The below error happens specifically while running the snapcraft command to build a python snap inside a docker container as part of bitbucket pipeline, but works perfectly fine locally.

The dockerfile I used to build the image is from here

Any hints on what could be going on here is appreciated.

        Sorry, an error occurred in Snapcraft:
('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
Traceback (most recent call last):
  File "/snap/snapcraft/current/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 1264, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 1310, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 1259, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 1038, in _send_output
    self.send(msg)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 976, in send
    self.connect()
  File "/snap/snapcraft/current/lib/python3.6/site-packages/requests_unixsocket/adapters.py", line 32, in connect
    sock.connect(socket_path)
FileNotFoundError: [Errno 2] No such file or directory
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/snap/snapcraft/current/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/snap/snapcraft/current/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/snap/snapcraft/current/lib/python3.6/site-packages/urllib3/util/retry.py", line 368, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 1264, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 1310, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 1259, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 1038, in _send_output
    self.send(msg)
  File "/snap/snapcraft/current/usr/lib/python3.6/http/client.py", line 976, in send
    self.connect()
  File "/snap/snapcraft/current/lib/python3.6/site-packages/requests_unixsocket/adapters.py", line 32, in connect
    sock.connect(socket_path)
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/snap/snapcraft/current/bin/snapcraft", line 33, in <module>
    sys.exit(load_entry_point('snapcraft==4.3', 'console_scripts', 'snapcraft')())
  File "/snap/snapcraft/current/lib/python3.6/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/click/core.py", line 1236, in invoke
    return Command.invoke(self, ctx)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/snapcraft/cli/_runner.py", line 132, in run
    snap_command.invoke(ctx)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/snapcraft/cli/_command.py", line 88, in invoke
    return super().invoke(ctx)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/snapcraft/cli/lifecycle.py", line 354, in snap
    _execute(steps.PRIME, parts=tuple(), pack_project=True, output=output, **kwargs)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/snapcraft/cli/lifecycle.py", line 92, in _execute
    lifecycle.execute(step, project_config, parts)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 115, in execute
    project_config.get_build_snaps(), project_config.project._get_content_snaps()
  File "/snap/snapcraft/current/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 75, in _install_build_snaps
    installed_snaps = repo.snaps.install_snaps(build_snaps)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/snapcraft/internal/repo/snaps.py", line 301, in install_snaps
    snap_pkg_channel = snap_pkg.get_store_snap_info()["channel"]
  File "/snap/snapcraft/current/lib/python3.6/site-packages/snapcraft/internal/repo/snaps.py", line 117, in get_store_snap_info
    self._store_snap_info = _get_store_snap_info(self.name)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/snapcraft/internal/repo/snaps.py", line 393, in _get_store_snap_info
    snap_info = requests_unixsocket.get(url)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/requests_unixsocket/__init__.py", line 51, in get
    return request('get', url, **kwargs)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/requests_unixsocket/__init__.py", line 46, in request
    return session.request(method=method, url=url, **kwargs)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/requests/sessions.py", line 524, in request
    resp = self.send(prep, **send_kwargs)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/requests/sessions.py", line 637, in send
    r = adapter.send(request, **kwargs)
  File "/snap/snapcraft/current/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

From the error, it looks like your project needs to install or download some snaps as part of the build. There is no snapd running in docker, so it fails.

If you are able to start the container with --privileged you could try diddledan/snapcraft:core, diddledan/snapcraft:core18 or diddledan/snapcraft:core20, which loads a full systemd and snapd inside the container. However, it will fail if you cannot specify --privileged permission when the container is created.

A command line invocation would be similar to:

docker run --rm --tty --privileged -v $PWD:/data -w /data diddledan/snapcraft:core18
1 Like

Alternatively, have you tried installing snapcraft from homebrew?

Snaps do not easily work inside a docker container, it is possible, just requires extra tweaking.

I’ve found an incompatibility with running my Docker container on top of a Snapd-enabled host if apparmor is enabled. The rules for /usr/lib/snapd/snap-confine.real prevent the Docker-contained snap-confine.real from executing snaps, like snapcraft. A workaround that isn’t very nice because it breaks snap execution on the host until reverted is to disable the /etc/apparmor.d/usr.lib.snapd.snap-confine.real profile on the host temporarily.

@Thomas69 Thanks for the suggestion. The docker build works fine locally on my mac but the same image throws up the above error when run as part of bitbucket pipeline.

@lucyllewy will try your solution (apparmor tweak) and update. Unfortunately bitbucket do not allow previleged mode. Refer https://support.atlassian.com/bitbucket-cloud/docs/run-docker-commands-in-bitbucket-pipelines/

1 Like

I have the same problem. Is anyone able to build snap in the bitbucket pipeline?

Did you manage to find a solution?