How to Dockerize and Deploy a Rust Application

by Didin J. on Aug 18, 2025 How to Dockerize and Deploy a Rust Application

Learn how to Dockerize and deploy a Rust web app with Actix Web. Build lightweight images, push to Docker Hub, and run on a VPS with ease.

Rust has become one of the most loved programming languages thanks to its performance, memory safety, and modern tooling. But when it comes to deploying Rust applications, developers often face the challenge of distributing their apps across different environments while keeping builds consistent and lightweight.

This is where Docker comes in. By Dockerizing your Rust application, you can package your code together with all its dependencies into a single container image. This ensures that your application runs the same way everywhere—from your local machine to staging servers and production environments.

In this tutorial, we’ll walk through how to:

  • Build a simple Rust application.

  • Create a multi-stage Dockerfile to reduce image size.

  • Run the containerized Rust app locally.

  • Deploy the Dockerized application to a remote environment.

By the end, you’ll have a ready-to-deploy Docker image for your Rust application, along with the knowledge to adapt the process for real-world projects.


Prerequisites

Before we get started, make sure you have the following installed on your system:

  • Rust (latest stable version). Install it via rustup:

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • Cargo (Rust’s package manager, included with Rust).

  • Optional but recommended:

  • A Docker Hub account (if you want to publish and deploy your image).

  • Docker. Download and install from Docker’s official site.

  • A code editor like VS Code, IntelliJ Rust, or your favorite IDE.

  • Basic familiarity with:

    • Rust (creating and running a simple app).

    • Docker (building and running containers).

👉 Next up, we’ll create a simple Rust application that we’ll later package into a Docker container.


Creating a Simple Rust Web Application with Actix Web

First, let’s create a new Rust project. Open your terminal and run:

cargo new rust-docker-app
cd rust-docker-app

This will generate a basic project structure:

rust-docker-app/
 ├─ Cargo.toml
 └─ src/
    └─ main.rs

Add Actix Web dependency

Open the Cargo.toml file and add actix-web:

[dependencies]
actix-web = "4"

Write a tiny web server

Now edit src/main.rs with the following code:

use actix_web::{ get, App, HttpResponse, HttpServer, Responder };

#[get("/")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello from Dockerized Rust with Actix Web!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    println!("Starting server at http://0.0.0.0:8085");
    HttpServer::new(|| { App::new().service(hello) })
        .bind(("0.0.0.0", 8085))?
        .run().await
}

Run the server locally

Compile and run the server:

cargo run

You should see output like:

Starting server at http://0.0.0.0:8085

Now open your browser and visit:
👉 http://localhost:8085

You’ll see:

How to Dockerize and Deploy a Rust Application - tiny web server

✅ At this point, we have a working Rust web application that we’ll soon Dockerize.


Writing a Multi-Stage Dockerfile

Inside your project root (rust-docker-app/), create a new file called Dockerfile:

# ---- Build Stage ----
FROM rust:1.80-slim AS builder

# Create a new empty shell project
RUN USER=root cargo new --bin app
WORKDIR /app

# Copy manifest first (for caching dependencies)
COPY Cargo.toml Cargo.lock ./

# Build dependencies only (to cache them)
RUN cargo build --release || true

# Now copy the source code
COPY src ./src

# Build for release
RUN cargo build --release

# ---- Runtime Stage ----
FROM debian:bullseye-slim AS runtime

# Install minimal runtime dependencies
RUN apt-get update && apt-get install -y \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy the compiled binary from the builder stage
COPY --from=builder /app/target/release/rust-docker-app .

# Expose port 8085
EXPOSE 8085

# Run the binary
CMD ["./rust-docker-app"]

Why multi-stage?

  • The builder image (Rust official image) compiles the app.

  • The runtime image (Debian slim) is small and only contains the binary + required libraries.

  • This reduces the final image size from ~1GB to under ~100MB.


Building the Docker Image

Run this command in your project root:

docker build -t rust-docker-app .

This will go through two stages: compiling your app, then producing the final lightweight image.


Running the Dockerized App

Once the build is done, run your container:

docker run -p 8085:8085 rust-docker-app

You should see:

Starting server at http://0.0.0.0:8080

Now, open your browser and visit 👉 http://localhost:8080.
You’ll see the response:

How to Dockerize and Deploy a Rust Application - dockerize rust

✅ Congrats! You’ve successfully Dockerized a Rust web app with Actix Web.

Next up, we can move on to deployment — pushing the image to Docker Hub and running it on a remote server/cloud.


Deploying the Dockerized Rust Application

1. Tagging and pushing the image to Docker Hub

First, log in to Docker Hub:

docker login

You’ll be prompted for your Docker Hub username and password.

Next, tag your local image with your Docker Hub username:

docker tag rust-docker-app <your-dockerhub-username>/rust-docker-app:latest

Now push it:

docker push <your-dockerhub-username>/rust-docker-app:latest

Once complete, your image is available on Docker Hub. You can verify it by visiting your Docker Hub repository page.

2. Preparing your VPS

On your VPS (e.g., Ubuntu 22.04 server):

  • Install Docker (if not already installed):

    sudo apt update sudo apt install -y docker.io sudo systemctl enable --now docker
  • Verify Docker is running:

    docker --version

3. Pulling and running the app on the VPS

Now, pull the image you pushed earlier:

docker pull <your-dockerhub-username>/rust-docker-app:latest

Run the container, mapping the port 80 on the VPS to the app’s internal 8080:

docker run -d -p 80:8080 <your-dockerhub-username>/rust-docker-app:latest
  • -d runs in detached mode.

  • -p 80:8080 maps port 80 (public web) → 8080 (inside container).

4. Testing the deployment

Now open your VPS IP address in a browser:

http://<your-vps-ip>/

You should see:

Hello from Dockerized Rust with Actix Web!

🎉 Your Rust application is live on the internet!

5. Next Steps (Optional)

  • Use a domain name: Point a domain to your VPS IP and set up DNS.

  • Add HTTPS with Nginx + Let’s Encrypt (or use Caddy).

  • Automate deployment with GitHub Actions or GitLab CI/CD.

  • Scale with Docker Compose or Kubernetes if your app grows.

✅ With this, we’ve gone full cycle: built a Rust web app → Dockerized it → pushed it to Docker Hub → deployed it on a VPS.


Conclusion

In this tutorial, you learned how to take a Rust application from local development to production with Docker. We:

  • Built a simple Actix Web server in Rust.

  • Created a multi-stage Dockerfile to produce a lightweight, production-ready image.

  • Ran the container locally to confirm everything worked.

  • Pushed the image to Docker Hub and deployed it on a VPS.

By containerizing your Rust apps, you gain the benefits of consistent environments, simplified deployment, and easier scaling across different platforms. Whether you’re deploying to a personal VPS, a cloud provider, or orchestrating with Kubernetes, the Dockerized workflow remains the same.


Next Steps

Now that you’ve successfully Dockerized and deployed a Rust web app, here are some directions to explore:

  • Add HTTPS using Nginx or Caddy as a reverse proxy with Let’s Encrypt.

  • Automate deployments using CI/CD pipelines (e.g., GitHub Actions, GitLab CI, or Jenkins).

  • Use Docker Compose to run multi-container setups (e.g., Rust app + PostgreSQL).

  • Explore Kubernetes if you want to scale your Rust microservices.

  • Experiment with smaller base images (e.g., Alpine with musl) for ultra-light builds.

With Docker as part of your Rust workflow, you’re ready to ship applications that are portable, reproducible, and production-ready. 🚀

You can get the full source code on our GitHub.

That is just the basics. If you need more deep learning about the Rust language and frameworks, you can take the following cheap course:

Thanks!