gpt4 book ai didi

docker - 将 nginx 容器指向 docker 中的静态文件

转载 作者:行者123 更新时间:2023-12-02 18:15:35 25 4
gpt4 key购买 nike

我对 Docker 和 Nginx 很陌生,这可能是一个愚蠢的问题,但是我如何指向我的 nginx 框来查看 Rails public 中的文件?基本上,我有一个 nginx 盒子和一个应用程序盒子。我想知道我可以把这些文件放在哪里,以便 nginx 盒子可以读取它们。

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 Docker 文件:
FROM nginx

ENV INSTALL_PATH /nginx

RUN mkdir -p $INSTALL_PATH

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

# COPY ?

EXPOSE 80

nginx 配置(这被正确地复制了)
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
}
}
}

现在,当我转到 localhost:80它显示了通用的 nginx 文件夹。但是,我不确定如何将 rails api/public/的公共(public)目录链接到 nginx 容器。

我可以吗 COPY path/to/rails/public path/nginx . nginx 期望在哪里找到这些文件?

编辑

我相信我应该把它们放在 /var/www/app_name , 正确的?

最佳答案

我认为你想要实现的应该通过将容器'api'的卷安装到容器'nginx'来完成,如下所示:

version: "3"

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

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

volumes:
apivol: {}

因此,为所有容器声明了一个共享卷, apivol , 映射到 /path/to/statics在您的 Rails 容器上,并发送至 /var/www/statics在你的 nginx 容器中。这样您就不需要手动将任何内容复制到 nginx 容器中。

关于docker - 将 nginx 容器指向 docker 中的静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45680014/

25 4 0
文章推荐: performance - Docker的调试和实验性标志是否会对性能产生影响?
文章推荐: reactjs - 下面代码中的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com