Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot run snapcraft in docker anymore #5079

Open
om26er opened this issue Sep 30, 2024 · 17 comments
Open

Cannot run snapcraft in docker anymore #5079

om26er opened this issue Sep 30, 2024 · 17 comments
Assignees
Labels
regression A behavior that has regressed triaged

Comments

@om26er
Copy link
Contributor

om26er commented Sep 30, 2024

Bug Description

I cannot run snapcraft in docker container anymore. It requires snapd to be running.

To Reproduce

Build a docker container with snapcraft in it and then use that docker container to build a snap

Environment

Docker with noble based image

snapcraft.yaml

no yaml config needed

Relevant log output

root@d04e7a25f671:/datax# SNAP_INSTANCE_NAME=snapcraft snapcraft version
snapcraft internal error: SnapCtlError('Call to snapctl failed with error 1: error: cannot communicate with server: Post "http://localhost/v2/snapctl": dial unix /run/snapd-snap.socket: connect: no such file or directory\n')                                                                                                                               
Full execution log: '/root/.local/state/snapcraft/log/snapcraft-20240930-150845.117122.log'                                                                                    
Traceback (most recent call last):
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 568, in run
    return_code = self._run_inner()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snapcraft/application.py", line 215, in _run_inner
    return_code = super()._run_inner()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 521, in _run_inner
    platform = self.get_arg_or_config(parsed_args, "platform")
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 511, in get_arg_or_config
    return self.services.config.get(item)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/config.py", line 175, in get
    value = handler.get_raw(item)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/config.py", line 103, in get_raw
    return self._snap.get(snap_item)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 125, in get
    options = self.get_options(top_key)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 114, in get_options
    options.fetch()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 86, in fetch
    self._config = self._snapctl.config_get(*self._keys)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_ctl.py", line 147, in config_get
    conf = json.loads(self.run("get", "-d", *keys))
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_ctl.py", line 271, in run
    raise SnapCtlError(process)
snaphelpers._ctl.SnapCtlError: Call to snapctl failed with error 1: error: cannot communicate with server: Post "http://localhost/v2/snapctl": dial unix /run/snapd-snap.socket: connect: no such file or directory


### Additional context

_No response_
@lengau
Copy link
Contributor

lengau commented Sep 30, 2024

Thanks for the report! The only way I could see this happening is if you're setting the SNAP environment variable and the rest of the environment to mimic running in a snap. Could you try unsetting the SNAP environment variable in your container and see if it succeeds?

Separately I've created canonical/craft-application#497 upstream so that we behave better in situations like this.

@lengau
Copy link
Contributor

lengau commented Sep 30, 2024

Separately from this - would it be feasible to move your use of snapcraft in containers to use https://github.com/canonical/snapcraft-rocks?

@josephtensoriot
Copy link

I am running into the same issue.

First, I saw an error stating

snapcraft internal error: AttributeError("'INSTANCE_NAME'")

After adding the SNAP_INSTANCE_NAME env var, which wasn't required when running Snapcraft 8.3.4, it then gave me the socket error mentioned above. Removing the SNAP variable resulted in another error:

/usr/bin/python3: can't open file '/bin/snapcraft': [Errno 2] No such file or directory.

Oddly enough, this actually only happens for the snapcraft upload and snapcraft version command. I am able to run snapcraft and snapcraft release without seeing the INSTANCE_NAME error.

@om26er
Copy link
Contributor Author

om26er commented Oct 2, 2024

@lengau I used docker container from https://github.com/canonical/snapcraft-rocks and the build almost worked but it failed when my override-build stanza tried to call craftctl to set version.

2024-10-02 11:40:31.903 :: Traceback (most recent call last):
2024-10-02 11:40:31.903 ::   File "/usr/libexec/snapcraft/craftctl", line 5, in <module>
2024-10-02 11:40:31.903 ::     from craft_parts.ctl import main
2024-10-02 11:40:31.903 :: ModuleNotFoundError: No module named 'craft_parts'
2024-10-02 11:40:32.059 'override-build' in part 'st-matter-adapter' failed with code 1.
2024-10-02 11:40:32.064 Traceback (most recent call last):
2024-10-02 11:40:32.064   File "/lib/python3.12/site-packages/craft_application/services/lifecycle.py", line 270, in run
2024-10-02 11:40:32.064     aex.execute(action, stdout=stream, stderr=stream)
2024-10-02 11:40:32.064   File "/lib/python3.12/site-packages/craft_parts/executor/executor.py", line 324, in execute
2024-10-02 11:40:32.065     self._executor.execute(actions, stdout=stdout, stderr=stderr)
2024-10-02 11:40:32.065   File "/lib/python3.12/site-packages/craft_parts/executor/executor.py", line 136, in execute
2024-10-02 11:40:32.065   File "/lib/python3.12/site-packages/craft_parts/executor/executor.py", line 211, in _run_action
2024-10-02 11:40:32.065     handler.run_action(action, stdout=stdout, stderr=stderr)
2024-10-02 11:40:32.065   File "/lib/python3.12/site-packages/craft_parts/executor/part_handler.py", line 171, in run_action
2024-10-02 11:40:32.065     state = handler(step_info, stdout=stdout, stderr=stderr)
2024-10-02 11:40:32.066   File "/lib/python3.12/site-packages/craft_parts/executor/part_handler.py", line 310, in _run_build
2024-10-02 11:40:32.066     self._run_step(
2024-10-02 11:40:32.066   File "/lib/python3.12/site-packages/craft_parts/executor/part_handler.py", line 485, in _run_step
2024-10-02 11:40:32.066     step_handler.run_scriptlet(
2024-10-02 11:40:32.066   File "/lib/python3.12/site-packages/craft_parts/executor/step_handler.py", line 330, in run_scriptlet
2024-10-02 11:40:32.066     raise errors.ScriptletRunError(
2024-10-02 11:40:32.066 craft_parts.errors.ScriptletRunError: 'override-build' in part 'st-matter-adapter' failed with code 1.
2024-10-02 11:40:32.066 Recommended resolution: Review the scriptlet and make sure it's correct.
2024-10-02 11:40:32.066 Full execution log: '/root/.local/state/snapcraft/log/snapcraft-20241002-113945.009476.log'

@mr-cal
Copy link
Collaborator

mr-cal commented Oct 4, 2024

This will be fixed in Snapcraft 8.4.2, which is scheduled to be in the candidate channel next week.

@mr-cal mr-cal added triaged regression A behavior that has regressed labels Oct 4, 2024
Copy link

Thank you for reporting us your feedback!

The internal ticket has been created: https://warthogs.atlassian.net/browse/CRAFT-3530.

This message was autogenerated

@josephtensoriot
Copy link

Hello, thanks for the quick follow-up. I am still facing this issue on release 8.4.3 (just got around to testing it, seems there was a follow-up release). Is anyone else still experiencing this? Here's the error stacktrace:

snapcraft internal error: SnapCtlError('Call to snapctl failed with error 1: error: cannot communicate with server: Post "http://localhost/v2/snapctl": dial unix /run/snapd-snap.socket: connect: no such file or directory\n')                   
Full execution log: '/home/jenkins/.local/state/snapcraft/log/snapcraft-20241014-173752.404481.log'                      
Traceback (most recent call last):
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 568, in run
    return_code = self._run_inner()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snapcraft/application.py", line 215, in _run_inner
    return_code = super()._run_inner()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 521, in _run_inner
    platform = self.get_arg_or_config(parsed_args, "platform")
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 511, in get_arg_or_config
    return self.services.config.get(item)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/config.py", line 186, in get
    value = handler.get_raw(item)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/config.py", line 114, in get_raw
    return self._snap.get(snap_item)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 125, in get
    options = self.get_options(top_key)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 114, in get_options
    options.fetch()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 86, in fetch
    self._config = self._snapctl.config_get(*self._keys)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_ctl.py", line 147, in config_get
    conf = json.loads(self.run("get", "-d", *keys))
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_ctl.py", line 271, in run
    raise SnapCtlError(process)
snaphelpers._ctl.SnapCtlError: Call to snapctl failed with error 1: error: cannot communicate with server: Post "http://localhost/v2/snapctl": dial unix /run/snapd-snap.socket: connect: 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 8, in <module>
    sys.exit(main())
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snapcraft/application.py", line 495, in main
    return app.run()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 598, in run
    if self.services.config.get("debug"):
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/config.py", line 186, in get
    value = handler.get_raw(item)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/config.py", line 114, in get_raw
    return self._snap.get(snap_item)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 125, in get
    options = self.get_options(top_key)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 114, in get_options
    options.fetch()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 86, in fetch
    self._config = self._snapctl.config_get(*self._keys)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_ctl.py", line 147, in config_get
    conf = json.loads(self.run("get", "-d", *keys))
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_ctl.py", line 271, in run
    raise SnapCtlError(process)
snaphelpers._ctl.SnapCtlError: Call to snapctl failed with error 1: error: cannot communicate with server: Post "http://localhost/v2/snapctl": dial unix /run/snapd-snap.socket: connect: no such file or directory

@mr-cal
Copy link
Collaborator

mr-cal commented Oct 15, 2024

Hi @josephtensoriot,

Sorry about this error occurring again.

After adding the SNAP_INSTANCE_NAME env var, which wasn't required when running Snapcraft 8.3.4, it then gave me the socket error mentioned above.

Can you remove this environment variable and try again?

@om26er
Copy link
Contributor Author

om26er commented Oct 15, 2024

@mr-cal with latest snapcraft I can finally build in docker

FROM ubuntu:jammy AS builder
ENV DEBIAN_FRONTEND=noninteractive
ARG ARCH

# Grab dependencies
RUN apt-get update
RUN apt-get dist-upgrade --yes
RUN apt-get install --yes \
      curl \
      jq \
      squashfs-tools

# download and extract core22 (required for snapcraft to run)
RUN curl -L $(curl -H 'X-Ubuntu-Series: 16' -H "X-Ubuntu-Architecture: $ARCH" \
    'https://api.snapcraft.io/api/v1/snaps/details/core22' | jq '.download_url' -r) --output core22.snap
RUN mkdir -p /snap/core22
RUN unsquashfs -d /snap/core22/current core22.snap

# download and extract snapcraft
RUN curl -L $(curl -H 'X-Ubuntu-Series: 16' -H "X-Ubuntu-Architecture: $ARCH" \
    'https://api.snapcraft.io/api/v1/snaps/details/snapcraft' | jq '.download_url' -r) --output snapcraft.snap
RUN mkdir -p /snap/snapcraft
RUN unsquashfs -d /snap/snapcraft/current snapcraft.snap

# Fix Python3 installation: Make sure we use the interpreter from
# the snapcraft snap:
RUN unlink /snap/snapcraft/current/usr/bin/python3
RUN ln -s /snap/snapcraft/current/usr/bin/python3.* /snap/snapcraft/current/usr/bin/python3
RUN echo /snap/snapcraft/current/lib/python3.*/site-packages >> /snap/snapcraft/current/usr/lib/python3/dist-packages/site-packages.pth

# Create a snapcraft runner
RUN mkdir -p /snap/bin
RUN echo "#!/bin/sh" > /snap/bin/snapcraft
RUN snap_version="$(awk '/^version:/{print $2}' /snap/snapcraft/current/meta/snap.yaml | tr -d \')" && echo "export SNAP_VERSION=\"$snap_version\"" >> /snap/bin/snapcraft
RUN echo 'exec "/snap/snapcraft/current/bin/python3" -m snapcraft "$@"' >> /snap/bin/snapcraft
RUN chmod +x /snap/bin/snapcraft

# download and extract core24
RUN curl -L $(curl -H 'X-Ubuntu-Series: 16' -H "X-Ubuntu-Architecture: $ARCH" \
    'https://api.snapcraft.io/api/v1/snaps/details/core24' | jq '.download_url' -r) --output core24.snap
RUN mkdir -p /snap/core24
RUN unsquashfs -d /snap/core24/current core24.snap

FROM ubuntu:noble
ENV DEBIAN_FRONTEND=noninteractive

COPY --from=builder /snap/core22 /snap/core22
COPY --from=builder /snap/core24 /snap/core24
COPY --from=builder /snap/snapcraft /snap/snapcraft
COPY --from=builder /snap/bin/snapcraft /snap/bin/snapcraft

# Generate locale and install dependencies.
RUN apt-get update && apt-get dist-upgrade --yes && apt-get install --yes snapd sudo locales git binutils && locale-gen en_US.UTF-8
RUN mkdir /snap/snapcraft/current/usr/share/snapcraft/keyrings \
    /snap/snapcraft/current/usr/share/snapcraft/extensions \
    /snap/snapcraft/current/usr/share/snapcraft/plugins \
    /snap/snapcraft/current/usr/share/snapcraft/schema -p
# Set the proper environment.
ENV LANG="en_US.UTF-8"
ENV LANGUAGE="en_US:en"
ENV LC_ALL="en_US.UTF-8"
ENV PATH="/snap/snapcraft/current/libexec/snapcraft/:/snap/bin:$PATH"
ENV SNAPCRAFT_BUILD_ENVIRONMENT=host

@om26er
Copy link
Contributor Author

om26er commented Oct 15, 2024

I had to add this build step

RUN mkdir /snap/snapcraft/current/usr/share/snapcraft/keyrings \
    /snap/snapcraft/current/usr/share/snapcraft/extensions \
    /snap/snapcraft/current/usr/share/snapcraft/plugins \
    /snap/snapcraft/current/usr/share/snapcraft/schema -p

@josephtensoriot
Copy link

Hi @josephtensoriot,

Sorry about this error occurring again.

After adding the SNAP_INSTANCE_NAME env var, which wasn't required when running Snapcraft 8.3.4, it then gave me the socket error mentioned above.

Can you remove this environment variable and try again?

Removing the variable gives the INSTANCE_NAME error from before

snapcraft internal error: AttributeError("'INSTANCE_NAME'")                                                                       
Full execution log: '/home/jenkins/.local/state/snapcraft/log/snapcraft-20241015-180014.486924.log'                               
Traceback (most recent call last):
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_env.py", line 61, in __getattr__
    return self._env[attr]
KeyError: 'INSTANCE_NAME'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 568, in run
    return_code = self._run_inner()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snapcraft/application.py", line 215, in _run_inner
    return_code = super()._run_inner()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 521, in _run_inner
    platform = self.get_arg_or_config(parsed_args, "platform")
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 511, in get_arg_or_config
    return self.services.config.get(item)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/service_factory.py", line 121, in __getattr__
    instance.setup()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/config.py", line 170, in setup
    snap_handler = SnapConfigHandler(self._app)
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/config.py", line 97, in __init__
    self._snap = snaphelpers.SnapConfig()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_conf.py", line 103, in __init__
    self._snapctl = snapctl or SnapCtl()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_ctl.py", line 81, in __init__
    self._instance_name = env.INSTANCE_NAME
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snaphelpers/_env.py", line 63, in __getattr__
    raise AttributeError(str(e))
AttributeError: 'INSTANCE_NAME'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/snap/snapcraft/current/bin/snapcraft", line 8, in <module>
    sys.exit(main())
  File "/snap/snapcraft/current/lib/python3.10/site-packages/snapcraft/application.py", line 495, in main
    return app.run()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/application.py", line 598, in run
    if self.services.config.get("debug"):
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/service_factory.py", line 121, in __getattr__
    instance.setup()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/config.py", line 163, in setup
    super().setup()
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_application/services/base.py", line 45, in setup
    emit.debug(f"Setting up {self.__class__.__name__}")
  File "/snap/snapcraft/current/lib/python3.10/site-packages/craft_cli/messages.py", line 417, in func
    raise RuntimeError("Emitter is stopped already")
RuntimeError: Emitter is stopped already

@josephtensoriot
Copy link

josephtensoriot commented Oct 15, 2024

Here is my Dockerfile for reference. I tried adding the suggestion above but still seem to get the same error. I also tried to run this as root, though we're trying to run this as another user in our pipeline. This worked with 8.3.4 and prior versions, not sure what specific change broke it.

# Build snapcraft in docker as seen in the example from
# https://raw.githubusercontent.com/snapcore/snapcraft/master/docker/Dockerfile
# This has been heavily modified to use Ubuntu 22.04 and a specific core version
# instead. The v2 info api had to be used to support arm64
# (with additional parsing logic, different from the example above)
# To use snapd, use the jenkins user in your pipeline

ARG RISK=stable
ARG UBUNTU=22.04
ARG SNAPCRAFT_VERSION=8

FROM ubuntu:$UBUNTU AS builder
ARG RISK
ARG UBUNTU
ARG SNAPCRAFT_VERSION
ARG TARGETARCH
RUN echo "Building Snapcraft ($RISK) in Ubuntu $UBUNTU ($TARGETARCH)"

# Grab dependencies
RUN apt-get update && apt-get -y dist-upgrade && apt-get -y install \
      curl=7.81.0-1ubuntu1.18 \
      jq=1.6-2.1ubuntu3 \
      squashfs-tools=1:4.5-3build1

# Grab the core snap (for backwards compatibility) from the stable channel and
# unpack it in the proper place.
RUN curl -L $(curl -H 'Snap-Device-Series: 16' "https://api.snapcraft.io/v2/snaps/info/core?architecture=$TARGETARCH" | jq ".\"channel-map\"[] | select(.channel.name == \"${RISK}\") | .download.url" -r) --output core.snap
RUN mkdir -p /snap/core
RUN unsquashfs -d /snap/core/current core.snap

# # Grab the core18 snap (which snapcraft uses as a base) from the stable channel
# # and unpack it in the proper place.
# RUN curl -L $(curl -H 'Snap-Device-Series: 16' "https://api.snapcraft.io/v2/snaps/info/core18?architecture=$TARGETARCH" | jq ".\"channel-map\"[] | select(.channel.name == \"${RISK}\") | .download.url" -r) --output core18.snap
# RUN mkdir -p /snap/core18
# RUN unsquashfs -d /snap/core18/current core18.snap

# # Grab the core20 snap (which snapcraft uses as a base) from the stable channel
# # and unpack it in the proper place.
# RUN curl -L $(curl -H 'Snap-Device-Series: 16' "https://api.snapcraft.io/v2/snaps/info/core20?architecture=$TARGETARCH" | jq ".\"channel-map\"[] | select(.channel.name == \"${RISK}\") | .download.url" -r) --output core20.snap
# RUN mkdir -p /snap/core20
# RUN unsquashfs -d /snap/core20/current core20.snap

# Grab the core22 snap (which snapcraft uses as a base) from the stable channel
# and unpack it in the proper place.
RUN curl -L $(curl -H 'Snap-Device-Series: 16' "https://api.snapcraft.io/v2/snaps/info/core22?architecture=$TARGETARCH" | jq ".\"channel-map\"[] | select(.channel.name == \"${RISK}\") | .download.url" -r) --output core22.snap
RUN mkdir -p /snap/core22
RUN unsquashfs -d /snap/core22/current core22.snap

# # Grab the core24 snap (which snapcraft uses as a base) from the stable channel
# # and unpack it in the proper place.
# RUN curl -L $(curl -H 'Snap-Device-Series: 16' "https://api.snapcraft.io/v2/snaps/info/core24?architecture=$TARGETARCH" | jq ".\"channel-map\"[] | select(.channel.name == \"${RISK}\") | .download.url" -r) --output core24.snap
# RUN mkdir -p /snap/core24
# RUN unsquashfs -d /snap/core24/current core24.snap

# Grab the snapcraft snap from the $RISK channel and unpack it in the proper
# place.
RUN curl -L $(curl -H 'Snap-Device-Series: 16' "https://api.snapcraft.io/v2/snaps/info/snapcraft?architecture=$TARGETARCH" | jq ".\"channel-map\"[] | select(.channel.name == \"${SNAPCRAFT_VERSION}.x/${RISK}\") | .download.url" -r) --output snapcraft.snap
RUN mkdir -p /snap/snapcraft
RUN unsquashfs -d /snap/snapcraft/current snapcraft.snap

# Fix Python3 installation: Make sure we use the interpreter from
# the snapcraft snap:
RUN unlink /snap/snapcraft/current/usr/bin/python3
RUN ln -s /snap/snapcraft/current/usr/bin/python3.* /snap/snapcraft/current/usr/bin/python3
RUN echo /snap/snapcraft/current/lib/python3.*/site-packages >> /snap/snapcraft/current/usr/lib/python3/dist-packages/site-packages.pth

# Create a snapcraft runner (TODO: move version detection to the core of
# snapcraft).
RUN mkdir -p /snap/bin
RUN echo "#!/bin/sh" > /snap/bin/snapcraft
RUN snap_version="$(awk '/^version:/{print $2}' /snap/snapcraft/current/meta/snap.yaml | tr -d \')" && echo "export SNAP_VERSION=\"$snap_version\"" >> /snap/bin/snapcraft
RUN echo 'exec "$SNAP/usr/bin/python3" "$SNAP/bin/snapcraft" "$@"' >> /snap/bin/snapcraft
RUN chmod +x /snap/bin/snapcraft

# Multi-stage build, only need the snaps from the builder. Copy them one at a
# time so they can be cached.
FROM ubuntu:$UBUNTU
COPY --from=builder /snap/core /snap/core
# COPY --from=builder /snap/core18 /snap/core18
# COPY --from=builder /snap/core20 /snap/core20
COPY --from=builder /snap/core22 /snap/core22
# COPY --from=builder /snap/core24 /snap/core24
COPY --from=builder /snap/snapcraft /snap/snapcraft
COPY --from=builder /snap/bin/snapcraft /snap/bin/snapcraft

RUN apt-get update && \
    apt-get -y dist-upgrade && \
    apt-get -y install sudo=1.9.9-1ubuntu2.4

RUN groupadd -g 1000 jenkins
RUN useradd -m -u 1000 -g 1000 -s /bin/bash jenkins
RUN adduser jenkins sudo
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers
# The apt-get install locales command requires access to these
RUN chown 1000:1000 /etc
RUN chown 1000:1000 /usr/lib/locale
# Needed for other users to read snap logs
RUN mkdir -p /root/.local/state/snapcraft/log
RUN chmod -R o+r /root/.local/state/snapcraft/log

USER jenkins

# Generate locale and install dependencies.
RUN sudo apt-get -y install \
      snapd=2.63+22.04ubuntu0.1 \
      locales=2.35-0ubuntu3.8 \
    && locale-gen en_US.UTF-8

RUN sudo mkdir /snap/snapcraft/current/usr/share/snapcraft/keyrings \
    /snap/snapcraft/current/usr/share/snapcraft/extensions \
    /snap/snapcraft/current/usr/share/snapcraft/plugins \
    /snap/snapcraft/current/usr/share/snapcraft/schema -p

ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8
ENV PATH=/snap/bin:/snap/snapcraft/current/libexec/snapcraft:${PATH}
ENV SNAP=/snap/snapcraft/current
ENV SNAP_NAME=snapcraft
ENV SNAP_INSTANCE_NAME=snapcraft
ENV SNAPCRAFT_BUILD_ENVIRONMENT=host

@om26er
Copy link
Contributor Author

om26er commented Oct 15, 2024

you should remove these three lines

ENV SNAP=/snap/snapcraft/current
ENV SNAP_NAME=snapcraft
ENV SNAP_INSTANCE_NAME=snapcraft

@josephtensoriot
Copy link

josephtensoriot commented Oct 15, 2024

you should remove these three lines

ENV SNAP=/snap/snapcraft/current
ENV SNAP_NAME=snapcraft
ENV SNAP_INSTANCE_NAME=snapcraft

Removing SNAP gives me: /usr/bin/python3: can't open file '/bin/snapcraft': [Errno 2] No such file or directory and the SNAP_INSTANCE_NAME gives me an emitter issue I posted in the previous response. I don't think I can get around removing either of those two. SNAP_NAME might be fine to remove though from what I've seen in my testing locally.

@om26er
Copy link
Contributor Author

om26er commented Oct 15, 2024

@josephtensoriot you should look at my complete Dockerfile I posted above. Note how I create the snapcraft launcher

@josephtensoriot
Copy link

josephtensoriot commented Oct 15, 2024

@josephtensoriot you should look at my complete Dockerfile I posted above. Note how I create the snapcraft launcher

Seems there was one line I missed that was different:
RUN echo 'exec "$SNAP/usr/bin/python3" "$SNAP/bin/snapcraft" "$@"' >> /snap/bin/snapcraft
needed to be changed to
RUN echo 'exec "/snap/snapcraft/current/bin/python3" -m snapcraft "$@"' >> /snap/bin/snapcraft
That was the missing link and did the trick for me. Thanks!

@mr-cal
Copy link
Collaborator

mr-cal commented Oct 15, 2024

Glad it's working. I'm going to leave this issue open because we can improve the error handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression A behavior that has regressed triaged
Projects
None yet
Development

No branches or pull requests

4 participants