September 26, 2018

Laravel "artisan" in docker container

At this stage the recommended way of executing the artisan command in a docker environment is to run a container whenever one needs artisan.

The below is my setup on Linux/ macOS.
In ~/.zshrc (or ~/.bashrc or whatever flavour of console you are running) create the following function:

artisan () {
    docker run \
        -t \
        --rm \
        -v $(pwd):/var/www \
        vcarreira/artisan "$@"

Then run source ~/.zshrc and you'll be able to execute artisan the same you're used to it.

The first time you execute the artisan command, docker will pull the image vcarreira/artisan.
The container will run, stop and remove itself as needed.

There are options to vcarreira/artisan on Docker Hub.
Pick whichever works for you.