3

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 2

3

I think what you want to achieve should be done by mounting a volume of container 'api' to container 'nginx', something like this:

version: "3"

services:
  api:
    image: apiimg
    volumes:
      - apivol:/path/to/statics

  nginx:
    image: nginximg
    volumes:
      - apivol:/var/www/statics

volumes:
  apivol: {}

So there's a shared volume declared for all containers, apivol, which is mapped to /path/to/statics on your Rails container, and to /var/www/statics in your nginx container. This way you don't need to copy anything manually into the nginx container.

Sign up to request clarification or add additional context in comments.

Comments

1

The default location for static content on nginx is /etc/nginx/html, but you could put it in var/www/app_name as long as you remember to add

root /var/www/app_name

in the corresponding location block for your static content.

2 Comments

Ok, I'll try that. That makes a lot of sense.
That is a separate issue. The hosts in the upstream block should not have an http prefix. In fact I wouldn't use an upstream block at all unless you have multiple hosts available for load balancing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.