1

I have a very strange issue with my Rust program that uses the rocket-rs library.

The issue I am facing is that when I try and build my program in a Docker container using a Dockerfile I created, some parts of the config I set out in the rocket.toml file is not applied. More specifically, I have set the log level option to critical in the config file and that is working but the address option I have set in the config file is not applied.

What is wierd is that I can build and all the options are applied on my local machine properly but not in the container.

Output when I build and run the program on my machine (no docker):

Configured for release.
   >> address: 0.0.0.0
   >> port: 8000
   >> workers: 12
   >> ident: Rocket
   >> keep-alive: 5s
   >> limits: bytes = 8KiB, data-form = 2MiB, file = 1MiB, form = 32KiB, json = 1MiB, msgpack = 1MiB, string = 8KiB
   >> tls: disabled
   >> temp dir: C:\Users\Nlanson\AppData\Local\Temp\
   >> log level: critical
   >> cli colors: true
   >> shutdown: ctrlc = true, force = true, grace = 2s, mercy = 3s

Output when I build and run the program in a docker container:

Configured for release.
   >> address: 127.0.0.1    //This is what I do not want
   >> port: 8000
   >> workers: 2
   >> ident: Rocket
   >> keep-alive: 5s
   >> limits: bytes = 8KiB, data-form = 2MiB, file = 1MiB, form = 32KiB, json = 1MiB, msgpack = 1MiB, string = 8KiB
   >> tls: disabled
   >> temp dir: /tmp
   >> log level: critical
   >> cli colors: true
   >> shutdown: ctrlc = true, force = true, signals = [SIGTERM], grace = 2s, mercy = 3s

Here is the Dockerfile I am using:

FROM rust as builder
WORKDIR /app
COPY . .
RUN cargo build --release

FROM rust as runtime
WORKDIR /app
COPY --from=builder /app/target/release/server .
COPY --from=builder /app/database.db .
EXPOSE 8000
CMD ["./server"]

and my rocket config file:

[global]
#address is not applied
address = "0.0.0.0"
#log level is applied
log_level = "critical"

I have tried a few things to trouble shoot this issue:

  • Run the container with docker run -it <container name> bash and check that all the required files including the config file is copied into the container
  • Build the program in the container through bash using different options.

Please let me know if I am missing any details.

Thanks in advance

nlanson
  • 357
  • 1
  • 3
  • 14
  • 1
    You say that you checked that the file is copied to the container, but I do not see any line that would copy `Rocket.toml` to the `runtime` image. Are you sure? – justinas Aug 07 '21 at 07:33
  • My bad I did not clarify. The ```COPY . .``` line copies the directory containing the project which includes ```Rocket.toml``` – nlanson Aug 07 '21 at 14:23
  • 1
    Yes, to the first container. But as you use a multi-stage build, your final image ends up with only the "server" binary and the database. However, Rocket.toml is required during runtime, not at compile time. – justinas Aug 08 '21 at 09:48

1 Answers1

0

You can create environment variable named ROCKET_ADDRESS in dockerfile. I am sharing an example

ENV_ROCKET_ADDRESS=0.0.0.0
EXPOSE 8000
CMD ["./server"]
adobean
  • 314
  • 2
  • 12