Cloud modernization is a crucial step for businesses aiming to enhance efficiency, scalability, and performance. In this post, we'll explore how developing Rust applications in Linux containers can be a game-changer in this process.
By the end of this article, you should understand how to:
Using Linux containers to develop software can save you time and effort. Developing software in containers gives you near-instant startup times, faster iteration, standardized tooling for local and remote deployment. Containers help you to bring organization to the chaos that is software development.
Rust, known for its performance and safety, is an excellent choice for cloud-based applications. When combined with the power of Linux containers, it provides an isolated, efficient environment for application development.
This combination is particularly potent and offers a robust platform for building and deploying modern applications.
There are a few different software tools that we’ll need to install locally, in order to set up a containerized development environment. Let’s briefly list those, and then dive further into each of them.
As you can see, we don’t need all that many tools. We just need to know how to use them! After installing the tools above, move onto the next section where we’ll launch a Rust container, using Docker CLI.
One of the benefits of using containers to develop and deploy software, is that pre-built container images exist with the necessary components for your language or framework. Rust is no exception. On the Docker Hub, a registry that hosts a vast, searchable array of container images, there is an official Rust container image with the toolchain pre-installed.
By simply running a container, using the Rust container image, you can skip the step of “installing” the Rust toolchain locally. Even better, the container has Rust installed on its own, isolated filesystem, which means your host filesystem is protected from dependency conflicts.
Fire up a terminal session and run the following command.
docker run --interactive --tty --restart=always rust
This command will spawn a Bash shell inside a new container, based on the Rust image. You can leave the interactive shell running, or you can detach from it using the standard Docker keyboard shortcut: CTRL + P, followed by CTRL + Q.
By setting the restart policy to always, you ensure that the Rust container automatically starts back up when you restart your development workstation.
Now that you have a Rust container running locally, you can connect to it from Microsoft Visual Studio Code. Open VSCode and then open the Command Palette, typically using F1 or CTRL+SHIFT+P keyboard shortcuts. Search for the command “Dev Containers: Attach to Running Container,” and hit ENTER.
Now you can create a project directory with the Cargo CLI tool. Notice that we don’t have to “install” the Rust toolchain, because the Rust container image already includes it. You can open the Integrated Terminal in VSCode by using the CTRL+` keyboard shortcut.
cargo new sample-app
Now you can open VSCode into that directory by using the Open Folder command. By default, the Open Folder command is mapped to the CTRL+K, CTRL+O keyboard sequence. You’ll notice the VSCode window reload, but this should only take a brief moment.
Once you're connected to a development container in vs code, you can install VSCode extensions into that container’s context. Although the Rust container image includes the Rust toolchain, it doesn't include any additional components specific to VSCode.
Once you’ve installed the Rust Analyzer extension into the Rust container, you should have advanced development features available, including: auto-completion, syntax highlighting, refactoring, and much more.
Now that our environment is all set up, you can edit your application code in src/main.rs and then build / test your Rust application. Let’s install the “uuid” crate and change our main.rs file to something like the following, or feel free to be creative!
cargo add uuid --features=all
fn main() {
println!("{}", uuid::Uuid::new_v4().to_string());
}
This simple program will print out a Universal Unique Identifier (UUID) every time you run it. It’s nothing fancy, but at least it shows something different on each launch! Now let’s try to run the program from the VSCode Integrated Terminal again.
We could just use the cargo run command, but let’s compile a slimmed-down release build, and then run it.
cargo build --release
./target/release/sample-app
You should see a UUID printed out to the terminal, similar to the screenshot below.
In this article, we learned how to use the Rust toolchain inside of an isolated Linux container, rather than installing the toolchain on our host operating system. This layer of isolation protects the Rust toolchain from other applications that might step on its toes, and prevents the Rust toolchain from having accidental impact on other applications.
If you need to develop applications using different versions of the Rust toolchain simultaneously, then using Linux containers can help you accomplish that easily. Some Rust crates may require that you use the cutting edge nightly version of the Rust toolchain, rather than the current release version. Other projects you’re developing may require you to use an older Rust toolchain, due to breaking changes.
If you’d like StratusGrid’s team of high-caliber engineers to help you build and optimize software and/or automation tools in the cloud, please reach out to us through our contact form. Make sure you subscribe to our blog for more, similar articles!
BONUS: Find Out if Your Organization is Ready for The Cloud ⤵️