Recommend way to Artisan on Docker
The best practice regarding Docker is to run each process inside it's own container. Therefore, the ideal way to run artisan
commands is to have an image for creating containers specifically for this purpose.
I've created an image which can be pulled from the Docker Hub dylanlindgren/docker-laravel-artisan
and it works really well. It's on GitHub as well if you want to take a look at the Dockerfile
behind it.
I've also just written a blog post describing the way that all these seperate containers fit together.
There are a couple of possibilities...
Mounting a host directory in your container as the folder in which your Laravel app lives. That way you can just run
php artisan migrate
orcomposer update
from the host. You might have problems with deployment, though, since you would have to replicate that part of your environment on the server.adding an SSH server to your container (which is not recommended; here's a good discussion of that).
build and use nsenter, a tool for "entering" a running container and getting shell access. Note, I haven't used it, I just found it a while ago via a reference in the link above.
If you're primarily interested in deployment and you're doing it via a dockerfile, then the answer would be to add composer install
and php artisan migrate
to your dockerfile so they run when the container is built.
I'm interested in hearing more answers to this. It's something that I'm just getting into as well and would like to learn more about.