Comments (5)
For people looking for answer.
It's possible to get fixed port mappings using PortBindings
. See kafka example.
Also, got single container variant working:
resource, err := pool.RunWithOptions(
&dockertest.RunOptions{
Repository: "docker.io/bitnami/kafka",
Tag: "3.2",
Env: []string{
"KAFKA_ENABLE_KRAFT=yes",
"KAFKA_BROKER_ID=1",
"KAFKA_CFG_PROCESS_ROLES=broker,controller",
"KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER",
"KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093",
"KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT",
"KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092",
"[email protected]:9093",
"ALLOW_PLAINTEXT_LISTENER=yes",
},
ExposedPorts: []string{"9092/tcp"},
PortBindings: map[docker.Port][]docker.PortBinding{
"9092/tcp": {{HostIP: "localhost", HostPort: "9092/tcp"}},
},
})
from dockertest.
If I understand correctly, as dockertest uses fsouza/go-dockerclient, it should be possible to use a similar approach to allow for dockertest to offer this feature. Perhaps by allowing a ":" to be present in an ExposedPorts declaration such that a host port and container port can be specified e.g 9092:9092
Yes, this was actually the case until the v3 release, but was replaced in favor of dynamic port allocation which avoids port conflicts, which happened a lot. I'm open to having an option to force specific ports though.
from dockertest.
You need to set the port in Sarama to 9092, you can get the current port mapping by doing, in the case of mysql, resource.GetPort("3306/tcp")))
:
db, err = sql.Open("mysql", fmt.Sprintf("root:secret@(localhost:%s)/mysql?parseTime=true", resource.GetPort("3306/tcp")))
For kafka this is probably something like resource.GetPort("32911/tcp")
. You can dump the ports to stdout to see which values are available
from dockertest.
Thanks for the reply @arekkas.
In regards to mysql this would certainly be the solution, however things have gotten a bit trickier since trying dockertest with Kafka.
I use resource.GetPort("9092/tcp")
initially to get the port number of Kafka that Docker has assigned (e.g 32911). This is used for the successful initial connection. The challenge is that this is only used to get metadata from Kafka, after that Sarama uses the port number Kafka tells it about, which is the container port number and not the host port number:
Sarama Kafka
| |
| get metadata (localhost:32911) |
|------------------------------->|
| |
| broker on localhost:9092 |
|<-------------------------------|
| |
| consume topic (localhost:9092) |
|-------------------------------X|
| |
For the moment, I have "solved" the issue by using fsouza/go-dockerclient directly. This allows for specifying the host ports the container ports should be mapped to: https://github.com/adbourne/go-archetype-kafka-processor/blob/master/app_integration_test.go#L124
If I understand correctly, as dockertest uses fsouza/go-dockerclient, it should be possible to use a similar approach to allow for dockertest to offer this feature. Perhaps by allowing a ":" to be present in an ExposedPorts declaration such that a host port and container port can be specified e.g 9092:9092
. This would be a similar approach to the Docker cli. The logic could live here: https://github.com/ory/dockertest/blob/v3/dockertest.go#L119
from dockertest.
Same problem here. I understand issue with port conflicts during testing.
Is there way to know port beforehand so I can configure Kafka properly via environment variables?
Is there way to use some sort of substitutions in Env variables?
Is there way to run a command to form environment variable?
from dockertest.
Related Issues (20)
- Kafka example doesn't work HOT 4
- Containers exit with exit code 137 HOT 2
- First example test does not fail if Docker is not running
- Add ability to set custom User-Agent (or append to default) HOT 1
- How can I update pool.Client.Endpoint? HOT 1
- Set container IP address in network HOT 1
- Cannot find /var/run/docker.sock on Mac/Windows HOT 4
- How I can purge the resource after failure?
- cannot build on arch: mips64
- Compile error in oras-go dependency
- volumes not working with Podman HOT 1
- dockertest.Resource.Exec fails with "Unrecognized input header: 100" HOT 1
- Mount equivalent for `--volume /var` succeeds on Docker-CE/Linux, but fails on Docker Desktop for WSL2
- github.com/imdario/mergo module renamed to dario.cat/mergo
- Create an example for starting mongodb as a replica set HOT 3
- Error from docker.NewAuthConfigurationsFromDockerCfg() when auths is empty HOT 1
- Create an example for DynamoDB
- Failure to Validate Error for Violated Foreign Key Constraints
- Add support for docker-compose
- dynamic port binding with ExposedPorts option stopped working HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dockertest.