Pointing nginx container to static files in docker

Viewed 2531

I'm very new to Docker and Nginx and this might be a stupid question but how can I point my nginx box to look at the files in Rails public? Basically, I have an nginx box, and an application box. I would like to know where I can put those files so that the nginx box can read them.

version: "3"

services:
  api:
    build: "./api"
    env_file:
      - .env-dev
    ports:
      - "3000:3000"
    depends_on:
      - db
    volumes:
      - .:/app/api
    command: rails server -b "0.0.0.0"
  nginx:
    build: ./nginx
    env_file: .env-dev
    volumes:
      - .:/app/nginx
    depends_on:
      - api
    links:
      - api
    ports:
      - "80:80"
    ...

Api dockerfile:

FROM ruby:2.4.1-slim


RUN apt-get update && apt-get install -qq -y \
  build-essential \
  libmysqlclient-dev \
  nodejs \
  --fix-missing \
  --no-install-recommends

ENV INSTALL_PATH /api

RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY Gemfile $INSTALL_PATH

RUN bundle install

COPY . .

EXPOSE 3000

Nginx Dockerfile:

FROM nginx

ENV INSTALL_PATH /nginx

RUN mkdir -p $INSTALL_PATH

COPY nginx.conf /etc/nginx/nginx.conf

# COPY ? 

EXPOSE 80

nginx config (this is correctly being copied over)

daemon off;

worker_processes: 1;

events { worker_connections: 1024; }

http {
  sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;
  # Rails Api
  upstream api {
    server http://api/;
  }

   # Configuration for the server
    server {

        # Running port
        listen 80;

        # Proxying the connections connections
        location /api {
            proxy_pass         http://api;
            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;
        }

        error_page 500 502 503 504 /public/50x.html
        error_page 404 /public/404.html

        location = /50x.html {
            root /api/public;
        }

        location = /404.html {
            root /api/public
        }
    }
}

Now, when I go to localhost:80 it show the generic nginx folder. However, I'm unsure how to link the public dir of rails api/public/ to the nginx container.

Can I just COPY path/to/rails/public path/nginx. Where is nginx expecting to find those files?

Edit

I believe I should be putting them in /var/www/app_name, correct?

2 Answers
Related