Home > Blockchain >  How can I build and run my Actix application using cargo in docker?
How can I build and run my Actix application using cargo in docker?

Time:04-16

Second part of my question: Is it wise to do that to start my application? Or you think it's better to build the app on my local machine and then use the binary file in docker? Is build release final file work on PaaS docker as well as on my windows machine if I use binary file?

CodePudding user response:

Of course, you can. But In my opinion, it may not be a good choice.

First, you can find two docker images: rust and rustlang/rust. The first one is for the stable channel use and the second one is for the nightly channel use. You can build and run programs in these images just as in a Debian/Alpine machine.

But these two images have a full rust environment, so they might be too big. If you wish for a much smaller image, it's better to use the docker multi-stage builds to put the binary in a new and clean image. A Dockerfile might be like this:

FROM rustlang/rust:nightly-alpine AS build

WORKDIR /usr/src/MyApp
COPY . .
RUN apk add --no-cach musl-dev && \
    cargo install --path .

# FINAL

FROM rustlang/rust:nightly-alpine

COPY --from=build /usr/local/cargo/bin/myapp /usr/local/bin/myapp
ENTRYPOINT [ "myapp" ]

It is slow to build in this way since docker cannot cache the build stage of cargo. Take a look at cargo-chef to get a solution.

For the last question, sorry currently I have no experience so I don't know the actual answer. But I assume it is fine if you correctly cross-compile for the target platform.

  • Related