-1

Recenly I have started to learn network programming. I have written simple server programm

  fn main() {
    println!("Start server");
    let server = TcpListener::bind("0.0.0.0:7878").unwrap();
    match server.accept() {
        Ok((_socket, addr)) => {
            println!("new client: {:?}", addr);
            loop {
              //do something
            }
        }
        Err(e) => { println!("couldn't accept the client: {:?}", e) }
    }
    println!("server disconnect")
}

and now I want run it in docker container and make it accessible from internet. what I mean is when I run docker container I want to connect to the tcp listener from another computer.

After running docker container I try to connect to that port using my public ip address. But I always get Timeout error

to run container I use docker run --rm -dp 7878:7878 appname

when I connect to that port using localhost everythign works fine

d.popovych
  • 359
  • 1
  • 3
  • 9
  • 2
    The `docker run` command in [your earlier question](https://stackoverflow.com/questions/66725777/how-to-connect-to-rust-server-app-that-runs-on-docker), with the bind address fix discussed there, should do it; you can connect to the application using the host's IP address and the first `docker run -p` port number. (NB: you would _also_ need to change the bind address to 0.0.0.0 if you were running the application outside of Docker if you wanted to access it from other hosts.) – David Maze Mar 21 '21 at 02:01
  • @DavidMaze I am not sure how it helps me. I want to bind tcp listener to my home pc on port 7878 and after that I need to connect to that port and send some msg from any pc in internet. If it seems like really newbie question I would really appreciate if you suggest some book to read about that theme. – d.popovych Mar 21 '21 at 08:37
  • solved by buying static ip address – d.popovych Mar 22 '21 at 05:49

2 Answers2

0

You need to run any kind of server your host machine, and then do port binding when running docker container.

For example, run server on your host machine, expose port 5000 on this server, then run

docker run -p 5000:<docker_app_port> container

This will create some sort of proxy, whenever someone from another IP is connecting to your host machine's IP, to port 5000, will be redirected through port expose to your docker container.

Misieq
  • 507
  • 2
  • 12
0

I would recommend taking a look at the docker-compose command. Don't know any Rust (the language of your program) but it looks like it is listening to port 7878 on localhost. There's probably a Dockerfile you have already running. docker-compose can expose the port 7878 to the host the container is running on:

docker-compose.yml:

version: "3.9"

services:

  web:
    build: .
    container_name: "rust_container"

    ports:
      - "3000:7878"

Run it with

docker-compose up 

Notice the ports: "3000:7878". This means that in the Docker Container the port 7878 is published on the Host on port 3000. The container is therefore accessible on http://localhost:3000/

Put in your Cargo.toml, the Dockerfile and all other necessary files in the description. Then I can create a working example.

BertC
  • 2,243
  • 26
  • 33