Rust Rocket Docker with multiple build stages
Rocket is a web framework for Rust. Rocket aims to be fast, easy, and flexible while offering guaranteed safety and security where it can. Importantly, Rocket also aims to be fun, and it accomplishes this by ensuring that you write as little code as needed to accomplish your task.
Multistage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain. See Use multi-stage builds | Docker Documentation - https://docs.docker.com/develop/develop-images/multistage-build/ to learn more.
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.
See Get Docker | Docker Documentation - https://docs.docker.com/get-docker/ to learn more.
Rocket makes use of the latest Rust features. Because of this, we’ll need a recent release of Rust to run Rocket applications. If you already have a working installation of the latest Rust compiler, feel free to skip to the next section.
Install rustup by following the instructions
See rustup.rs - The Rust toolchain installer - https://rustup.rs/ to learn more.
Note: You may prefer to develop using the nightly channel.
The nightly Rust toolchain enables certain improved developer experiences, such as better compile-time diagnostics, when developing with Rocket. You may choose to develop on the nightly channel to take advantage of these improved experiences. Note that all Rocket features are available across all Rust channels.
To set the nightly toolchain as your default, run
rustup default nightly.
Let’s write our first Rocket application! Start by creating a new binary-based Cargo project and changing into the new directory:
cargo new col-rust-rocket-docker-example --bin
Now, add Rocket as a dependency in your
It creates an
index route, mounts the route at the / path, and launches the application.
Create or edit
Dockerfile-base file to base image for the system and Rust packages.
Build base image.
docker build . -f Dockerfile-base -t cloudolife/col-rust-docker-example-base
Build final image.
docker build . -f Dockerfile-base -t cloudolife/col-rust-docker-example
docker run --rm --name col-rust-docker-example -p 8000:8000 cloudolife/col-rust-docker-example
Compare images size:
$ docker images | grep col-rust-docker-example
Make sure you also have the development packages of openssl installed.
12 790.6 run pkg_config fail: "pkg-config has not been configured to support cross-compilation.\n\nInstall a sysroot for the target platform and configure it via\nPKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\ncross-compiling wrapper for pkg-config and set it via\nPKG_CONFIG environment variable."
openssl dependency to fix that issue.