All Dockerizers

Rust Dockerfile generator

Generate a production Dockerfile for a Rust service with a cached cargo registry and target directory, shipping a single binary.

Configure

Used for the image tag, the compose service and the OCI labels.
The port the app listens on inside the container. Keep it above 1024 so the process can bind it without root.
The [[bin]] name from Cargo.toml — usually the package name.
Adds the database to docker-compose.yml with a healthcheck, a named volume and a DATABASE_URL wired into the app.
Adds Redis to docker-compose.yml and exposes REDIS_URL to the app.

Loading...

What this Dockerfile does

Nothing flagged

  • Good practice: Multi-stage build (2 stages)

    Compilers, dev dependencies and source files stay in the build stages. Only the runtime artefacts are copied into the final image.

  • Good practice: Runs as nonroot, not root

    A container escape from a root process is a host root process. Dropping privileges is the cheapest hardening step there is.

  • Good practice: .dockerignore keeps secrets out of the build context

    `.git`, `.env` and local dependency folders never reach the daemon, so they cannot end up in a layer or in `docker history`.

  • Good practice: Dependency cache persists across builds

    BuildKit cache mounts keep the package manager store outside the image. Rebuilds after a lockfile change reuse everything already downloaded, and the cache adds nothing to the final size.

  • Good practice: Healthcheck defined

    Docker, Compose and Easypanel can tell the difference between a running process and a working app, and restart it when it wedges.

  • Good practice: Registry and target directory both cached

    Cargo rebuilds are the slowest part of a Rust image. Caching both /usr/local/cargo/registry and target/ turns a ten-minute rebuild into seconds when only your own code changed.

  • Good practice: `--locked` refuses to update Cargo.lock

    The build fails rather than silently resolving different versions than the ones you tested.

  • Note: Requires BuildKit

    BuildKit is the default builder in Docker 23 and later. On anything older, export `DOCKER_BUILDKIT=1` before building.

  • Note: distroless/cc, not distroless/static

    Rust links against glibc by default. Use the musl target if you want a truly static binary that can run on scratch.

How to use it

Everything runs locally. Nothing is uploaded, and no account is needed.

  1. Drop the files into your project root

    Every generated file belongs next to your package manifest — the Dockerfile references paths relative to the build context, so nesting them in a subfolder breaks the COPY lines.

  2. Build the image

    docker build -t rust-app .
  3. Run it

    docker run --rm -p 8080:8080 rust-app

Rust in Docker

map illustration

Join 145,000+ self-hosters.

Free plan available. No credit card required.

Install Easypanel