title: Running celery worker + beat in the same container
body: <p>My flask app is comprised of four containers: web app, postgres, rabbitMQ and Celery.  Since I have celery tasks that run periodically, I am using celery beat.  I've configured my docker-compose file like this:</p>
  
  <pre><code>version: '2'
  services:
    rabbit:
      # ...      
    web:
      # ...
    rabbit:
      # ...
    celery:
      build:
          context: .
          dockerfile: Dockerfile.celery
  </code></pre>
  
  <p>And my Dockerfile.celery looks like this:</p>
  
  <pre><code># ...code up here...
  CMD ["celery", "-A", "app.tasks.celery", "worker", "-B", "-l", "INFO"]
  </code></pre>
  
  <p>While I read in the docs that I shouldn't go to production with the <code>-B</code> option, I hastily added it anyway (and forgot about changing it) and quickly learned that my scheduled tasks were running multiple times.  For those interested, if you do a <code>ps aux | grep celery</code> from within your celery container, you'll see multiple celery + beat processes running (but there should only be one beat process and however many worker processes).  I wasn't sure from the docs why you shouldn't run <code>-B</code> in production but now I know.</p>
  
  <p>So then I changed my Dockerfile.celery to:</p>
  
  <pre><code># ...code up here...
  CMD ["celery", "-A", "app.tasks.celery", "worker", "-l", "INFO"]
  CMD ["celery", "-A", "app.tasks.celery", "beat", "-l", "INFO"]
  </code></pre>
  
  <p>No when I start my app, the worker processes start but beat does not.  When I flip those commands around so that beat is called first, then beat starts but the worker processes do not.  So my question is: how do I run celery worker + beat together in my container?  I have combed through many articles/docs but I'm still unable to figure this out.</p>
  
  <p><strong>EDITED</strong></p>
  
  <p>I changed my Dockerfile.celery to the following:</p>
  
  <pre><code>ENTRYPOINT [ "/bin/sh" ]
  CMD [ "./docker.celery.sh" ]    
  </code></pre>
  
  <p>And my docker.celery.sh file looks like this:</p>
  
  <pre><code>#!/bin/sh -ex
  celery -A app.tasks.celery beat -l debug &amp;
  celery -A app.tasks.celery worker -l info &amp;
  </code></pre>
  
  <p>However, I'm receiving the error <code>celery_1 exited with code 0</code></p>
  
  <p>Edit #2</p>
  
  <p>I added the following blocking command to the end of my docker.celery.sh file and all was fixed:</p>
  
  <pre><code>tail -f /dev/null
  </code></pre>