I am following this tutorial to set up docker for my node.js rest api and there is this line in the tutorial:
docker run -p 49160:8080 -d <your username>/node-web-app
And this description:
The -p flag redirects a public port to a private port inside the
container. Run the image you previously built:
From the description, I know that port 49160 is a public port and 8080 is a private port. Since I am exposing port 5001 in my nodejs app, so I think I am running:
docker run -p 49160:5001 -d <your username>/node-web-app
But what exactly is a public port? Why is it "49160"?
It can be anything. Tutorial just used a random port. You can change it whatever you want. Then you can access your node-web-app running inside container at port 5001 at localhost:49160 from your host machine.
In your example port 8080 leads to some server (probably web server / Node) located inside of your Docker container. The outside (the host you're working on) port is 49160. The Docker setting named -p connects the inner port 8080 to the outer port 49160. If you now open the browser in your host system and hit the url http://localhost:49160 you will essentially access port 8080 inside the container.
Port 8080 is usually used for web servers. It is not obligatory though.
Port 49160 is just some port you or the auther of the tutorial decided to take as an example.
If you have a server inside the container listening on port 5001 it will not be accessible in your setup. If you want to make it accessible, you could adapt the following command:
docker run -p 49160:8080 -p 49159:5001 -d <your username>/node-web-app
Related
If docker container is set to network_mode: host, any port opened in the container would be opened on the docker host, without requiring the -p or -P docker run option.
How can I hide the port from public and make it accessible in localhost only.
If you're running with --net=host, you're using the host network stack and there are no other Docker controls over how it interacts with the network. If you want to only listen on a specific interface, your server would bind(2) to an address on it, just like any other process running directly on the host. Most higher-level server packages have an option to listen on some specific address, and you'd set the server to only listen on 127.0.0.1.
Using --net=host usually isn't a best practice. If you omit that option and use the standard Docker networking, then the docker run -p option has an option to bind to a specific host IP address. If your server listens on port 80 inside the container, and you actually want it to listen on port 8888 and only be accessible from the current host, you could
docker run -p 127.0.0.1:8888:80 ...
(In this latter case the process inside the container must listen on 0.0.0.0 or some other equivalent "all addresses" setting, even if you don't intend it to be reachable from off-host.)
You might need to look into iptables
This command below:
docker run -p 8000:8000 -t panels2
Which I am running should apparently allow me to run my application localhost on port 8000 locally.
In the docker terminal when running ‘docker ps’ I see:
0.0.0.0:8000->8000/tcp
Dockerfile look this:
EXPOSE 8000
ENTRYPOINT ["/usr/bin/python3.6", "/app/manage.py", "runserver"]
How to get access to localhost:8000?
The answer is set ip on 0.0.0.0 not 127.0.0.1(work inside container) because when we create a bridge then automatically ports are mapping on 0.0.0.0 ip even if we set 127.0.0.1 for application.
I tried this on multiple machines (Win10 and server 2016), same result
using this tutorial:
https://docs.docker.com/docker-for-windows/#set-up-tab-completion-in-powershell
This works
docker run -d -p 80:80 --name webserver nginx
Any other port, fails with
docker run -d -p 8099:8099 --name webserver nginx --> ERR_EMPTY_RESPONSE
Looks like Docker/nginx is listening failing on this port, but failing. Telneting to this port shows that the request goes through, but disconnects right away. This is different from when a port is not being listened to on at all.
There are two ports in that list. The first port is the one docker publishes on the host for you to connect to remotely. The second port is where to send that traffic in the container. Docker doesn't modify the application, so the application itself needs to be listening on that second port. By default, nginx listens on port 80. Therefore, you could run:
docker run -d -p 8099:80 --name webserver nginx
To publish on port 8099 and send that traffic to an app inside the container listening on port 80.
I launch docker container:
docker run --name node-arasaac -p 3000:3000 juanda/arasaac
And my node.js app works ok.
If I want to change host port:
docker run --name node-arasaac -p 8080:3000 juanda/arasaac
Web page is not loaded, logs from browser console:
Failed to load resource: net::ERR_CONNECTION_REFUSED
http://localhost:3000/app.318b21e9156114a4d93f.js Failed to load resource: net::ERR_CONNECTION_REFUSED
Do I need to have the same port both in host and container? It seems it knows how to resolve http://localhost:8080 so it loads my website, but internal links in the webpage go to port 3000 and it's not as good :-(
When you are running your node.js app in a docker container it will only expose the ports externally that you designate with your -p (lowercase) command. The first instance with, "-p 3000:3000", maps the host port 3000 to port 3000 being exposed from within your docker container. This provides a 1 to 1 mapping, so any client that is trying to connect to your node.js service can do so through the HOST port of 3000.
When you do "-p 8080:3000", docker maps the host port of 8080 to the node.js container port of 3000. This means any client making calls to your node.js app through the host (meaning not within the same container as your node.js app or not from a linked or networked docker container) will have to do so through the HOST port of 8080.
So if you have external services that expect to access your node.js at port 3000 they won't be able.
I have been trying to connect to a docker container via ip, but reamins unsuccessful. When I used "docker inspect container-id" I get this result.
My virtual box settings are by default:
Can someone help me resolving this issue?
When running docker, you can specify which port(s) you'd like to be accessible to the outside world. Basic syntax is:
docker run -p hostPort:containerPort imageName
or just
docker run -p hostPort imageName
In the first case, externally, hostPort will be used, but inside the container: containerPort will be used. In the second instance, you'd just be using that port both inside and outside your container.
You can also create an image with ports exposed by using the EXPOSE command in a Dockerfile.
You need to perform port forwarding or just simply expose port.
Port Forwarding:
docker run -p 2022:22 -p 2375:2375
Expose Port:
docker run -p 22 -p 2375