We can deploy a Rust application based on the Actix-Web framework in a Docker container when we are coding in Windows. In this post, we compile Rust codes on Windows but deploy the application to an Ubuntu 18.04 Docker image.
Docker Container to Build Codes for Ubuntu 18.04
First, we need a container that builds Rust codes in Ubuntu 18.04. This ensures that the codes will run on the same operating system running in a separate container.
Sample Actix-Web Project To Deploy
Next, we create a sample Actix-Web project.
1 2 3 4 5 6 7 8 9 | [package] name = "actix-web-docker" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" [dependencies] actix-web = "2.0.0" actix-rt = "1.1.1" |
Then, we have Rust codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | use actix_web::{App, get, HttpServer, Responder, HttpResponse}; #[get("/html")] async fn html() -> impl Responder { HttpResponse::Ok() .content_type("text/html") .body("<html><head><title>Turreta.com</title></head><body><h1>Hello from Turreta.com</h1></body></html>") } #[actix_rt::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| App::new() .service(html) ) // .bind("0.0.0.0:8080")? .bind(("0.0.0.0",8080))? .run() .await } |
Finally, create a DockerFile to build an Ubuntu 18.04 Docker image that has our Rust application.
1 2 3 4 5 6 7 8 9 10 11 12 | FROM ubuntu:18.04 RUN apt-get update && apt-get install -y curl RUN apt-get install build-essential -y RUN mkdir -p /actix-web/www WORKDIR /actix-web/www COPY target/debug/actix-web-docker /actix-web/www/actix-web-docker # EXPOSE 8080 - Don't use this if you want the web server accessible from the Windows host CMD ./actix-web-docker |
Build Codes On Windows Using Ubuntu 18.04 Docker Container
Let’s say we have an Actix-Web project in this folder – C:\Users\karl\Desktop\dev\blog\rust2\actix-web-docker, open a command-line window to change to that directory.
Then, run the following command to build the codes for Linux.
1 | docker run --rm --name rustc -v C:\Users\karl\Desktop\dev\blog\rust2\actix-web-docker:/user/turreta-rust-builder/src turreta-rust-builder:1.0 cargo build |
The command generates the following output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | C:\Users\karl\Desktop\dev\blog\rust2\actix-web-docker>docker run --rm --name rustc -v C:\Users\karl\Desktop\dev\blog\rust2\actix-web-docker:/user/turreta-rust-builder/src turreta-rust-builder:1.0 cargo build Updating crates.io index Downloading crates ... Downloaded actix-tls v1.0.0 Downloaded bytestring v0.1.5 Downloaded bytes v0.5.5 Downloaded cc v1.0.57 Downloaded crc32fast v1.2.0 Downloaded dtoa v0.4.6 Downloaded failure_derive v0.1.8 Downloaded futures-channel v0.3.5 Downloaded failure v0.1.8 Downloaded itoa v0.4.6 Downloaded pin-utils v0.1.0 Downloaded mio-uds v0.6.8 Downloaded memchr v2.3.3 Downloaded proc-macro-nested v0.1.6 Downloaded heck v0.3.1 Downloaded scopeguard v1.1.0 Downloaded signal-hook-registry v1.2.0 Downloaded serde_urlencoded v0.6.1 Downloaded slab v0.4.2 ... Compiling actix-testing v1.0.1 Compiling trust-dns-resolver v0.18.0-alpha.2 Compiling actix-connect v1.0.2 Compiling actix-http v1.0.1 Compiling awc v1.0.1 Compiling actix-web v2.0.0 Compiling actix-web-docker v0.1.0 (/user/turreta-rust-builder/src) Finished dev [unoptimized + debuginfo] target(s) in 11m 43s C:\Users\karl\Desktop\dev\blog\rust2\actix-web-docker> |
Once we built the codes, we get the following files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Directory of C:\Users\karl\Desktop\dev\blog\rust2\actix-web-docker\actix-web-docker\target\debug 03/07/2020 02:00 PM <DIR> . 03/07/2020 02:00 PM <DIR> .. 03/07/2020 01:49 PM 0 .cargo-lock 03/07/2020 01:49 PM <DIR> .fingerprint 03/07/2020 02:00 PM 93,853,696 actix-web-docker 03/07/2020 02:00 PM 105 actix-web-docker.d 03/07/2020 01:49 PM <DIR> build 03/07/2020 02:00 PM <DIR> deps 03/07/2020 01:49 PM <DIR> examples 03/07/2020 01:58 PM <DIR> incremental 3 File(s) 93,853,801 bytes 7 Dir(s) 372,356,173,824 bytes free |
Our Linux executable is the file actix-web-docker, which we deploy to an Ubuntu 18.04 Docker image. Then, run the following command.
1 | docker build -t turreta-rust-actix-web-env:1.0 . |
The command generates the following output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | Sending build context to Docker daemon 2.034GB Step 1/7 : FROM ubuntu:18.04 ---> 8e4ce0a6ce69 Step 2/7 : RUN apt-get update && apt-get install -y curl ---> Using cache ---> 7d0901bd266e Step 3/7 : RUN apt-get install build-essential -y ---> Using cache ---> 928186b00c6d Step 4/7 : RUN mkdir -p /actix-web/www ---> Using cache ---> efd92fda48f7 Step 5/7 : WORKDIR /actix-web/www ---> Using cache ---> ca4ebcf96c8c Step 6/7 : COPY target/debug/actix-web-docker /actix-web/www/actix-web-docker ---> Using cache ---> 482134082ebd Step 7/7 : CMD ./actix-web-docker ---> Running in df7af7ca23e7 Removing intermediate container df7af7ca23e7 ---> 82b3bd56b1c2 Successfully built 82b3bd56b1c2 Successfully tagged turreta-rust-actix-web-env:1.0 SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. |
Run Docker Container
To start our application, start a Docker container using the following command.
1 | docker run --rm -p 8181:8080 --name actix-web-env turreta-rust-actix-web-env:1.0 |
Then, open a web browser on Windows and go to http://localhost:8181/html.
In summary, we are coding in Windows but built the codes using an Ubuntu 18.04 image with Rust compiler. Also, we deployed that application to another Ubuntu 18.04 image.