title: Do I have to create a new docker container to run each rake task?
body: <p>Say I have a <code>Dockerfile</code> that will run a Ruby on Rails app:</p>
  
  <pre><code>FROM ruby:2.5.1
  
  # - apt-get update, install nodejs, yarn, bundler, etc...
  # - run yarn install, bundle install, etc...
  # - create working directory and copy files
  # ....
  
  CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
  </code></pre>
  
  <p>From my understanding, a container is an immutable running instance of an image and a set of runtime options (e.g. port mappings, volume mappings, networks, etc...). </p>
  
  <p>So if I build and start a container from the above file, I'll get something that executes the default <code>CMD</code> above (<code>rails server</code>)</p>
  
  <pre><code>docker build -t myapp_web:latest
  docker create --name myapp_web -p 3000:3000 -v $PWD:/app -e RAILS_ENV='production' myapp_web:latest
  docker start myapp_web
  </code></pre>
  
  <p>Great, so now it's running <code>rails server</code> in a container that has a <code>CONTAINER_ID</code>. </p>
  
  <p>But lets say tomorrow I want to run <code>rake db:migrate</code> because I updated something. How do I do that? </p>
  
  <ol>
  <li><p>I can't use <code>docker exec</code> to run it in that container because <code>db:migrate</code> will fail while <code>rails server</code> is running</p></li>
  <li><p>If I stop the <code>rails server</code> (and therefore the container) I have to create a <em>new container</em> with the same runtime options but a different command (<code>rake db:migrate</code>), which creates a new <code>CONTAINER_ID</code>. And then after runs that I have to restart my original container that runs <code>rails server</code>. </p></li>
  </ol>
  
  <p>Is #2 just something we have to live with? Each new rake task I run that requires <code>rails server</code> to be shut down will have to create a new CONTAINER and these pile up over time. Is there a more "proper" way to run this? </p>
  
  <p>Thanks!</p>
  
  <p><em>EDIT</em>: If #2 is the way to go, is there an easy way to create a new container from an existing container so I can copy over all the runtime configs and just change the command?</p>