me
Published on

How to deploy actix with docker

Authors
  • avatar
    Name
    Omer Atagun
    Twitter

It's been a while fiddling rust and its beauty on local. Time has come to create a barebone docker build to see what are the benefits and what actually has to be done to make it happen.

Building the image

Well, our beautiful rust application is compiled to binary, so we do not need anything, thus our image size will be very small :) to accomplish a very resource low container, we start first with bringing us rust compiler image

FROM rust:1.65.0 as builder

now we can use this as our builder to build our image into the platform we desire then move onto our alpine container.

We need muslC in order to build our rust

RUN apt-get update &&
apt-get -y install ca-certificates cmake musl-tools libssl-dev &&
rm -rf /var/lib/apt/lists/*

We can copy our project folder into builder image by doing;

COPY . .

Finally, we can start to get neccesary packages in order to build our rust

RUN rustup default nightly && rustup update

RUN rustup target add x86_64-unknown-linux-musl

Allow cross compilation

ENV PKG_CONFIG_ALLOW_CROSS=1

Start building release for x86_64 architecture with musl

RUN cargo build --target x86_64-unknown-linux-musl --release

Up until now, all should be smoothly done. Compilation might differ based on the size of your application. In our case, it was hello world web app, so its size is about 10-11mb

Lets get ourselves a alpine

FROM alpine:3.8

RUN apk --no-cache add ca-certificates

After all, we are ready to copy our compiled binary into the alpine image, not everything. change web-backend to your application name declared in config.toml by you.

COPY --from=builder /target/x86_64-unknown-linux-musl/release/web-backend .

Expose the port that you are going to use ( should already be determined in your application ). In my case i have used 8080 which was default by actix-web

EXPOSE 8080

Finally, we run our compiled binary inside the container, and we are done :)

// of course change the name to your app name
CMD ["/web-backend"]

Lets investigate how our image looks like;

Results

In my hello world app,

  • Container size 16MB ( 11MB application )
  • i have 3.5MB ram usage
  • In total there are only 2 processes running inside the container. first is our app, second /bin/sh and

I have just ran apache benchmark for the fun of it to see without rate limiting how far it could go. Well yeah, needless to say, 21k per sec with time per request 0.047miliseconds.

Is not it just fun :)

Lazy support

Here is the entire Dockerfile for you my friend,

FROM rust:1.65.0 as builder

# muslc is required in order to build the rust image.
RUN apt-get update && apt-get -y install ca-certificates cmake musl-tools libssl-dev && rm -rf /var/lib/apt/lists/*

COPY . .

RUN rustup default nightly && rustup update

RUN rustup target add x86_64-unknown-linux-musl
# Sets the environment variable for the cargo build command that follows.
ENV PKG_CONFIG_ALLOW_CROSS=1

RUN cargo build --target x86_64-unknown-linux-musl --release


FROM alpine:3.8


RUN apk --no-cache add ca-certificates
COPY --from=builder /target/x86_64-unknown-linux-musl/release/web-backend .

EXPOSE 8080
CMD ["/web-backend"]

Rest of the stuff you can add as you are doing for the other applications, exposing your env or doing some stuff whatsoever. I gave you the one working nicely :)

Till next time, stay hydrated