How send POST request to a localhost from mobile device? - docker

I'm trying to implement a login in react native + flask app. I have a docker container for my server API. I've run it on my localhost in my computer. I'm also running my front-end on expo. Since it's a different host, how can i send POST request from my mobile device (expo) to the server running on localhost in my computer?
Docker server runs in port 5000 in container. I've mapped the local port 5000 to container's 5000 port.

First of all I hope you are exposing the correct server ports in the docker container.
Then, it can be easily done by using you local machine IP address in your home network instead of "localhost". Maybe your Firewall settings must be changed.
For React I usually save the server IP in an .env, so for production I can easily switch the server

Related

Can't connect to docker container running minecraft server online

So I've been trying to host my own Minecraft server for a while now and I hit a snag.
I have proxmox (192.168.2.100) running an ubuntnu server VM (192.168.2.101) which has a docker container running my Minecraft server. I can connect to the server locally just fine using the ubuntu's IP address and minecraft port, but when I try to port forward the server, I can't connect to it. I checked to see if my port was exposed or not and it is so I know it's not that.
This is the container that I'm using
Okay, so after a long time looking at configurations and some help from the proxmox forum, it turns out that my modem doesn't support NAT reflection which means that any attempts to access my server though my public IP on the same network wouldn't work at all. I used my phone's mobile network to test if I could access the server from outside my local network and it worked just fine!

Exposing a docker container to the internet

I deployed a ghost blogging platform on my server using docker. Now I want to expose it to the internet but I'm having some difficulties doing so.
I opened port 8000 in my router a forwarded it to port 32769 which is the one assign to that container. Using port 32769 inside my network I can access the website fine but when I try to access it from the internet it gives a took too long to respond error.
Local IP + PORT: http://10.0.0.140:32769/
Docker port config
Port tester
Router settings
This post was also added to Super User since it has been said that it would be responded better in there.
Let's say your application inside docker is now working on port 8000
You want to expose your application to internet.
The request would go: internet -> router -> physical computer (host machine) -> docker.
You need to export your application to your host machine, this could be done via EXPOSE 8000 instruction in Dockerfile.
That port should be accessible from your host machine first, so, when starting your docker image as docker container, you should add -p parameter, such as
sudo docker run -d -it -p 8000:8000 --name docker_contaier_name docker_image_name
From now on, your docker application can be access within your host machine, let's say it is your physical computer.
Forward port from your router to your host machine
This time, you may want to do as what you did in your question.
Access your application from internet.
If I am thinking correctly, the ip address 10.0.0.140 is just your computer LAN IP address, it cannot accessible from internet.
You can only able to connect to your app via an internet IP, to do that, you can check your router to see what is your WAN IP address, which will be assigned to your router by your internet service provider. Or go google with "what is my IP"
What works for me, more or less, is setting up Apache2 as reverse proxy, redirecting a path in Apache2 to the port of the Docker container. This probably could also be done for example with NGINX.
This way the traffic from the net gets proxied to the container and back to the net, and I see the WordPress site. So regarding the question of OP, the docker container is now exposed to the internet.
However 1: This still doesn't explain why I don't get return traffic from the Docker container if I access it directly from the net.
However 2: Not all the url's in the WordPress site are correct, but that seems to be a WordPress issue and not a Docker / routing issue.

Flask in docker, access other flask server running locally

After finding a solution for this problem, I have another question: I am running a flask app in a docker container (my web map), and on this map I want to show tiles served by a (flask-based) Terracotta tile server running in another docker container. The two containers are on the same docker network and can talk to each other, however only the port where my web server is running is open to the public, and I like to keep it that way. Is there a way I can serve my tiles somehow "from local" without opening the port of the tile server? Maybe by setting up some redirects or something?
Main reason for this is that I need someone else to open ports for me, which takes ages.
If you are running your docker containers on a remote machine like ec2, then you need not worry about a port being open to public, as by default ports are closed in ec2 or similar services. You just need to open the port on which you are running your app, you can use aws console for that.
If you are running your docker container locally or on some server for which you don't have cosole access, then you can use somekind of firewall to open or close a port. I personally prefer UFW for Ubuntu systems. You can allow a certain range of ports using a simple command such as sudo ufw allow 9000 to allow incoming tcp packets on port 9000. Similarly you can deny incoming packets to a port. Also, you can open a port to a certain ip (like your own ip) using sudo ufw allow from <ip address>.

How to make a Docker container's service accessible via the container's IP address?

I'm a bit confused. Trying to run both a HTTP server listening on port 8080 and a SSH server listening on port 22 inside a Docker container I managed to accomplish the latter but strangely not the former.
Here is what I want to achieve and how I tried it:
I want to access services running inside a Docker container using the IP address assigned to the container:
ssh user#172.17.0.2
curl http://172.17.0.2:8080
Note: I know this is not how you would configure a real web server but I want the container to mimic an embedded device which runs both services and which I don't have available all the time. (So it's really just a local non-production thing with no security requirements).
I didn't expect integrating the SSH server to be easy, but to my surprise I just installed and started it and had to do nothing else to be able to connect to the machine via ssh (no EXPOSE 22 or --publish).
Now I wanted to access the container via HTTP on port 8080 and fiddled with --publish and EXPOSE but only managed to make the HTTP server available through localhost/127.0.0.1 on the host. So now I can access it via
curl http://127.0.0.1:8080/
but I want to access both services via the same IP address which is NOT localhost (e.g. the address the container got randomly assigned is totally OK for me).
Unfortunately
curl http://172.17.0.2:8080/
waits until it times out every time I tied it.
I tried docker run together with -p 8080, -p 127.0.0.1:8080:8080, -p 172.17.0.2:8080:8080 and much more combinations, together or without EXPOSE 8080 in the Dockerfile but without success.
Why can I access the container via port 22 without having exposed anything?
And how do I make it accessible via the container's IP address?
Update: looks like I'm experiencing exactly what's described here.

External access to home Rails server

My home server is running on Raspberry Pi with Rasbian (Debian for ARM)
There rails server is running: rvmsudo rails s -b ${LOCAL_IP} -p 80
It works perfectly fine to access from local machines, but it is not working when I am trying to access from outside of my local network
It should not be a router problem as I can easily access that server over ssh (both :22 and :80 are open in router preferences)
If you can access http via your local network ip, then it can be issue with router, look to port forwarding settings of DMZ.
BTW, router can use 80 port for its own purposes, like web interface. Try to bind to 8080 port for example.
As an alternative to share 80 port via internet you can use https://ngrok.com/
It allows you to share localhost http to anyone.

Resources