RGB-WG / rgb-node

RGB node - the official server-side implementation
https://rgb.tech
MIT License
148 stars 43 forks source link

refactor: optimize Docker build with caching (using cargo chef) + fix build issues #188

Closed louneskmt closed 2 years ago

louneskmt commented 2 years ago

This PR fixes some Docker build issues and allows us to build the dependencies separated from the main application. This allows the Docker layer compiling dependencies to be cached and reused in later builds, greatly reducing the build time when there isn't any dependency changes. It uses cargo-chef (the source code is pretty simple and straightforward).

I've done the same for BP Node, LNP Node, Storm Node and Store Service. I've been building the whole stack with that this morning, it is all working well so far. As the changes for the other repos Dockerfiles are substantially identical, I'm first opening this one PR to review.

louneskmt commented 2 years ago

Sidenote: I only tested this against the latest releases (rc, alpha), and not against master due to frequent cargo / build issues with master.

louneskmt commented 2 years ago

The recipe.json is created by the chef prepare command. The command basically goes through all the directories and subdirectories to find dependencies' Cargo.toml, in order to extract all the dependencies of the project. AFAIU the file contains all the Cargo.toml files contents (or only the dependencies part, not sure).

Then, in another build stage, we import that file. If the file has changed since the last build (which means dependencies has changed), it will detect the change and rerun the chef cook command (which is basically an alias for cargo build for building dependencies). If it hasn't changed, the next steps will use the cached layer from the previous build, and thus won't build again the dependencies.

louneskmt commented 2 years ago

You can check out the project repo for more details: https://github.com/LukeMathWalker/cargo-chef

louneskmt commented 2 years ago

@dr-orlovsky Opening a bunch of PR on every repo for the Dockerfiles. For the instructions, I only added the build ones (without usage), I plan to open a PR to LNPBP/nodes with full docker compose instructions (if that seems ok to you)