`testcontainers`: PermissionDenied

I'm trying the async example of testcontainers:

#[test]
async fn testcontainers() {
    let container = GenericImage::new("redis", "7.2.4")
        // .with_exposed_port(6379.tcp())
        .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections"))
        .start()
        .await
        .expect("Redis started");
}

Here is the output:

running 1 test
test testcontainers ... FAILED

failures:

---- testcontainers stdout ----
thread 'testcontainers' panicked at tests/integration_test.rs:33:10:
Redis started: Client(CreateContainer(HyperLegacyError { err: hyper_util::client::legacy::Error(Connect, Os { code: 13, kind: PermissionDenied, message: "Permission denied" }) }))
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    testcontainers

But I get PermissionDenied error, and I suspect it's because my Docker instance requires sudo. How do I run the test in sudo? Or is there a better way to fix this issue?

I solved it by adding this to .cargo/config:

[target.x86_64-unknown-linux-gnu]
runner = 'sudo -E'

That ends up running the code as root. You should probably set up docker to run as your regular user. See: Post-installation steps | Docker Docs

4 Likes

I have already set it up as a regular user and I can run commands in CLI without sudo. But when I try to call Docker from inside a Rust program (like running cargo t) then I get this issue