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.
Related
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
I have a Node app (Isomorphic React app) dockerized and deployed to AWS Elastic Beanstalk. I have all the information below but if you want a tldr: How do you configure port forwarding between a host and a container in AWS Elastic Beanstalk i.e. 5000:3000?
I want my application to work like this (the numbers are ports):
End User --80--> EC2 Instance / Nginx --5000--> Container --3000--> Application
I used the Dockerfile to EXPOSE 5000. I know that it's just a suggestion but as far as I know Amazon uses it to expose ports in the docker container instead of a docker-compose.yml. The app runs on port 3000. Code in the node to run on port 3000:
process.env.PORT || 3000
When trying to access the site using port 80, I'm getting a 502 Bad Gateway error.
I SSHed into the EC2 instance hosting my docker container. The nginx config for elasticbeanstalk looks like this:
upstream docker {
server [CONTAINER_IP]:5000;
keepalive 256;
}
The IP is the correct IP of the container (I checked by using Docker Inspect). When I run: sudo docker ps -a
I get:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
REDACTED REDACTED "node server.js" 34 hours ago Up 34 hours 5000/tcp jolly_williams
If I run netstat on the EC2 host instance I see that port 80 is open:
tcp 0 0 0.0.0.0:80
When I look at the logs on the EC2 host instance I see this error multiple times for different resources:
request: "GET / HTTP/1.1", upstream: "http://[CONTAINER_IP]:5000/", host: [Beanstalk URL] connection refused
Now here is the kicker, it works when I run:
sudo curl [ContainerIP]:3000 (from the EC2 host intance)
-or-
sudo docker exec -ti [CONTAINER_NAME] curl http://localhost:3000
Anyone know why the node is running off 3000 and not 5000? What can I do?
Talked to AWS Support. They're saying that the docker port has to be the same port the application is listening on i.e. no port mapping
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 have a MariaDB up and running in a Docker container. I want to know how to connect to it from an app running locally (not) in the docker container. How can I open up access?
your MariaDB container must publish ports, and you will connect using those ports. See for example http://amattn.com/p/installing_maria_db_mysql_with_docker.html
the port 3306 in the container will be mapped to a port on the host, and you will connect to that port.
when you call docker run to start your container you can bind a specific port like this
docker run -p your_port:3306
this will make your container accessible on docker_host_ip:your_port and the docker service will take care of forwarding the connection to the right container at the port 3306
I've two docker containers with different images. This is the partial output of "docker ps" command:
$user: docker ps
CONTAINER ID IMAGE PORTS
9c8ff81215d4 node:slim 0.0.0.0:5858->5858/tcp, 0.0.0.0:10101->10101/tcp
d85a0de91432 node-debug 0.0.0.0:8080->8080/tcp
The first container is running a server app with debug option:
$user: node --debug server.js
Debugger listening on port 5858
...
and listens on port 5858 with debugger and on port 10101 with server.js app.
The second container is running node-inspector
$user: node-inspector
Node Inspector v0.12.6
Visit http://127.0.0.1:8080/?port=5858
that connects by default on port 5858 to debugger and listens on port 8080 for web-inspector in Chrome.
The issue is when I visit http://127.0.0.1:8080/?port=5858 I see the inspector without loaded sources.
In the Chrome console is see this error:
Request with id = 10 failed. "ErrorNotConnected: Error: connect ECONNREFUSED 127.0.0.1:5858. Is node running with --debug port 5858?"
The problem here is that node inspector is trying to connect to localhost/127.0.0.1, i.e. local to that container, not local to your host. When you run in bridge networking (default), each container is on its own IP.
You could quickly resolve this with either of these options:
Use host networking for both containers
In this case the port forwarding you configured is not necessary any longer
Use host networking just for the node inspector container
In this case you still need port 5858 mapped to host but no longer the port 8080 on node inspector