Helpful commands for Laradock

Ben

March 25, 2020

In my previous posts I have talked about my experiences with Laradock, and how it has helped with development in PHP. In this short post I wanted to just share some commands that I found useful when using Laradock.

Configure

To make the commands work we need to configure a few items, firstly we need the repo directory that is going to be used. This is the location that is going to be mounted into the docker workspace. If the script with these commands is in the root of the Laradock clone, and you follow the pattern in the gist then the commands should work automatically.

Start, Stop & Build

As I routinely start and stop Laradock as to not waste resources when not using PHP, I use an alias to start and stop the containers when I need them. I also use an alias to provide a quick way to rebuild if needed, but I have rarely needed it.

alias larastart='docker-compose -f ${REPO_DIR}/laradock/docker-compose.yml --project-directory=${REPO_DIR}/laradock up -d nginx'
alias larastop='docker-compose -f ${REPO_DIR}/laradock/docker-compose.yml --project-directory=${REPO_DIR}/laradock down'
alias larabuild='docker-compose -f ${REPO_DIR}/laradock/docker-compose.yml --project-directory=${REPO_DIR}/laradock up --build -d nginx'

Other commands

If you have ever used PHP to develop then at some point you will need to use composer to install your dependencies or call PHP directly for checking some command or version etc. When using Laradock these things can get a bit complex as you need to execute the commands inside the docker containers. As I am a lazy developer I quickly got bored of using typing the 'docker exec' command to get into the docker container, so I wrote a few aliases to do it all for me.

alias composer='docker exec -it --user=laradock laradock_workspace_1 composer --working-dir=/var/www/${PWD:${#REPO_DIR}}'
alias php='docker exec -it --workdir=/var/www/${PWD:${#REPO_DIR}} --user=laradock laradock_workspace_1 php'

These commands allow me to run 'php' or 'composer' in my host terminal and execute the actual command inside the workspace Laradock container. This makes it feel much more like a native PHP installation.

tl;dr;

There are a few commands available on github, that can be sourced into your environment to make it easier to use Laradock.

Gist available here: https://gist.github.com/Umaaz/f1778ac35bbe4c3f2597c0c27737eeee

Any improvements to these commands are welcome.

Ben

Ben

Experienced developer in various languages, currently a product owner of nerd.vision leading the back end architecture.