Get started with Docker: PHP/Laravel

In the last post I talked about how to get started with using Vagrant for local development. I also mentioned that I didn’t use Docker because of the learning curve. Maybe it was just an excuse, a mental block.

The best way to overcome that mental block is just to get your hands dirty and see how it goes. So even if Docker might feel intimidating at first, but once you start using it you will realize that all the different parts start to come together.

To start off with Docker I found this as a very good starting point and you will get to learn by doing. Once you finish this, you will start to understand the basics, but then it might be hard to actually write out those Dockerfile/docker-compose files. This is where Laradock comes to rescue and their tagline “Use Docker First – Then Learn About It Later!” reinforces that idea of trying out.

Similar to the way Homestead has everything that you would ever need Laradock seems to be outdoing that and has more than anything that you will ever need for PHP Development.

Steps to get started with Docker using Laradock

I will be using the per project setup, which is a good option if you are not too concerned with disk space. You can even use the same containers for all your projects. Choose what works for you. Following are the basic steps you can follow to get set up with a PHP project on your local machine(Linux):

1. Let’s say we are setting up codisfy.local. Create a New directory

mkdir codisfy-docker && cd codisfy-docker

2. Have your project and laradock on the same level. Assuming you have already cloned your project in the codisfy-docker directory now do this
git clone https://github.com/Laradock/laradock.git

You should end up with following directory structure

codisfy-docker
— codisfy
— laradock

3. Copy over .env-exampe to .env

cd laradock

cp env-example .env

4. Set the path of your project

# Point to the path of your applications code on your host
APP_CODE_PATH_HOST=../codisfy/


4. Change the data directory for mysql. Optional but I configure it just in case I have to recover data later on.

# Choose storage path on your machine. For all storage systems
DATA_PATH_HOST=~/.laradock/codisfy/data

With usage over different projects and machines, I feel it is best to keep the data in the same directory as the project, it will make the project much more portable. You can just zip the root directory, unzip on a different machine and just run, so change above to be

DATA_PATH_HOST=../dockerdata


5. Change the prefix for docker containers. This will be used as prefix for your containers

# Define the prefix of container names. This is useful if you have multiple projects that use laradock to have seperate containers per project.
COMPOSE_PROJECT_NAME=codisfy


6. Copy over example conf file from nginx and create for your own site. For Laravel projects run

cp nginx/sites/laravel.conf.example nginx/sites/codisfy.conf

Edit the codisfy.conf file to update the path in our example it would just be about updating two lines

 server_name codisfy.local;
 root /var/www/public;


7. Update your /etc/hosts file and this to the bottom

127.0.0.1       codisfy.local


8. run docker-compose up nginx mysql for the first time, just to see if there are any errors. If you don’t see any error press ctrl + c and then you can run the containers in the background using docker-compose up -d nginx mysql

9. In your Laravel .env change DB_HOST to be mysql

10. Go to codisfy.local on browser and you should see your homepage.

Other tips:

  1. If you are working with mails enable mailhog by running docker-compose up -d nginx mysql mailhog. Update your mail settings
MAIL_DRIVER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025

Check your inbox at localhost:8025

2. Laradock uses a workspace container, use that to perform commands like composer install etc.

You can add --user=laradock to have files created as your host’s user. Example ( in your codisfy-docker/laradock directory:

docker-compose exec --user=laradock workspace bash

3. Many of the common software packages can be enabled with a flag inside the .env file (laradock .env file not laravel). For example I use wkhtmltopdf on a project all I had to do was to set WORKSPACE_INSTALL_WKHTMLTOPDF to true in the .env file. Check if the package you need is already supported.

I hope using Laradock introduces you to using Docker for local development and will help you learn many Docker concepts as you continue using it.

Leave a Reply

Your email address will not be published. Required fields are marked *