balena-io / balena-cli

The official balena CLI tool.
Apache License 2.0
454 stars 139 forks source link

`balena build --emulated` doesn't work when the Dockefile.template is in a different path than the build context #2550

Open thgreasi opened 2 years ago

thgreasi commented 2 years ago

This tracks the atm blocked use case of emulated builds when the Dockerfile.template needs to be in a different path than the builds context/the path that balena build is issued. Probably related to #2549.

Expected Behavior

Emulated builds should work when using a different build context should work just like native builds.

Actual Behavior

Please describe what actually happened instead:

balena build --debug --deviceType raspberrypi3 --arch armv7hf --emulated --nocache --dockerfile Dockerfile.template

on a amd64 system fails.

Interestingly enough when not explicitly providing the dockerfile parameter the command works:

balena build --debug --deviceType raspberrypi3 --arch armv7hf --emulated --nocache

This doesn't seem to be a case specific to not accepting template file for the --dockerfile parameter since the command works fine for non-emulated builds

balena build --debug --deviceType intel-nuc --arch amd64 --nocache --dockerfile Dockerfile.template

FWIW the emulated command does succeed when pointing to a plain Dockerfile

balena build --debug --deviceType raspberrypi3 --arch armv7hf --emulated --nocache --dockerfile OtherDockerfile
balena build --debug --deviceType intel-nuc --arch amd64 --nocache --dockerfile OtherDockerfile

Steps to Reproduce the Problem

  1. Create an empty test.txt file in your current directory
  2. Create a nested folder inside which create the following Dockerfile.template
FROM balenalib/%%BALENA_MACHINE_NAME%%-alpine

RUN echo "HELLO WORLD" | cat

COPY test.txt test.txt
  1. Run an emulated build (tested on amd64 Ubuntu 22.04) from the parent/top directory
~/tmp/nested$ DEBUG=1 balena build .. --debug --deviceType raspberrypi3 --arch armv7hf --emulated --nocache --dockerfile nested/Dockerfile.template 
[debug] original argv0="node" argv=[/home/myuser/.nvm/versions/node/v12.20.0/bin/node,/home/myuser/.nvm/versions/node/v12.20.0/bin/balena,build,..,--debug,--deviceType,raspberrypi3,--arch,armv7hf,--emulated,--nocache,--dockerfile,nested/Dockerfile.template] length=13
[debug] new argv=[/home/myuser/.nvm/versions/node/v12.20.0/bin/node,/home/myuser/.nvm/versions/node/v12.20.0/bin/balena,build,..,--deviceType,raspberrypi3,--arch,armv7hf,--emulated,--nocache,--dockerfile,nested/Dockerfile.template] length=12
[debug] Deprecation check: 1.02680 days since last npm registry query for next major version release date.
[debug] Will not query the registry again until at least 7 days have passed.
[Debug]   Parsing input...
[Debug]   Loading project...
[Debug]   Resolving project...
[Info]    No "docker-compose.yml" file found at "/home/myuser/tmp"
[Info]    Creating default composition with source: "/home/myuser/tmp"
[Debug]   Creating project...
[Build]   Building services...
[Build]   main Preparing...
[Info]    Building for armv7hf/raspberrypi3
[Info]    Emulation is enabled
[Debug]   Found build tasks:
[Debug]       main: build [.]
[Debug]   Resolving services with [raspberrypi3|armv7hf]
[Debug]   Found project types:
[Debug]       main: Dockerfile.template
[Debug]   Prepared tasks; building...
[Build]   main Step 1/3 : FROM balenalib/raspberrypi3-alpine
[Build]   main  ---> 3d3155b4337a
[Build]   main Step 2/3 : RUN echo "HELLO WORLD" | cat
[Build]   main  ---> [Warning] The requested image's platform (linux/arm/v7) does not match the detected host platform (linux/amd64) and no specific platform was requested
[Build]   main  ---> Running in f8cdbf4e8da9
[Build]   main exec /bin/sh: exec format error
[Build]   Built 1 service in 0:04
[Error]   Build failed.
The command '/bin/sh -c echo "HELLO WORLD" | cat' returned a non-zero code: 1

Error: The command '/bin/sh -c echo "HELLO WORLD" | cat' returned a non-zero code: 1
    at Stream.<anonymous> (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/@balena/compose/dist/build/builder.js:102:23)
    at Stream.stream.write (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/through/index.js:26:11)
    at Stream.ondata (internal/streams/legacy.js:19:31)
    at Stream.emit (events.js:314:20)
    at Stream.EventEmitter.emit (domain.js:483:12)
    at drain (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/through/index.js:36:16)
    at Stream.stream.queue.stream.push (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/through/index.js:45:5)
    at Parser.parser.onToken (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/JSONStream/index.js:132:18)
    at Parser.proto.write (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/jsonparse/jsonparse.js:135:34)
    at Stream.<anonymous> (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/JSONStream/index.js:23:12)
    at Stream.stream.write (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/through/index.js:26:11)
    at IncomingMessage.ondata (_stream_readable.js:718:22)
    at IncomingMessage.emit (events.js:314:20)
    at IncomingMessage.EventEmitter.emit (domain.js:483:12)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
From previous event:
    at /home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/@balena/compose/dist/build/builder.js:39:20
    at processImmediate (internal/timers.js:461:21)
From previous event:
    at Builder.createBuildStream (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/@balena/compose/dist/build/builder.js:38:97)
    at /home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/@balena/compose/dist/multibuild/build.js:90:21
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

For further help or support, visit:
https://www.balena.io/docs/reference/balena-cli/#support-faq-and-troubleshooting
  1. Run an emulated build (tested on amd64 Ubuntu 22.04) from the nested directory
    
    ~/tmp/nested$ DEBUG=1 balena build .. --debug --deviceType raspberrypi3 --arch armv7hf --emulated --nocache --dockerfile nested/Dockerfile.template ^C
    ~/tmp/nested$ cd ..
    ~/tmp$ DEBUG=1 balena build --debug --deviceType raspberrypi3 --arch armv7hf --emulated --nocache --dockerfile nested/Dockerfile.template 
    [debug] original argv0="node" argv=[/home/myuser/.nvm/versions/node/v12.20.0/bin/node,/home/myuser/.nvm/versions/node/v12.20.0/bin/balena,build,--debug,--deviceType,raspberrypi3,--arch,armv7hf,--emulated,--nocache,--dockerfile,nested/Dockerfile.template] length=12
    [debug] new argv=[/home/myuser/.nvm/versions/node/v12.20.0/bin/node,/home/myuser/.nvm/versions/node/v12.20.0/bin/balena,build,--deviceType,raspberrypi3,--arch,armv7hf,--emulated,--nocache,--dockerfile,nested/Dockerfile.template] length=11
    [debug] Deprecation check: 1.02698 days since last npm registry query for next major version release date.
    [debug] Will not query the registry again until at least 7 days have passed.
    [Debug]   Parsing input...
    [Debug]   Loading project...
    [Debug]   Resolving project...
    [Info]    No "docker-compose.yml" file found at "/home/myuser/tmp"
    [Info]    Creating default composition with source: "/home/myuser/tmp"
    [Debug]   Creating project...
    [Build]   Building services...
    [Build]   main Preparing...
    [Info]    Building for armv7hf/raspberrypi3
    [Info]    Emulation is enabled
    [Debug]   Found build tasks:
    [Debug]       main: build [.]
    [Debug]   Resolving services with [raspberrypi3|armv7hf]
    [Debug]   Found project types:
    [Debug]       main: Dockerfile.template
    [Debug]   Prepared tasks; building...
    [Build]   main Step 1/3 : FROM balenalib/raspberrypi3-alpine
    [Build]   main  ---> 3d3155b4337a
    [Build]   main Step 2/3 : RUN echo "HELLO WORLD" | cat
    [Build]   main  ---> [Warning] The requested image's platform (linux/arm/v7) does not match the detected host platform (linux/amd64) and no specific platform was requested
    [Build]   main  ---> Running in c05578037109
    [Build]   main exec /bin/sh: exec format error
    [Build]   Built 1 service in 0:04
    [Error]   Build failed.
    The command '/bin/sh -c echo "HELLO WORLD" | cat' returned a non-zero code: 1

Error: The command '/bin/sh -c echo "HELLO WORLD" | cat' returned a non-zero code: 1 at Stream. (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/@balena/compose/dist/build/builder.js:102:23) at Stream.stream.write (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/through/index.js:26:11) at Stream.ondata (internal/streams/legacy.js:19:31) at Stream.emit (events.js:314:20) at Stream.EventEmitter.emit (domain.js:483:12) at drain (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/through/index.js:36:16) at Stream.stream.queue.stream.push (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/through/index.js:45:5) at Parser.parser.onToken (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/JSONStream/index.js:132:18) at Parser.proto.write (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/jsonparse/jsonparse.js:135:34) at Stream. (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/JSONStream/index.js:23:12) at Stream.stream.write (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/through/index.js:26:11) at IncomingMessage.ondata (_stream_readable.js:718:22) at IncomingMessage.emit (events.js:314:20) at IncomingMessage.EventEmitter.emit (domain.js:483:12) at addChunk (_stream_readable.js:297:12) at readableAddChunk (_stream_readable.js:272:9) From previous event: at /home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/@balena/compose/dist/build/builder.js:39:20 at processImmediate (internal/timers.js:461:21) From previous event: at Builder.createBuildStream (/home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/@balena/compose/dist/build/builder.js:38:97) at /home/myuser/.nvm/versions/node/v12.20.0/lib/node_modules/balena-cli/node_modules/@balena/compose/dist/multibuild/build.js:90:21 at processTicksAndRejections (internal/process/task_queues.js:97:5)

For further help or support, visit: https://www.balena.io/docs/reference/balena-cli/#support-faq-and-troubleshooting


FWIW the same commands do work for native builds and for emulated builds pointing to plain Dockerfiles

~/tmp$ DEBUG=1 balena build --debug --deviceType intel-nuc --arch amd64 --nocache --dockerfile nested/Dockerfile.template ~/tmp/nested$ DEBUG=1 balena build .. --debug --deviceType intel-nuc --arch amd64 --nocache --dockerfile nested/Dockerfile.template ~/tmp$ DEBUG=1 balena build --debug --deviceType raspberrypi3 --arch armv7hf --emulated --nocache --dockerfile nested/MyDockerfile ~/tmp/nested$ DEBUG=1 balena build .. --debug --deviceType raspberrypi3 --arch armv7hf --emulated --nocache --dockerfile nested/MyDockerfile



# Specifications

- **balena CLI version:** 14.5.0
- **Cloud backend: balenaCloud
- **Operating system version:** Ubuntu 22.04
- **32/64 bit OS and processor:** 64-bit Intel
- **Install method:** npm
- **If npm install, Node.js and npm version:** e.g. Node v12.20.0 and npm v6.14.17

# Additional References

If applicable, please add additional links to GitHub projects, forums.balena.io threads,
gist.github.com, Google Drive attachments, etc.
jellyfish-bot commented 2 years ago

[thgreasi] This has attached https://jel.ly.fish/abf3ee13-ea23-46a4-92e6-facb984582cf