Need guidance for cross-compiling with Docker

Hello

So from this topic I learnt that cross-compiling from MacOS to Linux is natively not possible in Rust.

I am trying to solve this by using Docker. However, I have no clue on how to get started.
Can someone point me in the right direction on how to setup Docker and how to cross-compile a Rust-project on it?

I tried the following: GitHub - chinedufn/cross-compile-rust-from-mac-to-linux: An example of how to cross compile Rust from `macOS` to Linux `x86_64-unknown-linux-gnu`
But after running ./build.sh I just get the same error as in the post linked in the beginning of this question.

What I think I should do:

  1. Install a Debian image in Docker.
  2. Install Rust on that image.
  3. Install libraries like openssl and libdevssl on that image.
  4. And then what?

In step 4 I have my Rust-project on my MacOS, and a Debian Docker-image. How do I compile my Rust-project using Docker without copy/pasting files between my MacOS and the Docker-image?

You can mount the directory of your crate in a Docker volume, so that the Rust toolchain in the container can access it.

So I do the following:

  1. Install a Debian image in Docker.
  2. Install Rust on that image.
  3. Install libraries like openssl and libdevssl on that image.
  4. Mount the directory containing my Rust-project into the Docker volume
  5. Now Docker has access to my project.
  6. Build my project using the Rust-compiler in my Docker container
  7. The executable will be made in the directory I mounted, and thus also accessible from my host os (Mac OS)

Is that correct?

Yes, correct.

1 Like

It worked out following the steps above.
However, I did not use the Debian-image. That one gave me a lot of Input/Output errors for some reason.

I pulled the Rust-image.
I made a container for it and made a folder in the container /home/my-project-name.

Then I committed that image and mounted the folder on my host containing my project to that container.
Using the following command on my host:

cd /go/to/my/project/folder
docker run -ti -v "$PWD":/home/my-project-name name-given-in-commit /bin/bash

Now I had access to the container and my the files from my host OS containing my project were accessible there. Changing a file on my host also instantly changed it in the container and vice versa.

In the container I just had to do cargo build --release. After waiting a long time the binary was generated and accessible on my host.

This binary worked perfectly on my VPS.

Worked great.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.