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

Optionally Run Cross Build? #18

Open
bitbier opened this issue Jan 3, 2019 · 11 comments
Open

Optionally Run Cross Build? #18

bitbier opened this issue Jan 3, 2019 · 11 comments

Comments

@bitbier
Copy link

bitbier commented Jan 3, 2019

This is more a question and I'll tag it as such:

Is there a way to optionally check some kind of environment variable that if it exists, will not run cross-build-start? I ask because I've been trying to figure out a way of keeping the same Dockerfile for both building in CI (x86) and building on balena (ARM). Right now I have to have two separate Dockerfiles to build the same image minus the wrapping the installation code inside the

RUN [ "cross-build-start" ]
...
RUN [ "cross-build-end" ]

Thanks!

@bitbier
Copy link
Author

bitbier commented Jan 3, 2019

Oh, there are no tags so can't tag this issue as a question.

@petrosagg
Copy link
Contributor

@bitbier I banged my head for a long time trying to accomplish architecture parameterized dockerfiles but unfortunately Docker won't replace variables.

However you made me think about it again and I think there is a solution! It's a bit more verbose but I think if we have a dockerfile like this:

FROM balenalib/crossbuild

FROM raspbian
COPY --from=0 /xbuild /bin

RUN [ "xbuild", "start" ]
RUN ...                                                                                                                                                                                             
RUN [ "xbuild", "end" ]

And balenalib/crossbuild is set up as a multiarchitecture repo then Docker will automatically pull the right image and so on ARM xbuild could be a no-op but in x86 do what it currently does.

@nghiant2710 we should build this, it should be fairly easy to make

@bitbier
Copy link
Author

bitbier commented Feb 20, 2019

Nice that would be awesome

@nghiant2710
Copy link
Contributor

@petrosagg sorry, I missed this one. I will set up the crossbuild repository but how about we update balena-xbuild, https://github.com/balena-io-library/armv7hf-debian-qemu/blob/master/resin-xbuild.go#L44-L48 we just need to change here a bit, so if qemu doesn't exist (both arm and aarch64) then it's not ARM image and we do nothing here?

@jgentes
Copy link

jgentes commented Jul 15, 2019

We've got this working with:

# cross-build for x86
RUN if [ %%BALENA_ARCH%% = "i386" ] ; then [ "cross-build-start" ] ; fi

@bitbier
Copy link
Author

bitbier commented Jul 15, 2019

I haven't tested this out yet, but doesn't the balena variables only exist for Dockerfile.template or will it apply those values on any Dockerfile being used for building and not just the Dockerfile.template.

Also these variables are only applied when building in Balena, so if you have Circle CI or some other CI process, you may need to do something like:

RUN if [ "%%BALENA_ARCH%%" = "\%\%BALENA_ARCH\%\%" ] || [ "%%BALENA_ARCH%%" = "i386"] ; then [ "cross-build-start" ] ; fi

^ untested.

May work to see if I can get this done sometime this week, but it would be nice to just have the multi-arch repo for the crossbuild as suggested above.

@petrosagg
Copy link
Contributor

@jgentes @bitbier I don't think any approach based on RUN statements can work. Have you tried running the Dockerfile on both x86 and ARM machines?

The reason is that the if statement itself will be ran by /bin/bash which is a binary built either for ARM or x86 so when you try to run it on the wrong platform it won't even arrive at the point of running cross-build-start.

@bitbier
Copy link
Author

bitbier commented Jul 15, 2019

I haven't. Right now we just have two separate Dockerfiles, one for CI and one for balena build/deployments. I was going to try it later this week, but I also think that you are right. I don't think it will work because it'll run in bash or sh like you stated above.

@jgentes
Copy link

jgentes commented Jul 15, 2019

@petrosagg I have tested it, and it works, however I'm only building on Balena - one app for x86 and the other for ARM. My base image is debian-node:10 so perhaps that's why it works on both.

@petrosagg
Copy link
Contributor

Right, that's why. Our balena builders do some magic when they have to cross compile. Namely, we bind mount a qemu binary in the container and we have binfmt set up for emulation

@jgentes
Copy link

jgentes commented Jul 17, 2019

Another note.. building locally, my box shows up as amd64 so my line ultimately reads:

RUN if [ %%BALENA_ARCH%% = "i386" ] || [ %%BALENA_ARCH%% = "amd64" ] ; then [ "cross-build-start" ] ; fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants