TL;DR: We are going to install Docker and create five different containers for a Rust program, each one a little more complex than the other.
Hi...
For further actions, you may consider blocking this person and/or reporting abuse
For languages than can be statically compiled like Go or Rust, the best is to use "FROM scratch" base and only append the binary in the image. Your images will only be a few Mb sized.
I wanted to suggest scratch too. I once had made Rocket web app image as small as ~10MB. Probably it could be optimized even more.
Hi! Thank you both for your comments. I understand what you say and I think it is correct. The problem—specific to this tutorial—is that scratch would not work with the project I am using here; at least not without some extra work, which would lead me to fix a few things and explain them (e.g., I would need a static build, a similar problem that I would have with Alpine), which in turn would cross the threshold of a "first steps" beginner post.
Hopefully, people will read this and be aware of this alternative for their particular projects.
Ho, sorry, I thought that Rust makes static bainary by default. I just checked and I understand that I was wrong.
Reading this zderadicka.eu/static-build-of-rust... is a nice complement :)
Hey, Rust does make static binaries by default. But when you use a dependency, it'll bring their own dependencies, and you may end up using one that happens to load something dynamically... Like
libc
for example.Thanks for this. I to am just starting with rust. Going to rewrite a server of mine I wrote in py 2.7 so its time to update it. This looks a lot like the double build Dockerfile I use with my golang containers. Thanks again 😄
Using rust to rewrite a server made in Python is, IMHO, not the best way. Go is made for this kind of work, easier to use threads (and concurrency) and a lot more readable. It's closer to Python in syntax and you will have more or less the same performances than Rust for this kind of project.
You are correct and I agree with everything you are saying. To be a little clearer I plan to use Rust during the music server setup process and not necessarily the server itself. Is it a good design choice, probable not. Is it a good way to learn Rust , hopefully :)
If you feel encouraged to do that (and have the time), please share your results (and maybe even the process) of developing such a server :) I would love to read!
To say that Go is "more readable" depends on who you ask. Also, Rust has an excellent concurrency model.
When running
if you ran
you'd actually get smaller and cleaner layer as the rs files in src would not be part of it. By separating to two different layers you actually create one layer with deps+rs files and another layer that removes the rs files IMHO.
You have to use "&&" instead of "&", if you use a single & then the rm command would run before cargo builds, additionally you can remove this line too
and change that previous line too
This is interesting read. It would be useful to add a section related to building a Docker image for a Rust workspace as opposed to a regular project. For instance, my app consists of a workspace with three different projects, such that the Cargo.toml file is this:
[workspace]
members = [
"gooblen_lib",
"gooblen_cli",
"gooblen_api"
]
In turn, I have a corresponding Cargo.toml in each project folder (gooblen_lib, gooblen_cli, gooblen_api).
When building the Docker image, the system complains that
Is caching dependencies even supported when working with workspaces?
Thanks sharing your experience. Consider using Google Distroless as base image in Docker 5. Haven't seen more secure base image so far.
As said earlier, I would use "scratch" as base image (empty image) with only the binary inside ;)
You need to run
docker run hello-world
withsudo
for it to work correctly on Linux. Is this expected?Hi Matt! Please take a look at this: docs.docker.com/engine/install/lin...
Thanks!
What is this line for?
RUN rm ./target/release/deps/holodeck*
Hi Afonso! This removes the binary previously built, so when the last build for release is executed, Docker uses all the cached dependencies and only the application itself (which, in this scenario is the only thing that was changed) is rebuilt.
When I'm running this, I'm getting the error
cannot remove './target/release/deps/holodeck*': No such file or directory
. Anyone else getting this issue?Very helpful. I now understand how to make my images smaller! Thanks so much and great article!
I can't see the fifth docker file anywhere in this post. Am I the only one who can't see it?