Snapcraft pack step fails in a Docker image

I build Polarr with Docker using this configuration:

  "name": "polarr",
  "version": "5.1.2",
  "description": "Powerful and easy-to-use photo editor.",
  "main": "main.js",
  "author": "Polarr, Inc",
  "scripts": {
    "start": "electron . --dev",
    "build:mac": "electron-packager . Polarr --name=Polarr --platform=darwin --arch=x64 --icon=icon.icns --asar --overwrite --version-string.ProductName=Polarr",
    "build:win": "node build_win.js",
    "build:linux": "node build_linux.js",
    "docker": "bash",
    "snap:linux": "electron-builder",
    "build_win_zh": "node build_win_zh.js"
  "build": {
    "appId": "co.polarr.photoeditor",
    "linux": {
      "icon": "icon",
      "synopsis": "Powerful and easy-to-use photo editor.",
      "description": "Used by the world's most professional portrait and landscape photographers, Polarr offers advanced auto-enhance tools and sophisticated filters to edit every detail of your photo.",
      "category": "Graphics;Photography",
      "target": [
    "fileAssociations": [
        "ext": [
        "name": "Raw Image"
        "ext": ["jpg", "jpeg", "png", "bmp", "gif"],
        "name": "Image"
    "extraResources": [
    "appImage": {},
    "snap": {
      "confinement": "strict",
      "summary": "Powerful and easy-to-use photo editor.",
      "grade": "stable",
      "stagePackages": [
      "plugs": [
      "environment": {
        "LD_LIBRARY_PATH": "$SNAP/usr/lib:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/app/resources/node_modules/sharp/vendor/lib"
  "devDependencies": {
    "asar": "^0.14.3",
    "electron": "^2.0.0",
    "electron-builder": "^20.8.2",
    "electron-packager": "^12.1.0",
    "electron-squirrel-startup": "^1.0.0",
    "electron-winstaller": "^2.6.4",
    "env-paths": "^1.0.0",
    "grunt": "^1.0.2",
    "grunt-electron-installer": "^2.1.0",
    "iconv-lite": "^0.4.18",
    "locate-path": "^3.0.0",
    "universalify": "^0.1.0"
  "dependencies": {
    "electron-fetch": "^1.0.0",
    "get-pixels": "^3.3.0",
    "libraw": "file:libraw",
    "readable-stream": "^3.0.1",
    "sharp": "^0.20.5",
    "temp": "^0.8.3",
    "url-exists": "^1.0.3"
FROM ubuntu:16.04

COPY build_linux.js /tmp/src/
COPY electron-version.js /tmp/src/
COPY main.js /tmp/src/
COPY package.json /tmp/src/
COPY libraw /tmp/src/libraw
COPY /tmp/src/
ADD /tmp/

RUN apt-get update -y
RUN apt-get upgrade -y
RUN useradd -m -G sudo polarr-setup
RUN mv /tmp/src /home/polarr-setup/src
RUN chown -R polarr-setup:polarr-setup /home/polarr-setup/src
RUN apt-get install -y build-essential curl gosu sudo ruby snapcraft
RUN echo "%sudo ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/polarr-setup
RUN bash /tmp/
RUN rm /tmp/
RUN apt-get install -y nodejs
RUN npm i -g node-gyp
RUN npm i -g sass
RUN cd /home/polarr-setup/src && sudo -u polarr-setup -H sh -c "npm install && npm run build:linux && npm run snap:linux"

COPY /usr/local/bin/

ENTRYPOINT ["/usr/local/bin/"]

CMD ["bash"]

# Add local user
# Either use the LOCAL_USER_ID if passed in at runtime or fallback


echo "Starting with UID : $USER_ID"
useradd --shell /bin/bash -u ${USER_ID} -G sudo -o -c "" -m polarr-build
export HOME=/home/polarr-build
chown -R polarr-build /tmp
if [ -d /polarr-build ]; then
  ln -ns /polarr-build /home/polarr-build/src

exec gosu polarr-build "$@"
#!/usr/bin/env bash

docker run \
  --interactive \
  --tty \
  --rm \
  -e LOCAL_USER_ID=$(id -u $USER) \
  -v $PWD:/polarr-build \
npm run docker; # go into docker container
sudo apt update && sudo apt upgrade;
cd ~/src;
./; # builds C++ addons
npm run snap:linux; #fails at snapcraft pack

Here is the end of the build log where snapcraft (2.43) fails:

npm run snap:linux
Staging desktop-gtk3 
Hit xenial InRelease                                                                                                                                                  
Hit xenial-security InRelease                                                                                                                                            
Hit xenial InRelease                                                                                                                                                      
Hit xenial-updates InRelease                                                                                                                                              
Hit xenial-backports InRelease                                                                                                                                            
Fetched 0 B in 0s (0 B/s)                                                                                                                                                                                  
rm: cannot remove '/var/cache/apt/archives/partial/*.deb': Permission denied
Pulling app 
Skipping build desktop-gtk3 (already ran)
Building app 
Skipping stage desktop-gtk3 (already ran)
Staging app 
Priming desktop-gtk3 
Priming app 
Files from the build host were migrated into the snap to satisfy dependencies that would otherwise not be met. This feature will be removed in a future release. If these libraries are needed in the final snap, ensure that the following are either satisfied by a stage-packages entry or through a part:
  ⨯ error: exit status 1
path: /usr/bin/snapcraft
args: [snapcraft pack /polarr-build/dist/__snap-x64/prime --output /polarr-build/dist/polarr_5.1.2_amd64.snap]
output: Sorry, Snapcraft ran into an error when trying to running through its
lifecycle that generated a trace that has been put in '/tmp/tmpbkvppjrr/trace.txt'.
"Submitting this error to the Snapcraft developers is not possible through the CLI
without Raven installed.
If you wish to report this issue, please copy the contents of the previous traceback
and submit manually at

Error: /polarr-build/node_modules/app-builder-bin/linux/x64/app-builder exited with code 1
    at ChildProcess.childProcess.once.code (/polarr-build/node_modules/builder-util/src/util.ts:254:14)
    at Object.onceWrapper (events.js:273:13)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:962:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
From previous event:
    at (/polarr-build/node_modules/app-builder-lib/src/targets/snap.ts:143:44)
    at Function.buildAsyncTargets (/polarr-build/node_modules/app-builder-lib/src/platformPackager.ts:139:36)
    at LinuxPackager.packageInDistributableFormat (/polarr-build/node_modules/app-builder-lib/src/platformPackager.ts:118:24)
    at /polarr-build/node_modules/app-builder-lib/src/platformPackager.ts:113:10
    at (<anonymous>)
    at runCallback (timers.js:694:18)
    at tryOnImmediate (timers.js:665:5)
    at processImmediate (timers.js:647:5)
From previous event:
    at LinuxPackager.pack (/polarr-build/node_modules/app-builder-lib/src/platformPackager.ts:110:95)
    at /polarr-build/node_modules/app-builder-lib/src/packager.ts:376:24
    at (<anonymous>)
    at xfs.stat (/polarr-build/node_modules/fs-extra-p/node_modules/fs-extra/lib/mkdirs/mkdirs.js:56:16)
    at /polarr-build/node_modules/graceful-fs/polyfills.js:287:18
    at FSReqWrap.oncomplete (fs.js:155:5)
From previous event:
    at Packager.doBuild (/polarr-build/node_modules/app-builder-lib/src/packager.ts:344:39)
    at /polarr-build/node_modules/app-builder-lib/src/packager.ts:314:57
    at (<anonymous>)
    at /polarr-build/node_modules/graceful-fs/graceful-fs.js:99:16
    at /polarr-build/node_modules/graceful-fs/graceful-fs.js:43:10
    at FSReqWrap.oncomplete (fs.js:141:20)
From previous event:
    at Packager._build (/polarr-build/node_modules/app-builder-lib/src/packager.ts:285:133)
    at /polarr-build/node_modules/app-builder-lib/src/packager.ts:281:23
    at (<anonymous>)
From previous event:
    at (/polarr-build/node_modules/app-builder-lib/src/packager.ts:238:14)
    at build (/polarr-build/node_modules/app-builder-lib/src/index.ts:58:28)
    at build (/polarr-build/node_modules/electron-builder/src/builder.ts:227:10)
    at then (/polarr-build/node_modules/electron-builder/src/cli/cli.ts:42:48)
    at runCallback (timers.js:694:18)
    at tryOnImmediate (timers.js:665:5)
    at processImmediate (timers.js:647:5)
From previous event:
    at Object.args [as handler] (/polarr-build/node_modules/electron-builder/src/cli/cli.ts:42:48)
    at Object.runCommand (/polarr-build/node_modules/yargs/lib/command.js:237:44)
    at Object.parseArgs [as _parseArgs] (/polarr-build/node_modules/yargs/yargs.js:1085:24)
    at Object.get [as argv] (/polarr-build/node_modules/yargs/yargs.js:1000:21)
    at Object.<anonymous> (/polarr-build/node_modules/electron-builder/src/cli/cli.ts:25:28)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:696:3)
npm ERR! errno 1
npm ERR! polarr@5.1.2 snap:linux: `electron-builder`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the polarr@5.1.2 snap:linux script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/polarr-build/.npm/_logs/2018-09-11T08_34_08_205Z-debug.log
cat /home/polarr-build/.npm/_logs/2018-09-11T08_34_08_205Z-debug.log
polarr-build@c600c59ebbe0:~/src$ cat /home/polarr-build/.npm/_logs/2018-09-11T08_34_08_205Z-debug.log
0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'run', 'snap:linux' ]
2 info using npm@6.4.1
3 info using node@v10.10.0
4 verbose run-script [ 'presnap:linux', 'snap:linux', 'postsnap:linux' ]
5 info lifecycle polarr@5.1.2~presnap:linux: polarr@5.1.2
6 info lifecycle polarr@5.1.2~snap:linux: polarr@5.1.2
7 verbose lifecycle polarr@5.1.2~snap:linux: unsafe-perm in lifecycle true
8 verbose lifecycle polarr@5.1.2~snap:linux: PATH: /usr/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/polarr-build/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
9 verbose lifecycle polarr@5.1.2~snap:linux: CWD: /polarr-build
10 silly lifecycle polarr@5.1.2~snap:linux: Args: [ '-c', 'electron-builder' ]
11 silly lifecycle polarr@5.1.2~snap:linux: Returned: code: 1  signal: null
12 info lifecycle polarr@5.1.2~snap:linux: Failed to exec snap:linux script
13 verbose stack Error: polarr@5.1.2 snap:linux: `electron-builder`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:182:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:182:13)
13 verbose stack     at maybeClose (internal/child_process.js:962:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
14 verbose pkgid polarr@5.1.2
15 verbose cwd /polarr-build
16 verbose Linux 4.15.0-33-generic
17 verbose argv "/usr/bin/node" "/usr/bin/npm" "run" "snap:linux"
18 verbose node v10.10.0
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error polarr@5.1.2 snap:linux: `electron-builder`
22 error Exit status 1
23 error Failed at the polarr@5.1.2 snap:linux script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

I can work around this failure by installing snapcraft outside of Docker and running the snapcraft pack ... command where it will succeed and finish building the .snap file. I shouldn’t have to call snapcraft twice, each in a different way, to build a package though.

If this is a bug and not just me misconfiguring something, it may be a duplicate of Running snapcraft inside Docker container fails, but the failing step here is “snapcraft pack” and I don’t see that in the error log for the other post.

I have no idea why this was failing. I updated my packages and now it’s not happening, so good.