2
votes

I am building a Rails application and I am setting up the application for deployment on docker with Nginx as the webserver. I am, however, having issues setting up Nginx with Docker for the application.

I keep getting this error when I run docker-compose up:

nginx: [emerg] host not found in upstream "app:3000" in /etc/nginx/conf.d/mailing_list.conf

Here's my docker-compose.yml file:

version: '3'
 
services:
  app:
    build:
      context: .
      dockerfile: ./docker/${RAILS_ENV}/Dockerfile
    depends_on:
      - database
    ports:
      - "3000:3000"
    restart: always
    volumes:
      - .:/app
      - gem-cache:/usr/local/bundle/gems
      - node-modules:/app/node_modules
    env_file:
      - .env
    environment:
      RAILS_ENV: ${RAILS_ENV}
      RACK_ENV: ${RACK_ENV}
 
  database:
    image: postgres:12.1
    expose:
      - "5432"
    restart: always
    env_file:
      - .env
    environment:
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
      POSTGRES_DB: ${DATABASE_NAME}
      POSTGRES_HOST_AUTH_METHOD: ${DATABASE_HOST}
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
  nginx:
    build:
      context: .
      dockerfile: ./docker/nginx/Dockerfile
    depends_on:
      - app
    ports:
      - "8080:8080"
    restart: always
    volumes:
      - .:/app
      - nginx-config:/etc/nginx
      - nginx-log:/var/log/nginx
 
volumes:
  gem-cache:
  nginx-config:
  nginx-log:
  node-modules:
  postgres-data:

And here's my nginx.conf file:

upstream app {
  server app:3000;
}
 
server {
    listen 8080;
    listen [::]:8080;
 
    root app/public;
    index index.html index.htm;
 
    server_name localhost;
 
    location /app {
      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-Proto $scheme;
      proxy_pass http://app;
    }
}

I have tried a lot of solutions, but none seems to work.

I keep getting this error when I run docker-compose up:

nginx: [emerg] host not found in upstream "app:3000" in /etc/nginx/conf.d/mailing_list.conf

Any form of help would be appreciated. Thank you.

1
Even with depends_on, there could be some delay where app is not responding to requests. I think this is a good workaround. - chash
Thank you @hmm, let me check through the it right now. - Promise Preston

1 Answers

0
votes

After several hours of research and trials I finally figured this out.

The issues had to do with wrong configurations in my docker-compose.yml and my_app.conf (Nginx configuration) files.

Here's the correct configuration:

The Dockerfile for Nginx:

FROM nginx:1.18.0
LABEL maintainer="[email protected]"

# Set working directory
WORKDIR /app

# Copy over static assets
COPY public public/

# Copy over entrypoint
COPY docker/entrypoints/nginx-entrypoint.sh /usr/local/bin/nginx-entrypoint.sh

# Copy Nginx config template
RUN rm /etc/nginx/conf.d/default.conf
COPY docker/nginx/my_app.conf /etc/nginx/conf.d/my_app.conf

# Nginx init
RUN ["chmod", "+x", "/usr/local/bin/nginx-entrypoint.sh"]
ENTRYPOINT ["/usr/local/bin/nginx-entrypoint.sh"]

The docker-compose.yml file (This contains my app, database, and Nginx configuration):

version: '3'

services:
  app:
    build:
      context: .
      dockerfile: ./docker/${RAILS_ENV}/Dockerfile
    depends_on:
      - database
    expose:
      - "3000"
    restart: always
    volumes:
      - .:/app
      - gem-cache:/usr/local/bundle/gems
      - node-modules:/app/node_modules
    env_file:
      - .env
    environment:
      RAILS_ENV: ${RAILS_ENV}
      RACK_ENV: ${RACK_ENV}

  database:
    image: postgres:12.1
    expose:
      - "5432"
    restart: always
    env_file:
      - .env
    environment:
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
      POSTGRES_DB: ${DATABASE_NAME}
      POSTGRES_HOST_AUTH_METHOD: ${DATABASE_HOST}
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
  nginx:
    build:
      context: .
      dockerfile: ./docker/nginx/Dockerfile
    depends_on:
      - app
    ports:
      - "8084:80"
      # - "443:443"
    restart: always
    volumes:
      - .:/app

  volumes:
    gem-cache:
    node-modules:
    postgres-data:

The my_app.conf file for Nginx:

upstream rails_app {
  server app:3000;

}

server {
  listen       80;

  # define your domain
  server_name localhost;

  # define the public application root
  root   /app/public;
  index  index.html index.htm;

  # define where Nginx should write its logs
  access_log /app/log/nginx.access.log;
  error_log /app/log/nginx.error.log;

  # deny requests for files that should never be accessed
  location ~ /\. {
    deny all;
  }

  location ~* ^.+\.(rb|log)$ {
    deny all;
  }

  # serve static (compiled) assets directly if they exist (for rails production)
  location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
    try_files $uri @rails;

    access_log off;
    gzip_static on; # to serve pre-gzipped version

    expires max;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }

  # send non-static file requests to the app server
  location / {
    try_files $uri @rails;
  }

  location @rails {
    proxy_pass        http://rails_app;
    proxy_redirect    off;
    proxy_set_header  Host $http_host;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Real-IP  $remote_addr;

  }

  # redirect server error pages to the static page /50x.html
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
      root   /app/public;
  }
}

The nginx-entrypoint.sh file for Nginx:

#!/bin/sh

set -e

# Allow nginx to stay in the foreground
# so that Docker can track the process properly
nginx -g 'daemon off;'

Note:

  1. I used variables like RAILS_ENV, RACK_ENV, and lots more, which you may not need.
  2. Also, my WORKDIR for Nginx is /app, and so it's important to specify it as a volume under the Nginx configuration in the docker-compose.yml file just like I did.
  3. I mapped Nginx to port 8084 on the host machine, you are free to map it to any available port on the host machine.

That's all.

I hope this helps