Skip to content

Unable to use exposed port #750

Description

As a continuation of #749, I am trying to get a very simple example working. Unfortunately, I seem to be misusing the functions and am unable to figure out how to expose a port and then access the exposed port.

This is the example I am trying to make work. It is based on the example from the Quickstart.

$ cat src/main.rs
use testcontainers::{
    core::{IntoContainerPort, WaitFor},
    runners::AsyncRunner,
    GenericImage,
};

#[tokio::main]
async fn main() {
    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
        .unwrap();

    let host = container.get_host().await.unwrap();
    let port = container.get_host_port_ipv4(6379).await.unwrap();
    let url = format!("redis://{host}:{port}");
    println!("Redis is running on {}", url);
}

Unfortunately, it does not work.

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.07s
     Running `target/debug/asdf`
thread 'main' panicked at src/main.rs:17:57:
called `Result::unwrap()` on an `Err` value: PortNotExposed { id: "98ae2d954adcbbcc99dbeb7bcccfd5aa6d3507e0d3b36f741849d6b589934ec4", port: Tcp(6379) }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

How can a port be exposed and then used?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions