首页 > 解决方案 > 配置 docker 卷以跨主机和容器共享数据

问题描述

我被困在尝试配置 docker 卷以在我的主机之间共享文件并在我的容器中使用这些文件。让我解释。

我有一个带有 puma 作为 Web 服务器的 rails docker 应用程序,我想让 puma 能够查看和使用 ssl .key 和 .crt 文件,所以对于这个项目,我也在“生产模式”下使用 docker-compose,但我不知道如何使这项工作。

我的设置是这样的:

/home/ubuntu/docker-compose.yml

version: '3'

services:
  postgres:
    image: postgres:10.5
    environment: 
      POSTGRES_DB: my_app_production
    env_file:
      -~/production.env

  redis:
    image: redis:4.0.11

  web:
    image: my_app:latest
    command: bundle exec rails server -p 3000 -b 'ssl://127.0.0.1:3000?key=/home/ubuntu/my_app_keys/server.key&cert=/home/ubuntu/my_app_keys/server.crt' -e production
    ports:
      - '3000:3000'
    volumes:
      - /home/ubuntu/my_app_keys
    depends_on:
      - postgres
      - redis
    env_file:
      - ~/production.env
    restart: always

  sidekiq:
    image: my_app_sidekiq:latest
    command: bundle exec sidekiq -C config/sidekiq.yml
    depends_on:
      - postgres
      - redis
    env_file:
      - ~/production.env
    restart: always

因此,如您所见:command: bundle exec rails server -p 3000 -b 'ssl://127.0.0.1:3000?key=/home/ubuntu/my_app_keys/server.key&cert=/home/ubuntu/my_app_keys/server.crt'正在 /home/ubuntu/my_app_keys 中寻找 ssl 文件,当我执行docker-compose uppuma 时找不到 ssl 文件并退出:

/usr/local/bundle/gems/puma-3.9.1/lib/puma/minissl.rb:180:in `key=': No such key file '/home/ubuntu/my_app_keys/server.key' (ArgumentError)

我认为是因为key=/home/ubuntu/my_app_keys/server.key&cert=/home/ubuntu/my_app_keys/server.crt指向容器上下文,但我的主机上下文中有证书和密钥

因此,我在 docker compose 卷中包含以绑定挂载文件:

volumes:
  - /home/ubuntu/my_app_keys

但没有运气,同样的错误。

在容器上下文中,我的应用程序位于/var/www/my_app目录中,因此我尝试指定一个绝对路径(出于某种原因,我认为这是因为 ssl 文件不在我的应用程序所在的同一目录中无法共享),所以我添加正如compose-file 文档所说:

volumes:
  - /home/ubuntu/my_app_keys:/var/www/my_app

并更改撰写文件:

command: bundle exec rails server -p 3000 -b 'ssl://127.0.0.1:3000?key=server.key&cert=server.crt' -e

当我执行组合我的 Web 服务退出时出现错误:

web | Could not locate Gemfile or .bundle/ directory

Web 服务运行的唯一方法是(但不存在 ssl 文件):

volumes:
  - /home/ubuntu/my_app_keys

所以,我不知道现在该怎么办。有什么帮助吗?

标签: ruby-on-railsdockerssldocker-compose

解决方案


When your Docker Compose YAML file says:

volumes:
  - /home/ubuntu/my_app_keys

It means, "make /home/ubuntu/my_app_keys in container space persist across restarts of the container; it will start off empty unless the Dockerfile did something special; it's not connected to any specific host content".

When you say:

volumes:
  - /home/ubuntu/my_app_keys:/var/www/my_app

It means, "totally replace the contents of /var/www/my_app in container space with the contents of /home/ubuntu/my_app_keys on the host". (The path names in host and container space don't need to be the same.)

As a bonus question, when you say:

rails server -b 'ssl://127.0.0.1:3000?...'

It means, "only listen for inbound connections on port 3000 initiated from within this Docker container; don't accept any connections from outside the container at all, whether from the same physical host, other containers, or elsewhere."


推荐阅读