Unable to connect when server is in a docker container.
Closed this issue · 6 comments
What should the server address be when renet is inside a docker container? Everything I've tried doesn't work.
It depends where you are hosting the docker, but the public address in the server is always the IP that the client uses to send packets to the server.
If testing locally, when running the container you need to add the flag -p 8080:80/udp, this map the port 8080 from the host to the docker port 80, so now you would bind the udp socket to 127.0.0.1:80 but the server public_addr would be 127.0.0.1:8080 because that is the port that the client would actually send packets to.
https://docs.docker.com/config/containers/container-networking/
Here's what I have which doesn't seem to work. See anything wrong?
In docker-compose.yml I have
ports:
- "8080:8080/udp"
creating the server
fn new_renet_server() -> RenetServer {
let server_addr = "206.184.204.145:8080".parse().unwrap();
let socket = UdpSocket::bind("127.0.0.1:8080").unwrap();
let connection_config = server_connection_config();
let server_config = ServerConfig::new(64, 7, server_addr, ServerAuthentication::Unsecure);
let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
RenetServer::new(current_time, server_config, connection_config, socket).unwrap()
}connecting with client
fn connect_to_server (
mut commands: Commands,
) {
let server_addr = "206.184.204.145:8080".parse().unwrap();
let socket = UdpSocket::bind("127.0.0.1:0").unwrap();
let connection_config = client_connection_config();
let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
let client_id = current_time.as_millis() as u64;
let authentication = ClientAuthentication::Unsecure {
client_id,
protocol_id: 7,
server_addr,
user_data: None,
};
let client: RenetClient = RenetClient::new(current_time, socket, client_id, connection_config, authentication).unwrap();
commands.insert_resource(client);
}Can you try using let server_addr = "127.0.0.1:8080".parse().unwrap(); in both the server and client.
Not 100% sure, but don't Docker containers need to listen on 0.0.0.0 not 127.0.0.1 in order for expose to work?
Not 100% sure, but don't Docker containers need to listen on
0.0.0.0not127.0.0.1in order for expose to work?
I think you are right, you prob need to change the server bind to let socket = UdpSocket::bind("0.0.0.0:8080").unwrap();
Got it working. Had to change both client and server udp socket addresses to 0.0.0.0.
client
const SERVER_ADDRESS: &str = "206.184.204.145:8080";
const UDP_SOCKET_ADDRESS: &str = "0.0.0.0:0";server
const SERVER_ADDRESS: &str = "206.184.204.145:8080";
const UDP_SOCKET_ADDRESS: &str = "0.0.0.0:8080";thanks!