Building a Local Development Environment with Rust and Rocket Using Docker
In this post, we'll explore how to set up a local development environment for a Rust web application using the Rocket framework and Docker. The setup includes automatic recompilation and logging whenever you update your code. Here's how it works, step by step.
Table of Contents
Introduction
When building web applications in Rust, having a reliable local environment is crucial for productivity. This Docker setup enables:
-
Automatic code recompilation on file changes using
cargo-watch
. - Log management, so you can track application output efficiently.
With this setup, you can focus on writing code while Docker handles the environment.
Directories
Below is the directory structure for the project:
.
├── Cargo.lock
├── cargo.log
├── Cargo.toml
├── docker-compose.yml
├── Dockerfile
└── src
└── main.rs
src/main.rs
#[macro_use]
extern crate rocket;
#[get("/")]
fn home() -> &'static str {
"Welcome to Rocket!"
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![home])
}
Cargo.toml
[package]
name = "rocket"
version = "0.1.0"
authors = ["German Lozickyj"]
edition = "2021"
[dependencies]
rocket = { version = "0.5.0-rc.2"}
Dockerfile Overview
FROM rust:1.74.1
ENV ROCKET_ADDRESS=0.0.0.0
ENV ROCKET_PORT=8080
WORKDIR /app
COPY . .
EXPOSE 8080
RUN rustup default nightly
RUN cargo install cargo-watch
CMD ["sh", "-c", "cargo watch -x run > cargo.log 2>&1"]
Docker Compose Overview
version: '3.3'
services:
rocket:
build: .
volumes:
- '.:/app'
ports:
- 8080:8080
networks:
- rocket
networks:
rocket:
external: true
Now you can build and setup:
docker-compose up --build
Check if It works https://localhost:8080/
How It Works
Live Reloading
cargo-watch
listens for file changes and automatically recompiles the Rust project. This eliminates the need to manually restart the server, speeding up your workflow.Logs for Debugging Logs are written to cargo.log, ensuring that all output (including errors) is accessible for review.
Conclusion
This setup is perfect for developing web applications with Rust and Rocket in a Dockerized environment. With automatic recompilation, integrated logging, you can focus on building features instead of managing your environment.
Feel free to clone this configuration and adapt it to your needs!
Top comments (0)