How do I deploy my containerized Nextjs and Django app to render?
I successfully containerized my Django and Nextjs apps with docker, these containers are running locally, but deploying to Render a hosting platform, I have an issue binding my service to a port, at least that's the log error message I am getting, Port scan timeout reached, no open ports detected. Bind your service to at least one port. If you don't need to receive traffic on any port, create a background worker instead.
docker-compose.yaml
version: "3.9"
services:
postgis:
image: geonode/postgis:15.3
container_name: postgis4agro_watcher
volumes:
- ./init-db:/docker-entrypoint-initdb.d/
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
ports:
- "5432:5432"
restart: always
networks:
- agrowatcher-api
nginx:
build:
context: ./nginx
dockerfile: Dockerfile
container_name: nginx4agro_watcher
restart: always
depends_on:
- awatcher-frontend
ports:
- 8080:8080
networks:
- agrowatcher-api
django:
build:
context: .
dockerfile: Dockerfile
container_name: django4agro_watcher
env_file:
- .env
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./:/home/geouser/agrowatcher
ports:
- "8000:8000"
restart: always
depends_on:
- postgis
networks:
- agrowatcher-api
awatcher-frontend:
build:
context: ./awatcher-frontend
dockerfile: Dockerfile
container_name: awatcherfrontend4agro_watcher
volumes:
- ./awatcher-frontend:/usr/src/app
- /usr/src/app/node_modules
ports:
- "3000:3000"
environment:
- NODE_ENV=development
# stdin_open: true
# tty: true
depends_on:
- django
networks:
- agrowatcher-api
networks:
agrowatcher-api:
driver: bridge
volumes:
postgres_data:
I created a shell script to use the environment variable or default to 8080 if not available
start-nginx.sh:
#!/bin/sh
# Use the environment variable or default to 8080
LISTEN_PORT=${PORT:-8080}
# Replace the port in the nginx.conf
sed -i "s/listen 8080;/listen ${LISTEN_PORT};/" /etc/nginx/conf.d/default.conf
# Start Nginx
exec nginx -g 'daemon off;'
Nginx-dockerfile:
FROM nginx:1.27-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Copy the startup script
COPY start-nginx.sh /start-nginx.sh
RUN chmod +x /start-nginx.sh
# Use the startup script as the entry point
CMD ["/start-nginx.sh"]
nginx.conf:
upstream django {
server django:8000;
}
upstream frontend {
server frontend:3000;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://frontend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location api/v1/farms {
proxy_pass http://django;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect http://django http://$server_name/;
}
}