I think the issue here is that some users want something in the middle of the out of the box functionality for exec
vs run
.
docker-compose run
stands up a new container, that then needs to be cleaned up.
docker-compose exec
only works on an existing and RUNNING container, which errors out if the container is not running.
As far as I know there is not a command or "one-liner" to accomplish the desired effect of starting a potentially stopped container and running a one-off command.
If you want to avoid the expense of creating a new container and then destroying it every time as suggested in the accepted answer by using the --rm
flag; as pointed out in ldg's comment.
Then you have to manually start the container and then run exec
to get the desired effect.
I recommend the following concatenated commands:
docker-compose start web; docker-compose exec web bin/rails c
NOTE: assuming you have dependencies set up in your docker-compose file, those will also be started with the start
command.