Hello. I am trying to build and run a small rust server image. I have the simple version up and running but the image size is 2.xgb, this is obviously too big. here is the working simple dockerfile
FROM rust
COPY . /app
WORKDIR /app
RUN cargo build --release
CMD ["./target/release/arb_data"]
I have been trying to shrink the image for a few days now and keep running into different issues. Either it won’t compile because of linux, it won’t run because its an exe or it just does nothing and I can’t spot why.
I have read the multistep docs, the rust image docs and examples and well and multiple blogs and posts at this point to no avail.
Does anyone have a dockerfile that could produce this small image with a rocket server inside?
Multi-stage build itself doesn’t solve everything and you need to use the same (or very similar: using the same base image) images for different stages so the required libraries are there on the target stage where you copy the built binary,
You also need to know how the build works, what files it creates and which files can be removed, which files you need to keep or copy. If there are files required for the build only, you can remove those files but make sure you do it in the same layer in which the build ran. In your case
RUN cargo build --release \
&& rm -rf /path/to/cache-folder
Otherwise the removable files will not be removed just hidden on the next layer.
If you can tell us more about these issues, then we may be able to help. For example what does “because of linux” means?
FROM rust:alpine AS build
COPY . /app
WORKDIR /app
RUN cargo build --release
FROM alpine as runtime
COPY --from=build /app/target/release/arb_data /
EXPOSE 8000
CMD ["./arb_data"]
this is my current Dockerfile. The error on compile is error: linking with `cc` failed: exit status: 1. or an error with openssl. I am very new to docker and as a windows user am finding debugging linux very difficult.
I did add the following to my cargo.toml but had no luck openssl = {version = "0.10", features = ["vendored"]}
Then the problem with the multi-stage build is that you don’t have the required libraries in the alpine image, but the rust image contains them. The rust image has gcc, a simmple alpine does not have it. Try to use the same base image in the runtime stage too.
I don’t know why it still wants to build something. What exactly arb_data is supposed to do? If copying that file into the same image is not enough, it indicates that arb_data needs some other files too which was generated or installed system-wide not in the target folder. Optimizing Docker images requires a deeper knowledge about the actual build process and I don’t have that in this case.
PS.: I also fixed your post because you accidentally missed a linebreak before closing the first code block. Please, check your post using the preview function and after sending it so you can fix it next time by editing your post