简体   繁体   中英

Can I run my rake jobs:work in the same docker container as I run my rails server?

My docker compose file

 web:
    build: .
    command: bundle exec rails s -b 0.0.0.0 -p 3000
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    links:
      - db
      - db-test
    depends_on:
      - db
      - db-test

Then I usually login into the container with

docker-compose exec web bash

and then run

rake jobs:work

Is it possible to run both things and skip the last step?

If you have two long-running tasks you want to do on the same code base, you can run two separate containers off the same image. In Docker Compose syntax that would look like

version: '3'
services:
  db: { ... }
  db-test: { ... }
  web:
    build: .
    command: bundle exec rails s -b 0.0.0.0 -p 3000
    ports:
      - "3000:3000"
    depends_on:
      - db
      - db-test
  worker:
    build: .
    command: bundle exec rake jobs:work
    depends_on:
      - db
      - db-test

Running multiple tasks in one container is kind of tricky and isn't usually recommended. The form you arrived on in comments, for example, launches the Rails server as a background task, and then makes the worker the main container process; if for some reason the main Rails application dies, Docker won't notice this, and if the worker dies, it will take the Rails application with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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