I trying to build a webapp with docker containers and I am getting connection refused when trying to run Nginx as a reverse proxy to my node app. I am not sure if it is a nginx server config issue or a docker-compose config issue.
[error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: foo.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:7770/", host: "foo.com"
I get this error when hitting foo.com, the weird thing is that my app works when the port number is referenced, so foo.com:7770 runs the app.
My nginx server config:
server {
listen 80;
server_name foo.com;
port_in_redirect off;
autoindex on;
location / {
proxy_pass http://127.0.0.1:7770;
proxy_redirect off;
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;
}
}
My Docker Compose File: (There might be some redundant things in here)
version: "2"
services:
nginx:
build: ./nginx
ports:
- "80:80"
depends_on:
- app
links:
- app
app:
build:
context: .
dockerfile: DockerFile
ports:
- "7770:7770"
links:
- mongo
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"
volumes_from:
- mongodata
depends_on:
- mongodata
mongodata:
image: tianon/true
volumes:
- /data/db
My Node Dockerfile:
FROM node:latest
ADD package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/
WORKDIR /opt/app
ADD . /opt/app
EXPOSE 7770
CMD ["npm", "start"]
My ngnix Dockerfile
FROM nginx:1.10
COPY default.conf /etc/nginx/conf.d/default.conf
On npm start this will run:
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(7770, function(err) {
if (err) {
console.log(err);
return;
}
console.log('Listening at http://localhost:7770');
});
This is my first run at docker, so I may have muddled up a few things. Also I am pointing foo.com to 127.0.0.1 in /private/etc/hosts.
c-holmes,
First of all, you need to remember each container has its own network stack, so you cannot use localhost inside the container to reach a service running in the docker host.
For this specific project, you will need to point the proxy_pass directive in your Nginx server config to a value what reach the app container. Something like:
proxy_pass http://app:7770;
You will need to do right that because in docker-compose context your container name will be mapped to an internal DNS entry. With that, you will not need to publish 7770 of the app container to the outside world and if your MongoBD will be accessed just by your app container, you will not need to publish the 27017 port either.
If you want to route traffic from nginx to app you have to use ip address or dns of the app container on proxy_pass. With Docker Compose services can discover each other with service names, so change in nginx conf
proxy_pass http://app:7770;
You don't need to publish port 7770 to outside world. Also for mongo you don't need to publish 27017 port.