Multistep rust alpine

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?

here is the github repo of the working rust api GitHub - putintin420/arb_data

By “multistep” do you mean “multi-stage”?

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?

thanks for the response.

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.


COPY . /app

WORKDIR /app

RUN cargo build --release

FROM rust:alpine as runtime

COPY --from=build /app/target/release/arb_data /

EXPOSE 8000

CMD ["./arb_data"]

here is the updated dockerfile. still failing at link with ‘cc’

here is the full error message

error: linking with `cc` failed: exit status: 1
#8 41.16   |
#8 41.16   = note: "cc" "-m64" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.0.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.1.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.10.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.11.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.12.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.13.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.14.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.15.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.2.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.3.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.4.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.5.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.6.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.7.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.8.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.time_macros.b8ff2989-cgu.9.rcgu.o" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.3bx9576ud7z3uaf.rcgu.rmeta" "/app/target/release/deps/time_macros-efb7fde6ceb274c5.4d31qwi8e7uny4lo.rcgu.o" "-Wl,--as-needed" "-L" "/app/target/release/deps" "-L" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,-Bstatic" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libproc_macro-08015c7b4a795a6e.rlib" "-Wl,--start-group" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-2c0ff204c579e132.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-d0915314d07d6d36.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-99eb98cb8036c0ac.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libmemchr-33a08e03781a81ec.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-cf39d7df9a8f82a7.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-28357830b23e2fef.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-0b91567160d0bd5f.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-e4f469286cdd380f.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-490774d955e3e803.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-b93dd1f288009466.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-f9f641e713eb7197.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-f2a6926cd25d276e.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-d667f5a0aef5d8cf.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-1ff32a3f72a57c9d.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-43c83f88d784cf11.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-88a075cf6c777e0d.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-ab55f55aa6424772.rlib" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-a210d209c558541d.rlib" "-Wl,--end-group" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-c9e9dccd3a48e06a.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/usr/local/rustup/toolchains/1.62.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-o" "/app/target/release/deps/libtime_macros-efb7fde6ceb274c5.so" "-Wl,--gc-sections" 
"-shared" "-Wl,-zrelro,-znow" "-Wl,--strip-all" "-nodefaultlibs"
#8 41.16   = note: /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find crti.o: No such file or directory
#8 41.16           collect2: error: ld returned 1 exit status
#8 41.16
#8 41.16
#8 41.18    Compiling rocket v0.5.0-rc.2
#8 41.22    Compiling lock_api v0.4.7
#8 41.24    Compiling indexmap v1.9.1
#8 41.34 error: could not compile `time-macros` due to previous error
#8 41.34 warning: build failed, waiting for other jobs to finish...
------

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 :slight_smile: