I run development using --config
file on my Rust API, it is something like this
cargo run --config dev-config.toml
where the dev-config.toml is like my Environments
like this
[env]
APP_ENV = "development"
APP_PORT = "8181"
APP_TIMEOUT = "3" # in seconds
APP_MAX_ATTEMPTS = "50" # in attempts ( 50 request in 60 seconds )
MONGO_URI="balblaaa"
...
I can build image in my docker like this to copy the file
# Use the official Rust image as the base image
FROM rust:latest as builder
# Set the current working directory inside the Docker image
WORKDIR /usr/src
# Copy the Cargo.toml file and your source code into the Docker image
COPY Cargo.toml .
COPY src ./src
COPY secrets ./secrets
# Copy the dev-config.toml file
COPY dev-config.toml .
# Build the application in release mode
RUN cargo build --release --config dev-config.toml
# Start a new build stage
FROM rust:latest
# Copy the binary from the builder stage to the current stage
COPY --from=builder /usr/src/target/release/my-api /usr/local/bin
# Set the command to run your application
CMD ["my-api"]
when I run docker run -p 8080:8080 my-api
I got error the env was empty and not set
[INFO] - "Trying to Connect MongoDB..."
thread 'main' panicked at src/db.rs:43:75:
MONGO_URI must be set.: NotPresent
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
is that something I miss here and other way to use --config
? instead set manual env
I have been read cases on internet, but there is none of my topic out there because rust is new,
The [env]
from using a cargo --config
file only affects cargo invocations with it (i.e. cargo build
, cargo run
, etc.). So you could make this work as-is by using:
CMD = ["cargo", "run", "--release", "--config", "dev-config.toml"]
Though I don't actually recommend that even if cargo build
was ran beforehand.
Instead, I would suggest reading How do I pass environment variables to Docker containers? Options there like using a --env-file
or launching with a docker-compose
configuration would likely be more palatable. If you go with a .env
file, you could change your Rust code to load from it using the dotenvy crate instead of provided by cargo's config.