Recommend way to Artisan on Docker

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.


Docker 1.3 bring new command exec So 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.


I use SSH and run migrations from a terminal inside the container.

I personally like Phusion's approach of using Docker as a 'lightweight virtual machine'. So I used their baseimage-docker which I've extended to create my own Docker image for Laravel applications.

I'm aware Phusion's image can be contentious in the Docker community, and that SSH is frowned upon by some who advocate Docker containers as microservices. But I'm happy with Phusion's approach until there are more established tools and practices for the multi-container approach.