Node.js server only listening on ipv6 - node.js

I am running a node.js server on port 5403. I can telent to the private ip on this port but cannot telnet to the public ip on the same port.
I assume the cause of this is because node.js is only listening on ipv6. This is the result of
netstat -tpln
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
-
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
-
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
-
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
-
tcp6 0 0 :::5611 :::* LISTEN
25715/node
tcp6 0 0 :::22 :::* LISTEN
-
tcp6 0 0 ::1:631 :::* LISTEN
-
tcp6 0 0 :::5403 :::* LISTEN
25709/node
How do I make the node server listen on ipv4

You need to specify an IPV4 address when you call the listen(), I had the same issue with the http module. If I use this:
var http = require('http');
var server = http.createServer(function(request, response) {
...
});
server.listen(13882, function() { });
It only listen on IPV6, as you can see from netstat output:
$ netstat -lntp
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp6 0 0 :::13882 :::* LISTEN
However, if I specify an IPV4 address like this:
var http = require('http');
var server = http.createServer(function(request, response) {
...
});
server.listen(13882, "0.0.0.0", function() { });
netstat will report the server as listening on IPV4:
$ netstat -lntp
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0 0.0.0.0:13882 0 0.0.0.0:13882 LISTEN
I'm using Ubuntu 16.04 and npm 5.3.0.
HTH

Related

bind(): "Cannot assign request address"

I am aware of bind: cannot assign requested address and Cannot assign requested address - possible causes?, I have not been able to derive a solution from these.
I am trying to create a TCP stream (specifically here a std::net::TcpListener) directly with libc. I am encountering the error Cannot assign requested address and error code: 99 when running my code.
The exact output being:
error message: error code: : Cannot assign requested address
thread 'main' panicked at 'error code: 99', src/main.rs:43:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
My code (playground):
use libc; // 0.2.136
use std::mem::{size_of, transmute};
fn main() {
// Create socket
let socket_fd = unsafe {
let socket = libc::socket(libc::AF_INET6, libc::SOCK_STREAM | libc::SOCK_NONBLOCK, 0);
assert_ne!(socket, -1);
let optval = 1i32;
let res = libc::setsockopt(
socket,
libc::SOL_SOCKET,
libc::SO_REUSEPORT,
(&optval as *const i32).cast(),
4,
);
assert_eq!(res, 0);
socket
};
// Bind socket
// decimal 127.0.0.1 -> hexadecimal 007f.0000.0000.0001
let sin6_addr = unsafe { transmute::<_, libc::in6_addr>(*b"007f000000000001") };
let socket_address = libc::sockaddr_in6 {
sin6_family: libc::AF_INET6 as u16,
sin6_port: 8080,
sin6_flowinfo: u32::default(),
sin6_addr,
sin6_scope_id: u32::default(),
};
let socket_address_length = size_of::<libc::sockaddr_in6>() as u32;
unsafe {
let res = libc::bind(
socket_fd,
(&socket_address as *const libc::sockaddr_in6).cast(),
socket_address_length,
);
if res == -1 {
let err = errno();
print_error("error message: ");
panic!("error code: {err}");
}
}
assert_eq!(unsafe { libc::close(socket_fd) },0);
}
fn print_error(s: &str) {
unsafe {
libc::perror(s.as_ptr().cast());
}
}
fn errno() -> i32 {
unsafe { *libc::__errno_location() }
}
I set the option SO_REUSEPORT here as I need to create a stream that multiple processes can listen to.
https://lwn.net/Articles/542629/:
The basic concept of SO_REUSEPORT is simple enough. Multiple servers (processes or threads) can bind to the same port
The port does not appear to be in use:
jonathan#jonathan-pc:~$ sudo lsof -i -P -n
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd-r 793 systemd-resolve 13u IPv4 19452 0t0 UDP 127.0.0.53:53
systemd-r 793 systemd-resolve 14u IPv4 19453 0t0 TCP 127.0.0.53:53 (LISTEN)
avahi-dae 855 avahi 12u IPv4 31377 0t0 UDP *:5353
avahi-dae 855 avahi 13u IPv6 31378 0t0 UDP *:5353
avahi-dae 855 avahi 14u IPv4 31379 0t0 UDP *:49594
avahi-dae 855 avahi 15u IPv6 31380 0t0 UDP *:58397
NetworkMa 859 root 36u IPv4 36212 0t0 UDP 192.168.0.22:68->192.168.0.1:67
NetworkMa 859 root 37u IPv4 110801 0t0 UDP 192.168.0.17:68->192.168.0.1:67
tor 1038 debian-tor 6u IPv4 31407 0t0 TCP 127.0.0.1:9050 (LISTEN)
rust-anal 4117 jonathan 46u IPv4 33176 0t0 UDP 127.0.0.1:35972->127.0.0.53:53
rust-anal 4189 jonathan 46u IPv4 33176 0t0 UDP 127.0.0.1:35972->127.0.0.53:53
firefox 4297 jonathan 3u IPv4 112786 0t0 TCP 192.168.0.22:46702->151.101.1.69:443 (ESTABLISHED)
firefox 4297 jonathan 29u IPv4 100032 0t0 TCP 192.168.0.22:55828->34.120.208.123:443 (ESTABLISHED)
firefox 4297 jonathan 56u IPv4 115182 0t0 TCP 192.168.0.22:50798->52.51.190.37:443 (ESTABLISHED)
firefox 4297 jonathan 75u IPv4 95741 0t0 TCP 192.168.0.22:48466->142.250.200.10:443 (ESTABLISHED)
firefox 4297 jonathan 81u IPv4 67879 0t0 TCP 192.168.0.22:55638->34.214.17.205:443 (ESTABLISHED)
firefox 4297 jonathan 116u IPv4 111739 0t0 TCP 192.168.0.22:37986->172.217.169.68:443 (ESTABLISHED)
firefox 4297 jonathan 121u IPv4 95751 0t0 TCP 192.168.0.22:46054->198.252.206.25:443 (ESTABLISHED)
firefox 4297 jonathan 129u IPv4 102073 0t0 TCP 192.168.0.22:51478->34.120.237.76:443 (ESTABLISHED)
firefox 4297 jonathan 136u IPv4 96742 0t0 TCP 192.168.0.22:36606->34.120.115.102:443 (ESTABLISHED)
firefox 4297 jonathan 139u IPv4 97943 0t0 TCP 192.168.0.22:36626->172.217.169.68:443 (ESTABLISHED)
firefox 4297 jonathan 144u IPv4 99520 0t0 TCP 192.168.0.22:49264->198.252.206.25:443 (ESTABLISHED)
firefox 4297 jonathan 157u IPv4 104859 0t0 TCP 192.168.0.22:58042->93.184.220.29:80 (ESTABLISHED)
jonathan#jonathan-pc:~$
You are transmuting b"007f000000000001" into a libc::in6_addr. But I don't thing that is the proper thing to do.
Looking at the man page, that struct is:
struct in6_addr {
uint8_t s6_addr[16];
};
That is, just 16 bytes, the proper thing for an IPv6. But your bytes are actually the ASCII values of that string: 30303766..., that while technically an IPv6 address, it is not a local one of yours so you cannot bind to it.
Moreover, in IPv6 the proper localhost address is ::1, not 127.0.0.1. That is 15 zeros followed by a single one.
If you want to bind to the IPv6 localhost by using a transmute that would be:
let sin6_addr = unsafe { transmute::<_, libc::in6_addr>([0_u8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]) };
Or if you insist on using a literal string (why?):
let sin6_addr = unsafe { transmute::<_, libc::in6_addr>(*b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01") };

How do I run a next application on port 80?

I'm trying to run a nextjs app on port 80 on arch linux.
I'm getting a permission error even though I'm using sudo.
It seems like no other app is using port 80?
This is package.json:
"scripts": {
"dev": "next dev -p 80",
"build": "next build",
"start": "next start -p 80",
"lint": "next lint"
},
Now when i run sudo npm run dev
I get this error message:
Error: listen EACCES: permission denied 0.0.0.0:80
at Server.setupListenHandle [as _listen2] (node:net:1313:21)
at listenInCluster (node:net:1378:12)
at Server.listen (node:net:1465:7)
at /home/pera/Desktop/projects/agency/frontend/node_modules/next/dist/server/lib/start-server.js:45:16
at new Promise (<anonymous>)
at Object.startServer (/home/pera/Desktop/projects/agency/frontend/node_modules/next/dist/server/lib/start-server.js:19:12)
at nextDev (/home/pera/Desktop/projects/agency/frontend/node_modules/next/dist/cli/next-dev.js:116:23)
at /home/pera/Desktop/projects/agency/frontend/node_modules/next/dist/bin/next:130:34 {
code: 'EACCES',
errno: -13,
syscall: 'listen',
address: '0.0.0.0',
port: 80
}
Now when i run sudo netstat -tulpn
tcp 0 0 127.0.0.1:6463 0.0.0.0:* LISTEN 17894/Discord --typ
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 585/mongod
tcp 0 0 127.0.0.1:27060 0.0.0.0:* LISTEN 1350/steam
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 317/systemd-resolve
tcp 0 0 0.0.0.0:27036 0.0.0.0:* LISTEN 1350/steam
tcp 0 0 127.0.0.1:57343 0.0.0.0:* LISTEN 1350/steam
tcp 0 0 127.0.0.1:9050 0.0.0.0:* LISTEN 438/tor
tcp 0 0 0.0.0.0:5355 0.0.0.0:* LISTEN 317/systemd-resolve
tcp 0 0 127.0.0.54:53 0.0.0.0:* LISTEN 317/systemd-resolve
tcp6 0 0 :::5355 :::* LISTEN 317/systemd-resolve
udp 0 0 0.0.0.0:35486 0.0.0.0:* 17894/Discord --typ
udp 0 0 0.0.0.0:60196 0.0.0.0:* 17894/Discord --typ
udp 0 0 0.0.0.0:44070 0.0.0.0:* 17894/Discord --typ
udp 0 0 127.0.0.54:53 0.0.0.0:* 317/systemd-resolve
udp 0 0 127.0.0.53:53 0.0.0.0:* 317/systemd-resolve
udp 0 0 192.168.0.10:68 0.0.0.0:* 277/systemd-network
udp 0 0 0.0.0.0:5355 0.0.0.0:* 317/systemd-resolve
udp 0 0 0.0.0.0:27036 0.0.0.0:* 1350/steam
udp6 0 0 :::5355 :::* 317/systemd-resolve
This is the output. No app is open on port 80?

Google cloud engine external access issue

I'm new to Google cloud platform and I didn't understand why I cannot reach a node.js instance running on a new VM.
Node is running on port 8084 through app.listen('8084', "0.0.0.0")
Firewall rules are the following:
gcloud compute firewall-rules list
NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED
default-allow-http default INGRESS 1000 tcp:80 False
default-allow-https default INGRESS 1000 tcp:443 False
default-allow-icmp default INGRESS 65534 icmp False
default-allow-internal default INGRESS 65534 tcp:0-65535,udp:0-65535,icmp False
default-allow-rdp default INGRESS 65534 tcp:3389 False
default-allow-ssh default INGRESS 65534 tcp:22 False
node-8084 default INGRESS 999 tcp:8084 False
netstat:
netstat -na | grep LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8084 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:34339 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:8998 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:65001 0.0.0.0:* LISTEN
tcp6 0 0 :::970 :::* LISTEN
tcp6 0 0 :::980 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::922 :::* LISTEN
I can reach the VM via SSH (port 22) but not through http://35.206.91.238:8084/medical on Chrome. 35.206.91.238 is the external IP showed by google cloud console.
Node.js presents no errors (and no requests).
main code is
var app = express();
app.get('/medical', function(request, response){
if( request.query.q )
run(request.query.q, function(results, queries) { parseResult(q, results, queries, response) } );
})
app.listen('8084', '0.0.0.0')
console.log('Server started on port 8084');
exports = module.exports = app;
Wireshark traffic is only
Only SYN is passing, chrome says "35.206.91.238 refused to connect."
Node is executed manually from shell and doesn't report any error after "Server started on port 8084".
Any idea

Nginx docker as nodejs proxy in local

At the moment I am using nginx in docker to proxy my node applications
for example I add website.dev in my host file
and my actual nginx config is
worker_processes 4;
pid /run/nginx.pid;
events {}
http {
server {
listen 80;
server_name website.dev;
location / {
proxy_pass http://localnode:3000;
proxy_set_header X-Base-Path "/";
proxy_set_header Website-Name "test";
}
}
}
And I a starting my docker container with this command
docker run --name infra-nginx --add-host localnode:$(ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}') -p 80:80 -d docker-registry.host.com:5000/infra-nginx:dev
Where
$(ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}') is getting my local ip, not working all the time.
The problem is if I want to work without internet, I can't.
And when my ip change, I have to restart the container with the new ip.
I tried with this config instead
worker_processes 4;
pid /run/nginx.pid;
events {}
http {
server {
listen 80;
server_name localhost;
location / {
return 200 'gangnam style!';
}
}
server {
listen 80;
server_name website.dev;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Base-Path "/";
proxy_set_header Website-Name "test";
}
}
}
And running
docker run --name infra-nginx --network host -d docker-registry.host.com:5000/infra-nginx:dev
In this case, when I am running:
curl http://website.dev/
I have
curl: (7) Failed to connect to website.dev port 80: Connection refused
The docker ps is giving
81da561dd131 ajouve/infra-nginx:dev "nginx -g 'daemon ..." 32 minutes ago Up 32 minutes infra-nginx
the netstat -plant is giving me
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
tcp 0 0 172.17.0.1:35962 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35938 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35994 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:45080 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:35990 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 192.168.65.2:44900 151.101.0.204:80 TIME_WAIT -
tcp 0 0 172.17.0.1:45126 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:36000 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35958 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:59172 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:35976 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:59106 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:35980 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35996 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58356 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35966 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:45112 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:35932 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58366 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35998 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 192.168.65.2:41386 206.251.255.63:80 TIME_WAIT -
tcp 0 0 172.17.0.1:58358 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35956 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35924 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:36004 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58360 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35964 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35916 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58362 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:59148 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:59166 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:35944 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35912 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35954 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:45116 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:58354 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35988 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:59122 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 192.168.65.2:34936 5.153.231.4:80 TIME_WAIT -
tcp 0 0 192.168.65.2:44904 151.101.0.204:80 TIME_WAIT -
tcp 0 0 172.17.0.1:59162 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:59180 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:45130 172.17.0.5:6379 ESTABLISHED -
tcp 0 0 172.17.0.1:59140 172.17.0.3:5672 ESTABLISHED -
tcp 0 0 172.17.0.1:36002 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35922 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:35970 172.17.0.4:27017 ESTABLISHED -
tcp 0 0 172.17.0.1:58364 172.17.0.4:27017 ESTABLISHED -
tcp6 0 0 :::6379 :::* LISTEN -
tcp6 0 0 :::15672 :::* LISTEN -
tcp6 0 0 :::5672 :::* LISTEN -
tcp6 0 0 :::27017 :::* LISTEN -
From the docker container:
curl -v localhost
Is giving
* Rebuilt URL to: localhost/
* Hostname was NOT found in DNS cache
* Trying ::1...
* connect to ::1 port 80 failed: Connection refused
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.10.3 is not blacklisted
< Server: nginx/1.10.3
< Date: Fri, 29 Sep 2017 08:46:10 GMT
< Content-Type: text/plain
< Content-Length: 14
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
gangnam style!
But curl -v website.dev is returning
* Rebuilt URL to: website.dev/
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to website.dev (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: website.dev
> Accept: */*
>
< HTTP/1.1 502 Bad Gateway
* Server nginx/1.10.3 is not blacklisted
< Server: nginx/1.10.3
< Date: Fri, 29 Sep 2017 08:46:37 GMT
< Content-Type: text/html
< Content-Length: 173
< Connection: keep-alive
<
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
* Connection #0 to host website.dev left intact
And docker inspect infra-nginx
[
{
"Id": "16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e",
"Created": "2017-09-29T08:31:21.144827953Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2656,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-09-29T08:31:21.548119911Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:25d085baee52923e32f8d134048238fb67e71173e01f758c391119235f7fc565",
"ResolvConfPath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/hostname",
"HostsPath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/hosts",
"LogPath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e-json.log",
"Name": "/infra-nginx",
"RestartCount": 0,
"Driver": "aufs",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "host",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": null,
"Name": "aufs"
},
"Mounts": [],
"Config": {
"Hostname": "moby",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"443/tcp": {},
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.10.3-1~jessie"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "ajouve/infra-nginx:dev",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "175272649c9a9c5abbfde7516328bdab5cb3825e1e027eee0580eb18f7ff77cb",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "3e04a4c12b5a5b3f55e7b4e918dadec64806b5c926fc249e8aa3e28398a02954",
"EndpointID": "7de54daaa31230c9492a463792015af727e9562eaacbaa0c2d70cdc3d3b04236",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
So when you run it using --net host it actually still is not on mac network as such. That is why it is not working.
From the documentation
The Mac has a changing IP address (or none if you have no network access). From 17.06 onwards our recommendation is to connect to the special Mac-only DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host.
So you need to change your config to
worker_processes 4;
pid /run/nginx.pid;
events {}
http {
server {
listen 80;
server_name localhost;
location / {
return 200 'gangnam style!';
}
}
server {
listen 80;
server_name website.dev;
location / {
proxy_pass http://docker.for.mac.localhost:3000;
proxy_set_header X-Base-Path "/";
proxy_set_header Website-Name "test";
}
}
}
And you should run the container as below
docker run --name infra-nginx -p 80:80 -d docker-registry.host.com:5000/infra-nginx:dev
You're making it way too complex. Just EXPOSE a port like 8080 from your app container.
In nginx use:
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.2:8080;
}
Ok I think the solution I did is simple enough.
First I have an app in nodejs running on the port 3000 on my local machine, so if I go to localhost:3000 in the browser I can see the app running.
Then I create a file called default.conf inside a folder in my machine ~/projects/docker/default.conf, you can create that file wherever you want. And paste this code inside the file:
server {
listen 80;
server_name myapp.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Look the line proxy_pass http://localhost:3000; I am redirecting to my app running on my local machine.
Then I run a nginx container with the following command:
sudo docker run -d -p 80:80 --name="nginx" --net="host" -v ~/projects/docker/default.conf:/etc/nginx/conf.d/default.conf:ro nginx
The -p 80:80 is to bind that port to the nginx container
The --net="host" is to tell the container that the network it will use is the same that the host, it means the same as my machine, with this I can forward to the localhost:3000 from inside the container.
The -v ~/projects/docker/default.conf:/etc/nginx/conf.d/default.conf:ro
is to tell to the nginx that its configuration file will be the file I have in my local machine.
It means something like: You will find this /etc/nginx/conf.d/default.conf file in this ~/projects/docker/default.conf location
And that's all.
If I go to my browser and type just localhost (without the port) it will go through the nginx container and will be redirected to the app running in the localhost:3000 in my local machine
Let me know if this helps you

Can't connect to MongoDB from NodeJS on Ubuntu. Same code on OSX works fine

I have mongodb installed locally on my osx laptop and on a remote ubuntu server. Both have mongodb running and I can verify this using the http diagnostics on port 28017. I'm running the same code on both computers. On osx everything works fine, but on Ubuntu I can't make a connection to the database through NodeJS. I keep getting this error:
Error: failed to connect to [localhost:27017]]
message: listen EADDRNOTAVAIL
stack: Error: listen EADDRNOTAVAIL
at errnoException (net.js:769:11)
at Server._listen2 (net.js:892:19)
at listen (net.js:936:10)
at Server.listen (net.js:993:9)
at asyncCallback (dns.js:67:16)
at Object.onanswer [as oncomplete] (dns.js:120:9)
What I don't understand is that I can connect on Ubuntu locally via the mongo commandline interface. I can also connect to the database on Ubuntu via the mongo command on my OSX computer. So nothing seems to be wrong with the installation of MongoDB itself.
Can anyone think of a reason why I can't connect via NodeJS? I have tried using the mongodb and mongoose packages. Both give me the same error.
Here are the 2 ways I tried:
var mongo = require("mongodb");
var host = "localhost";
var port = mongo.Connection.DEFAULT_PORT;
var db = new mongo.Db('node-mongo-examples', new mongo.Server(host, port, {}), {});
db.open(function(err, db){
if(err){
log.error('MongoDB connection error:', err);
}else{
log.info("OPEN MONGO CONNECTION");
}
});
And the with mongoose:
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'node-mongo-examples');
db.on('error', function(err){
log.error('MongoDB connection error:', err);
});
db.once('open', function () {
log.debug("OPEN MONGO CONNECTION");
});
In the logs I see nothing special, and nothing happens either
***** SERVER RESTARTED *****
Wed Sep 26 18:00:18 [initandlisten] MongoDB starting : pid=13377 port=27017 dbpath=/var/lib/mongodb 64-bit host=octo-dev
Wed Sep 26 18:00:18 [initandlisten] db version v2.2.0, pdfile version 4.5
Wed Sep 26 18:00:18 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Wed Sep 26 18:00:18 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Wed Sep 26 18:00:18 [initandlisten] options: { config: "/etc/mongodb.conf", dbpath: "/var/lib/mongodb", logappend: "true", logpath: "/var/log/mongodb/mongodb.log" }
Wed Sep 26 18:00:18 [initandlisten] journal dir=/var/lib/mongodb/journal
Wed Sep 26 18:00:18 [initandlisten] recover : no journal files present, no recovery needed
Wed Sep 26 18:00:18 [websvr] admin web console waiting for connections on port 28017
Wed Sep 26 18:00:18 [initandlisten] waiting for connections on port 27017
..... except when I connect through the mongo commandline interface:
Wed Sep 26 18:30:40 [initandlisten] connection accepted from 127.0.0.1:38229 #3 (1 connection now open)
I ran out of things to try. Any suggestions for troubleshooting this?
Some extra info
sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 13377/mongod
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 885/mysqld
tcp 0 0 0.0.0.0:1935 0.0.0.0:* LISTEN 1102/java
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 1102/java
tcp 0 0 192.87.219.76:10000 0.0.0.0:* LISTEN 31171/webserver
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1387/java
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1076/apache2
tcp 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN 13377/mongod
tcp 0 0 0.0.0.0:48466 0.0.0.0:* LISTEN 12418/java
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3507/sshd
tcp 0 0 127.0.0.1:9016 0.0.0.0:* LISTEN 12418/java
tcp 0 0 0.0.0.0:5080 0.0.0.0:* LISTEN 1102/java
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 1216/master
tcp 0 0 0.0.0.0:41018 0.0.0.0:* LISTEN 12418/java
tcp 0 0 0.0.0.0:8443 0.0.0.0:* LISTEN 1102/java
tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 12418/java
tcp 0 0 0.0.0.0:29090 0.0.0.0:* LISTEN 12418/java
tcp 0 0 127.0.0.1:8100 0.0.0.0:* LISTEN 8535/soffice.bin
tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN 1387/java
tcp 0 0 0.0.0.0:389 0.0.0.0:* LISTEN 887/slapd
tcp 0 0 0.0.0.0:33736 0.0.0.0:* LISTEN 1102/java
tcp6 0 0 :::22 :::* LISTEN 3507/sshd
tcp6 0 0 :::389 :::* LISTEN 887/slapd
udp 0 0 192.87.219.76:123 0.0.0.0:* 721/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 721/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 721/ntpd
udp 0 0 0.0.0.0:5353 0.0.0.0:* 797/avahi-daemon: r
udp 0 0 0.0.0.0:55248 0.0.0.0:* 797/avahi-daemon: r
udp6 0 0 :::123 :::* 721/ntpd
udp6 0 0 :::35920 :::* 797/avahi-daemon: r
udp6 0 0 :::5353 :::* 797/avahi-daemon: r
Make sure port 27017 is opened to the web server in Ubuntu.
I found the problem!! The system had a messed up /etc/hosts file. Something with localhost configuration that was unusual. Correcting this file solved everything :D
Try firing the mongo command from your terminal.It will show an error if theres some problem with mongodb.

Resources