How to dynamically set a port (from a Testcontainers container)

Your call.

Regarding coding what you’re trying to do, it’s pretty straightforward.
Just don’t use the before hook and directly place all the code you need in the instance initializer.

{
  myTestcontainers.setup();
  // capture application port from getMappedPort by some mechanism, e.g. return it from that call and assign to a variable in the simulation class
  int myPort = ???;

 HttpProtocolBuilder httpProtocol = http
        .baseUrl("http://localhost:" + myPort)
        .acceptHeader("application/json")
        .userAgentHeader("Gatling");

  ScenarioBuilder getFoo = scenario("get foo")
            .exec(http("Get Foo")
                    .get("/foo")
                    .check(status().is(200)))
            .pause(Duration.ofMillis(500));
 
 setUp(getFoo.injectOpen(constantUsersPerSec(10).during(300)).protocols(httpProtocol));
}