Jepzen
Jepzen

Reputation: 3162

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?

Upvotes: 0

Views: 702

Answers (1)

David Maze
David Maze

Reputation: 158647

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.

Upvotes: 1

Related Questions