How to Set Up a Local Docker Environment with Rust and Rocket

germanlozickyj

Germán Lozickyj

Posted on November 17, 2024

How to Set Up a Local Docker Environment with Rust and Rocket

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
Enter fullscreen mode Exit fullscreen mode

src/main.rs

#[macro_use]

extern crate rocket;

#[get("/")]
fn home() -> &'static str {
    "Welcome to Rocket!"
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![home])
}


Enter fullscreen mode Exit fullscreen mode

Cargo.toml

[package]
name = "rocket"
version = "0.1.0"
authors = ["German Lozickyj"]
edition = "2021"

[dependencies]

rocket = { version = "0.5.0-rc.2"}
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

Docker Compose Overview

version: '3.3'

services:
  rocket:
    build: .
    volumes:
      - '.:/app'
    ports:
      - 8080:8080
    networks:
        - rocket

networks:
   rocket:
        external: true
Enter fullscreen mode Exit fullscreen mode

Now you can build and setup:

docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

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!

💖 💪 🙅 🚩
germanlozickyj
Germán Lozickyj

Posted on November 17, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related