Containerize Rust Application with Docker

Containerization is a lightweight alternative to a virtual machine that involves encapsulating an application in a container with its own operating system.
So here I acquaint you all with containerizing Rust Application with Docker.

Docker is a tool that allows users to easily deploy their applications in a container to run on the host operating system.

It allows users to package an application with all of its dependencies into a standardized unit for software development. Docker gives you the ability to write your rust code once and run everywhere you want.
Because Rust provides us a binary file after compilation of Rust code then it’s quite easy to build a docker image of Rust application.
To build and run a docker image you’ve to follow certain steps:

  • Write Rust code
  • Build Rust Code
  • Write docker file
  • Docker build (with docker file)
  • Run docker image

Step 1: Write Rust Code

Writing Rust program is the very first step to go with containerization. Lets start with a simple rust program.

fn main() {
    println!("Congratulations your application is successfully containerized with Docker!!");
}

Step 2: Build Rust Code

Build rust code means to generate a complied file of a rust program.
Rust provides us a build tool called as cargo which helps for compilation of Rust application.

cargo build

cargo build is used for building rust program or application.
After successful compilation, a binary file will be created inside a debug folder of the target folder (docker-program->target->debug->docker-program).

Step 3: Write docker file

Docker file is the main source of the building a docker image. It is basically a configuration file of the docker image.
The default name for docker file is Dockerfile although we can change its name, here we’ll go with the default naming convention.
There are some steps for writing a docker file.

FROM rust

COPY docker-program /bin/docker-program

CMD ["/bin/docker-program"]
	

In the above file, we’ve used:
FROM: Sets the Base Image for subsequent instructions.
COPY: Copies new files or directories to the container.
CMD: Specifies what command to run within the container.

We have created our Dockerfile in the target folder, parallel to the binary file.
In Dockerfile we’ve used Rust’s official image as a base image and copy it to the container’s bin directory.
After COPY instruction we use the CMD to specify the command which will be executed when we run our docker image.

Step 4: Docker build (with docker file)

After creating a Dockerfile the next step is to build the docker file to create a docker image. To build a Docker image we have to run docker build command.
With docker build, we have to provide some instructions which are:

docker build -t docker-image-name Dockerfile-path

-t: used to give docker image name and optionally a tag in the ‘name:tag’ format
So to build our image we have to run docker build command like:

docker-program/target/debug$ docker build -t docker-program-image .
Sending build context to Docker daemon  2.741MB
Step 1/3 : FROM rust
 ---> fb4df5f8070c
Step 2/3 : COPY docker-program /bin/docker-program
 ---> 009ede7d76f5
Step 3/3 : CMD ["/bin/docker-program"]
 ---> Running in 6b9c7b9ad6c8
Removing intermediate container 6b9c7b9ad6c8
 ---> 522077e09af3
Successfully built 522077e09af3
Successfully tagged docker-program-image:latest

Here docker-program is the docker image name and ‘.’ is used to point to the current directory[because Dockerfile exist in the same directory]

Step 5: Run docker image

After building a docker image the final step is to run the docker image.
Before running a docker image please ensure that our docker image is reflecting in the list of docker images or not. To check the docker images docker images command is used.

docker-program/target/debug$ docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
docker-program-image             latest              522077e09af3        11 seconds ago      1.68GB
bishtdocker/rasp_image           latest              a1a1a8603fde        21 hours ago        473MB

Our Docker image is shown in the list of images, now we are ready to run our docker image by using docker run command.

$ docker run docker-program-image
Congratulations your application is successfully containerized with Docker!!

Hope you all will get acquaint with containerize Rust Application.
Thanks for reading!


Knoldus-blog-footer-image

Written by 

Pawan Singh Bisht is the Trainee Software Consultant at Knoldus Software LLP. He has good knowledge of languages C++ and Java, and currently working on Rust. He is always ready to accept new challenges. As a fresher, he always tries to explore the different type of software and tools.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!