How to containerize a Rust app with Docker

How to containerize a Rust app with Docker Picture 1

Containerization provides the necessary benefits in terms of mobility, isolation, and efficiency. It allows consistent deployment of applications across different environments, and at the same time, ensures safety and stability through application isolation. It also optimizes resource usage, simplifies development and management, and enhances scalability.

Containerizing a Rust app with Docker provides a reliable and efficient method of packaging applications and dependencies into standalone and portable environments. It allows for seamless deployment on a variety of systems without regard to the underlying infrastructure.

Set up a simple web server in Rust with Actix

You can set up a simple web server in Rust with Actix and containerize the application with Docker. You will show a new port where you will access the server to receive queries.

Run this command to create a new Rust project with the Cargo package manager:

 

cargo new my-app

When creating a new Rust project, Cargo adds the cargo.toml file to the project's root directory. Open the cargo.toml file and add the Actix crate to the dependencies section as follows:

[dependencies] actix-web = "4.3.1"

Here's how you can set up a simple server in Rust with crate Actix:

use actix_web::{get, App, HttpResponse, HttpServer, Responder}; // Nhập các phần phụ thuộc cần thiết từ framework Actix Web #[get("/")] async fn hello() -> impl Responder { // Xác định hàm xử lý cho truy vấn GET tới đường dẫn gốc ("/") // Hàm này trả về một kiểu triển khai trait Responder HttpResponse::Ok().body("Hello, World!") // Trả về phản hồi HTTP với mã trạng thái 200 (OK) // và nội dung của phản hồi là "Hello, World!" } #[actix_web::main] async fn main() -> std::io::Result<()> { // Điểm nhập của ứng dụng HttpServer::new(|| { // Tạo phiên bản mới của HttpServer App::new().service(hello) // Tạo phiên bản mới của App và đăng ký hàm hello }).bind("127.0.0.1:8080")?.run().await // Liên kết server tới địa chỉ IP và cổng này // Khởi động server và đợi quá trình hoàn tất }

This program sets up a basic HTTP Web Server with Actix. The hello function is a function that handles the response to a GET query on port 8080 with 'Hello, World!'.

The main function sets up a server instance with the HttpServer::new function and binds the server to run on localhost 127.0.0.1:8080.

Now, run the cargo run command to run the web server. This is the result of opening the address on a web browser.

How to containerize a Rust app with Docker Picture 2

 

Writing Dockerfile for Rust app

To containerize a Rust app with Docker, you must create a Dockerfile and define commands for the containerization process.

Dockerfile has no extension. You just need to create a Dockerfile . You can also create .dockerignor files as abstract files in the active directory from the build process.

Define command in Dockerfile

The Docker file will contain commands to pull a base image from the Docker repository, set the working directory, copy the file, build dependencies/applications and create minimal thumbnails, export the port, and run the application.

# Dùng bản mới nhất của ảnh cơ sở Rust FROM rust:latest # Đặt thư mục đang hoạt động trong container sang container to /my WORKDIR /usr/src/my-app # Sao chép file dự án Rust sang thư mục đang hoạt động COPY . . # Xây dựng app Rust RUN cargo build # Đặt lệnh này để chạy app Rust CMD cargo run

After defining the required commands for app containerization, you can build a container with this command:

docker build -t my-app .

This command builds a Docker image for the app my-app with the tag my-app from the current directory.

You can use the dockerrun command to run Docker images.

docker run -p 8080:8080 my-app

The -p 8080:8080 option maps the host machine's port 8080 to the container's port 8080. Docker will forward traffic redirected to port 8080 on the host machine to port 8080 in the container.

How to containerize a Rust app with Docker Picture 3

You can send queries to this container over localhost port 8080 to invoke the web service.

Above is how to containerize a Rust app using Docker . Hope the article is useful to you.

5 ★ | 1 Vote

May be interested

  • Ways to handle errors in RustWays to handle errors in Rust
    you have many ways to fix bugs in rust. the article will summarize for you all the most effective methods to fix errors in rust.
  • How to safely check desktop applications with DockerHow to safely check desktop applications with Docker
    docker allows applications to run in their own sandbox world. these applications share resources but do not interfere with programs running on the system.
  • Things to know about Fearless Concurrency in RustThings to know about Fearless Concurrency in Rust
    rust is distinguished by features for high performance, safe and efficient concurrency support. rust's concurrency is based on the concept of 'fearless concurrency'.
  • Documenting a Rust project with mdBookDocumenting a Rust project with mdBook
    documentation plays an important role in the success of a project. it is a guide for programmers and users to better understand the project.
  • Rust - interesting programming language worth learningRust - interesting programming language worth learning
    rust - interesting programming language, this is a statement of many developers. there are many reasons why rust became the next language you should learn. consider the following reasons.
  • Common commands in DockerCommon commands in Docker
    docker is a computer program that performs os-level virtualization, also known as containerization.
  • How to set up a Rust environment on LinuxHow to set up a Rust environment on Linux
    start your rust development journey by setting up a rust development environment on your linux pc. here are detailed instructions.
  • What is Docker Compose? Summary of knowledge about Docker composeWhat is Docker Compose? Summary of knowledge about Docker compose
    docker compose is a tool used to define and run docker programs using multiple containers (multi-containers).
  • Top 3 Roblox games like RustTop 3 Roblox games like Rust
    rust is more than 7 years old but always receives a lot of attention from the gaming community around the world. what do you think of a survival experience similar to rust but in roblox's signature lego-like world? try 3 great roblox games like rust right away.
  • How to install Docker in LinuxHow to install Docker in Linux
    docker is a containerized utility that has become very popular, simplifying such tasks. moreover, when there is a problem with the operating system, instead of installing and reconfiguring the application, users only need to reinstall the operating system, copy the container again.