[Rust - Docker] Docker build and run Rust Rocket Web app with multi-stage builds
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.
Prerequites
Docker
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.
Installing Rust
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.
1 | 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
.
Initialize
Let’s write our first Rocket application! Start by creating a new binary-based Cargo project and changing into the new directory:
1 | cargo new col-rust-rocket-docker-example --bin |
Now, add Rocket as a dependency in your Cargo.toml
:
1 | # Cargo.toml |
Make main.rs
file
1 | // src/main.rs |
It creates an index
route, mounts the route at the / path, and launches the application.
See Getting Started - Rocket Programming Guide - https://rocket.rs/v0.5-rc/guide/getting-started/ to learn more.
Dockerfile
Base Image
Create or edit Dockerfile-base
file to base image for the system and Rust packages.
1 | # Dockerfile-base |
Build base image.
1 | docker build . -f Dockerfile-base -t cloudolife/col-rust-docker-example-base |
Final Image
1 |
|
Build final image.
1 | docker build . -f Dockerfile-base -t cloudolife/col-rust-docker-example |
Run
1 | docker run --rm --name col-rust-docker-example -p 8000:8000 cloudolife/col-rust-docker-example |
Image Size
Compare images size:
1 | $ docker images | grep col-rust-docker-example |
FAQS
Make sure you also have the development packages of openssl installed.
1 | 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." |
Add openssl
dependency to fix that issue.
1 | # Cargo.toml |
See Ubuntu 18: failed to run custom build command for openssl-sys v0.9.39
· Issue #1021 · sfackler/rust-openssl - https://github.com/sfackler/rust-openssl/issues/1021 to learn more.
References
[1] [Getting Started - Rocket Programming Guide - https://rocket.rs/v0.5-rc/guide/getting-started/])(https://rocket.rs/v0.5-rc/guide/getting-started/)
[2] Get Docker | Docker Documentation - https://docs.docker.com/get-docker/
[3] rustup.rs - The Rust toolchain installer - https://rustup.rs/
[6] Dockerfile reference | Docker Documentation - https://docs.docker.com/engine/reference/builder/
[7] Rust - Official Image | Docker Hub - https://hub.docker.com/_/rust
[9] How to create small Docker images for Rust - https://kerkour.com/rust-small-docker-image/