proxy'ing between docker containers - ruby-on-rails

I have a docker setup with the following
rails api backend
mysql db
redis db
node/react frontend (webpack)
nginx serving the frontend
(the rails backend is currently being served through the builtin puma server - I think I will move it to the same nginx server runing the node app)
My problem is that the frontend will request stuff on the backend, but this does not work.
I have set up a proxy on nginx as follows:
#nginx.conf
server {
listen 8080;
# Always serve index.html for any request
location / {
# Set path
root /wwwroot/;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:3000;
}
}
But when I when I initiate an api call I get the following in the nginx log:
nginx-server | 2017/05/13 20:56:08 [error] 5#5: *19 connect() failed (111: Connection refused) while connecting to upstream, client: 172.21.0.1, server: , request: "POST /api/authenticate HTTP/1.1", upstream: "http://127.0.0.1:3000/api/authenticate", host: "localhost:8080", referrer: "http://localhost:8080/"
nginx-server | 172.21.0.1 - - [13/May/2017:20:56:08 +0000] "POST /api/authenticate HTTP/1.1" 502 575 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36" "-"
And I do not see any thing hitting the puma server.
I am not sure where I should be looking. Is this a problem with my docker-compose file or is it a nginx issue (or both).
I have included my docker-compose.yml below:
version: '2'
services:
nginx:
build:
context: .
dockerfile: docker.nginx
image: pt-nginx
container_name: nginx-server
ports:
- "8080:8080"
volumes:
- wwwroot:/wwwroot
webpack:
build:
context: ./frontend
dockerfile: docker.webpack
image: pt-webpack
container_name: react-frontend
ports:
- "35729:35729"
volumes:
- ./frontend:/app
- /app/node_modules
- wwwroot:/wwwroot
db:
build:
context: ./backend
dockerfile: docker.mysql
image: pt-mysql
container_name: mysql-server
env_file: ./backend/.env
ports:
- "3306:3306"
volumes:
- ./sql/data/:/var/lib/mysql
redis:
build:
context: ./backend
dockerfile: docker.redis
image: pt-redis
container_name: redis-server
app:
build:
context: ./backend
dockerfile: docker.rails
image: pt-rails
container_name: rails-server
command: >
sh -c '
rake resque:start_workers;
bundle exec rails s -p 3000 -b 0.0.0.0;
'
env_file: ./backend/.env
volumes:
- ./backend:/usr/src/app
- /Users/mh/Pictures/ROR_PT/phototank:/Users/martinhinge/Pictures/ROR_PT/phototank
ports:
- "3000:3000"
expose:
- "3000"
depends_on:
- db
- redis
volumes:
wwwroot:
driver: local

The problem is that your nginx service is requesting upstream of localhost (127.0.0.1). But the application is in another container (with another IP). You can reference the rails application by DNS at appsince they are both in a default network. The upstream in nginx configuration would look something like proxy_pass http://app:3000; instead.
Read more about the networking at https://docs.docker.com/compose/networking/, specifically:
Within the web container, your connection string to db would look like postgres://db:5432, and from the host machine, the connection string would look like postgres://{DOCKER_IP}:8001.

Related

How to connect two docker containers together

I have a reactjs front end application and a simple python flask. And I am using a docker-compose.yml to spin up both the containers, and it is like this:
version: "3.2"
services:
frontend:
build: .
environment:
CHOKIDAR_USEPOLLING: "true"
ports:
- 80:80
links:
- "backend:backend"
depends_on:
- backend
backend:
build: ./api
# volumes:
# - ./api:/usr/src/app
environment:
# CHOKIDAR_USEPOLLING: "true"
FLASK_APP: /usr/src/app/server.py
FLASK_DEBUG: 1
ports:
- 8083:8083
I have used links so the frontend service can talk to backend service using axios as below:
axio.get("http://backend:8083/monitors").then(res => {
this.setState({
status: res.data
});
});
I used docker-compose up --build -d to build and start the two containers and they are started without any issue and running fine.
But now the frontend cannot talk to backend.
I am using an AWS ec2 instance. When the page loads, I tried to see the for any console errors and I get this error:
VM167:1 GET http://backend:8083/monitors net::ERR_NAME_NOT_RESOLVED
Can someone please help me?
The backend service is up and running.
You can use a nginx as reverse proxy for both
The compose file
version: "3.2"
services:
frontend:
build: .
environment:
CHOKIDAR_USEPOLLING: "true"
depends_on:
- backend
backend:
build: ./api
# volumes:
# - ./api:/usr/src/app
environment:
# CHOKIDAR_USEPOLLING: "true"
FLASK_APP: /usr/src/app/server.py
FLASK_DEBUG: 1
proxy:
image: nginx
volumes:
- ./nginx.conf:/etc/nginx/conf.d/example.conf
ports:
- 80:80
minimal nginx config (nginx.conf):
server {
server_name example.com;
server_tokens off;
location / {
proxy_pass http://frontend:80;
}
}
server {
server_name api.example.com;
server_tokens off;
location / {
proxy_pass http://backend:8083;
}
}
The request hits the nginx container and is routed according the domain to the right container.
To use example.com and api.example.com you need to edit your hosts file:
Linux: /etc/hosts
Windows: c:\windows\system32\drivers\etc\hosts
Mac: /private/etc/hosts
127.0.0.1 example.com api.example.com

connection refused while connecting to upstream Nginx Docker

I'm trying to build a FastAPI app in docker, I find the site comes to a '502-Bad Gate Way' error and nginx log shows:
nginx_1 | 2020/12/21 18:23:13 [error] 29#29: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.27.0.1, server: , request: "GET /user HTTP/1.1", upstream: "http://172.27.0.3:8000/user", host: "localhost:8080"
Here is my docker-compose.yaml file:
version: '3.7'
services:
fastapi:
build: ./api
command: uvicorn main:app --reload --host 0.0.0.0 --port 8000
volumes:
- ./fastapi/:/fastapi/
ports:
- 8000:8000
depends_on:
- db
db:
image: mongo:latest
container_name: mongo4
ports:
- "27017:27017"
volumes:
- "./data/mongo/data:/data/db"
nginx:
image: nginx:latest
ports:
- "8080:8080"
volumes:
- ./nginx_config.conf:/etc/nginx/conf.d/default.conf
depends_on:
- fastapi
Nginx config file:
server {
listen 8080;
location /user {
proxy_pass http://fastapi:8000/user;
}
}
Thanks for help.

failed connect (nginx with docker)

I'm getting this failed to connect issues when it comes to communicating with other docker containers e.g Java server end.
This is the issue I'm getting
nginx_1 | 2019/12/14 17:39:47 [error] 6#6: *2 connect() failed (111:
Connection refused) while connecting to upstream, client: 172.19.0.1,
server: , request: "GET /api/cards HTTP/1.1", upstream:
"http://172.19.0.4:80/api/cards", host: "localhost", referrer:
"http://localhost/"
Here is my Docker-Compose configuration (docker-compose.development.yml)
version: '3'
services:
auth:
build:
context: ./auth
dockerfile: dev.Dockerfile
volumes:
- ./auth:/var/app
- ~/.gradle/:/root/.gradle/
client:
build:
context: ./client
dockerfile: dev.Dockerfile
volumes:
- ./client:/var/app/
- /var/app/node_modules
server:
build:
context: ./server
dockerfile: dev.Dockerfile
volumes:
- ./server:/var/app/
- ~/.gradle/:/root/.gradle/
nginx:
image: nginx
volumes:
- ./server.dev.conf:/etc/nginx/conf.d/default.conf
ports:
- "80:80"
depends_on:
- auth
- client
- server
Here is my nginx docker configuration (server.dev.conf)
server {
listen 80;
location /login/ {
proxy_pass http://auth:80/login/;
}
location /api/ {
proxy_pass http://server:80/api/;
}
location /resource/ {
proxy_pass http://server:80/resource/;
}
location / {
proxy_pass http://client:80;
rewrite ^/home/.*$ / last;
}
}
Remind you this is for our development hosted locally. The production version uses SSL

Problem with nginx reverse proxy and docker. Only one proxy_path is working

I am completly lost.. So i have two proxy_paths in my nginx conf: '/' and '/api'. '/' redirects to my frontend and is working perfectly but the '/api' proxy path is not working at all. The proxy server is logging requests to '/api/' but not forwarding them to my actual api-server. I'm missing something. Is the '/' proxy_path some sort of catch all that overrides any other proxy paths? Any assistance would be invaluable! Thanks! Here are my configs:
nginx reverse proxy conf:
server {
listen 80;
server_name proxy;
location / {
proxy_pass http://frontend_prod:3000/;
}
location /api {
proxy_pass http://api_prod:3333/;
}
}
docker-compose:
version: '3.1'
services:
proxy:
build: ./proxy/
ports:
- '9000:80'
restart: always
depends_on:
- frontend_prod
- api_prod
frontend_prod:
build: ./frontend/nginx/
ports:
- '3000'
depends_on:
- api_prod
restart: always
db:
image: mariadb
restart: always
environment:
MYSQL_ROOT_PASSWORD: hunter2
api_prod:
build: ./backend/api/
command: npm run production
ports:
- '3333'
depends_on:
- db

Error 502 accessing nextcloud via docker with nginx

Heyo!
Update: I figured it out and added my answer.
I'm currently in the process of learning docker and I've written a docker-compose file that should launch nginx, gitea, nextcloud and route them all via domain name as a reverse proxy.
All is going well except for with nextcloud. I can access it via localhost:3001 but not via the nginx reverse proxy. All is well with gitea, it works both ways.
The error I'm getting is:
nginx_proxy | 2018/08/10 00:17:34 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: cloud.example.ca, request: "GET / HTTP/1.1", upstream: "http://172.19.0.4:3001/", host: "cloud.example.ca"
docker-compose.yml:
version: '3.1'
services:
nginx:
container_name: nginx_proxy
image: nginx:latest
restart: always
volumes:
// Here I'm swapping out my default.conf for the container's by mounting my
directory over theirs.
- ./nginx-conf:/etc/nginx/conf.d
ports:
- 80:80
- 443:443
networks:
- proxy
nextcloud_db:
container_name: nextcloud_db
image: mariadb:latest
restart: always
volumes:
- nextcloud_db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/cloud_db_root
MYSQL_PASSWORD_FILE: /run/secrets/cloud_db_pass
MYSQL_DATABASE: devcloud
MYSQL_USER: devcloud
secrets:
- cloud_db_root
- cloud_db_pass
networks:
- database
gitea_db:
container_name: gitea_db
image: mariadb:latest
restart: always
volumes:
- gitea_db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/cloud_db_root
MYSQL_PASSWORD_FILE: /run/secrets/cloud_db_pass
MYSQL_DATABASE: gitea
MYSQL_USER: gitea
secrets:
- cloud_db_root
- cloud_db_pass
networks:
- database
nextcloud:
image: nextcloud
container_name: nextcloud
ports:
- 3001:80
volumes:
- nextcloud:/var/www/html
restart: always
networks:
- proxy
- database
gitea:
container_name: gitea
image: gitea/gitea:latest
environment:
- USER_UID=1000
- USER_GID=1000
restart: always
volumes:
- gitea:/data
ports:
- 3000:3000
- 22:22
networks:
- proxy
- database
volumes:
nextcloud:
nextcloud_db:
gitea:
gitea_db:
networks:
proxy:
database:
secrets:
cloud_db_pass:
file: cloud_db_pass.txt
cloud_db_root:
file: cloud_db_root.txt
My default.conf that gets mounted into /etc/nginx/conf.d/default.conf
upstream nextcloud {
server nextcloud:3001;
}
upstream gitea {
server gitea:3000;
}
server {
listen 80;
listen [::]:80;
server_name cloud.example.ca;
location / {
proxy_pass http://nextcloud;
}
}
server {
listen 80;
listen [::]:80;
server_name git.example.ca;
location / {
proxy_pass http://gitea;
}
}
I of course have my hosts file setup to route the domains to localhost. I've done a bit of googling but nothing I've found so far seems to align with what I'm running into. Thanks in advance!
Long story short, one does not simply reverse proxy to port 80 with nextcloud. It's just not allowed. I have it deployed and working great with a certificate over 443! :)

Resources