Ember不能用回形针和Docker从Rails后端检索图像资产

这只是生产中的一个问题。

我有两个Docker主机,一个运行一个nginx容器(提供一个Ember应用程序),另一个运行postgres容器和一个rails-api容器。 我想将后者2个容器移到第一个Docker主机上,这样它们都可以托pipe在一个主机上,我可以删除第二个Docker主机。 Rails-api使用回形针上传照片。

在Ember应用程序生产版本中,我通过在适配器/ application.js ENV.host = 'dockerhost2.com:3000/' host: ENV.host / ENV.host = 'dockerhost2.com:3000/'添加到config / environment.js和host: ENV.host中来指向rails后端。

如果我保留所有的东西(2个docker主机),一切都很奇怪 – 也就是说,图像被传送并显示在前端。 但是,如果我将所有的容器移动到一个docker主机,图像本身不能被检索,并返回一个404。

图片url总是“public / system / 000 / path / to / Image.jpg”。 当我把事情分开的时候(在2个docker主机上)转到'dockerhost1.com/public/system/000/path/to/Image.jpg'可以完美的工作,虽然我不明白为什么,因为rails应用程序在端口上3000和不同的主机上。 但是,当我把所有的容器合并到一个docker主机上时,相同的图像URL将返回404,并且只有当我进入“dockerhost1.com:3000/public/system/000/path/to/Image.jpg”时才起作用。 这是令人困惑的。

当我把所有的容器放到1docker机器上,并build立Ember应用程序指向docker.machine.ip:3000时,本地所有的东西都很漂亮。 去“docker.machine.ip / public / system / 000 / path / to / Image.jpg”确实find了这个图像,但是我承认,我不明白为什么。

我的Ember docker-compose.yml

 version: '2' services: server: image: nginx volumes: - ./dist:/usr/share/nginx/html - ./nginx-site.conf:/etc/nginx/conf.d/default.conf ports: - "80:80" 

我的铁轨docker-compose.yml

 version: '2' volumes: sarahdeyong-postgres: images: services: rails: &defaults build: . image: romanhood/sarahdeyong-api:0.0.1 volumes: - .:/myapp - images:/myapp/public/system/ depends_on: - postgres entrypoint: ["bundle", "exec", "rails"] server: <<: *defaults tty: true stdin_open: true command: server -p 3000 -b '0.0.0.0' ports: - "3000:3000" postgres: image: postgres volumes: - sarahdeyong-postgres:/var/lib/postgresql/data ports: - "5432:5432" 

和nginx-site.conf:

 # pushState friendly! # The setup: # * website name is `_` # * javascript app is located at `/app` charset utf-8; tcp_nopush on; tcp_nodelay off; client_header_timeout 10s; client_body_timeout 10s; client_max_body_size 128k; reset_timedout_connection on; gzip on; gzip_types text/css text/javascript text/xml text/plain application/javascript application/x-javascript application/json application/xml application/rss+xml application/atom+xml font/truetype font/opentype image/svg+xml; server { listen 80; server_name sarahdeyong; root /usr/share/nginx/html; # To make sure any assets can get through :) location / { try_files $uri @rewrites; } # If no asset matches, send it to your javascript app. Hopefully it's a route in the app! location @rewrites { rewrite ^(.+)$ /index.html last; } } 

我怎样才能让我的图像检索和正确显示在我的Ember应用程序?

既然你已经在使用docker,为什么不使用它的内部networkingfunction。 我怀疑这个应用程序应用程序连接到localhost / domain1.com时遇到了问题,因为它指的是容器而不是主机。

创build一个dockernetworking,并将两个容器放在上面。 这实际上连接了本地networking上的容器。 然后你可以通过他们的名字来引用容器。 因此,在烬应用程序中,您只需使用name-of-rails-container:3000来访问它。 不需要在rails应用上公开端口3000。

https://docs.docker.com/compose/networking/