简体   繁体   English

我如何隔离不同容器中的Rails API和Rails worker工作

[英]How can i isolate Rails API and Rails workers job in diffrent containers docker

I'm planning dockerize my existing Rails project. 我正在计划对现有的Rails项目进行dockerize。

My project has an API controller and worker to run jobs in background 我的项目有一个API控制器和一个工作程序,可在后台运行作业

Is there a way to separate my application in 3 parts using docker containers: 有没有一种方法可以使用Docker容器将我的应用分为三部分:

Eg: 例如:

  • One Container for the application 一个容器的应用程序
  • One Container for run Rails jobs 一个用于运行Rails作业的容器
  • One Container for the Rails API Rails API的一个容器

I know that i can use one container for the application and another to database, but my question is about isolate the application. 我知道我可以为应用程序使用一个容器,而对数据库使用另一个容器,但是我的问题是关于隔离应用程序。

Typically, your jobs will be saved to a backend- eg Redis for Sidekiq, some sql database for delayed_jobs. 通常,您的作业将保存到后端,例如Redis用于Sidekiq,一些sql数据库用于delay_jobs。 The actual execution of the jobs can be placed somewhere else. 作业的实际执行可以放在其他位置。

In the web server's Dockerfile you might have the entry point written like this (example): 在Web服务器的Dockerfile中,您可能具有如下所示的入口点(示例):

CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]

This runs only the web-server. 这仅运行Web服务器。 To be honest, I think the "application" and the API that you're talking about should run off the same container. 坦白地说,我认为您所讨论的“应用程序”和API应该在同一容器中运行。


For the execution of the jobs, you can use the same image, but define a different entry point. 为了执行作业,可以使用相同的图像,但是定义不同的入口点。 (Check out docker-compose , it's really handy) (查看docker-compose ,非常方便)

In there, you would ask it to.. start up some workers for instance, and that command would depend on which background processing gem you chose to go with. 在那儿,您会要求它..例如启动一些工作程序,而该命令将取决于您选择使用哪个后台处理gem。 You will also need to pass it the configuration for the database that the jobs are stored in. 您还需要将存储作业的数据库的配置传递给它。


An sample docker-compose file that you might write might look like this: 您可能会编写的一个示例docker-compose文件可能如下所示:

version: '2'
services:
  redis:
    image: 'redis:3.2-alpine'
    command: redis-server
    ports:
      - 6379:6379
    volumes:
      - redis:/data

  app:
    depends_on:
      - redis
      - postgres (or some other db you have)
    build: .
    ports:
      - 3000:3000
    volumes:
      - .:/app

  sidekiq:
    depends_on:
      - redis
    build: .
    command: sidekiq -C config/sidekiq.yml.erb
    volumes:
      - .:/app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM