Recommend way to Artisan on Docker Recommend way to Artisan on Docker laravel laravel

Recommend way to Artisan on Docker


Docker 1.3 bring new command execSo now you can "enter" running container like

docker exec -it my-container-name /bin/bash

After that you can run any command you want

php artisan --version


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...

  1. 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 or composer 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.

  2. adding an SSH server to your container (which is not recommended; here's a good discussion of that).

  3. 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.