Getting started with Laravel on Docker

In this tutorial, I will show you how you can set up a new Laravel project using Docker. With Docker, you can set up a virtual environment without dependencies from your system, this way your project can run on any given system. If you don’t know what Docker is you can google it and read more. Linux is the chosen operating system.

We will download a new blank version of Laravel, download Laradock (a full PHP development environment for Docker) to handle all the Docker side of the project. Use MySQL for our database and add phpmyadmin (optional).

Downloading all you need to run Laravel on Docker

In this section we will download and install:

  • Git
  • Docker
  • Docker-compose
  • Blank copy of the Laravel framework
  • Laradock

Let’s get started by downloading Git, if you have it already, you can skip this step. In your terminal:

yavengy@statsmaestro:~/Desktop$ sudo apt-get install git

To install docker click here and follow the official Docker instructions.

To use Docker with ease you need to install docker-compose. To install it use official docker site steps:

yavengy@statsmaestro:~/Desktop$ sudo curl -L`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
yavengy@statsmaestro:~/Desktop$  sudo chmod +x /usr/local/bin/docker-compose

In your terminal cd to your preferred installation folder. In my case, I will create a new folder on my desktop.

yavengy@statsmaestro:~/Desktop$ mkdir New_Project
yavengy@statsmaestro:~/Desktop$ cd New_Project

Download Laravel from their official repository at GitHub:

yavengy@statsmaestro:~/Desktop/New_Project$ git clone

After the download is finished rename the Laravel folder to your projects name. In my case, I will name it ‘Blog’. Cd into your new Laravel folder and download Laradock:

yavengy@statsmaestro:~/Desktop/New_Project/Blog$ git submodule add

If this isn’t your first project, you need to rename laradock folder as this can create conflicts between existing docker containers. In my case, I will name it ‘laradock-b’.

Docker and Laravel setup:

Here we will handle all the setup requirements of Docker and Laravel. After the setup, we can run Docker for the first time.

We need to create a new .env file for Docker to run. A default file is supplied with Laradock, it’s named ‘env-example’. We will make a copy of this file and name it .env. Without this file, you won’t be able to run Docker as these are the core settings of Laradock. Cd into your Laradock folder and make a copy of the env file:

yavengy@statsmaestro:~/Desktop/New_Project/Blog$ cd laradock-b
yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ cp env-example .env

For now you don’t need to touch the default env file.

Now we can run Docker for the first time. To run Docker enter:

yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ docker-compose up -d nginx mysql phpmyadmin

The ‘up’ tells docker-compose to start Docker containers, the -d stands for detached. I personally don’t like detached mode, I like to see my system ‘breathe’ and look for errors if any are present. When -d is present you will see the containers start and remain in the terminal. Without -d you will see all containers logs run all the time. Nginx,mysql and phpmyadmin tell Laradock to run these containers too. If you don’t want phpmyadmin you can omit it from the call (docker-compose up -d nginx mysql). Nginx is the web server, without it, you won’t reach your project in the browser. After running this command for the first time, Docker will download the images, this may take a few minutes. The images are downloaded once, next time you run this commands, containers will start immediately.

Now that we have Docker running we need to ssh into our workspace container to continue with the setup process. If you started Docker without the -d in the previous step open a new terminal tab. To ssh into our workspace container we need this command:

yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ docker-compose exec workspace bash

This way we can connect to each of our containers. To see active containers you can run ‘docker ps’ command. You can take container name or id and ssh into it.

When you are in the workspace container you need to run the following commands:

root@8c7af2d39f68:/var/www# composer install
root@8c7af2d39f68:/var/www# cp .env.example .env
root@8c7af2d39f68:/var/www# artisan key:generate
root@8c7af2d39f68:/var/www# exit
yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ cd ..
yavengy@statsmaestro:~/Desktop/New_Project/Blog$ sudo chmod -R 777 storage bootstrap/cache

Composer install will install all Laravel PHP dependencies, this may take a couple of minutes as all dependencies are being downloaded. If you don’t know what composer is, go ahead and google it.

‘cp .env.example .env’ does the same thing we did for Laradock and Docker, this time its Laravel env configurations, we will adjust them in a minute.

Next ‘artisan key:generate’ is generating Laravel application encryption key.

Last but not least ‘sudo chmod -R 777 storage bootstrap/cache’ Laravel framework requires some folders to be writable for the web server user.

Now that we are done with all the initial setup you can open a browser and enter http://localhost. This will open Laravels home page.

MySQL and Phpmyadmin setup

We need to setup Laravel’s connection to our MySQL database:

First thing first, you need to change the password of your database connection. Open Laradock .env file, I am using vim, feel free to use your preferred editor:

yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ vim .env

Find MySQL section and edit MYSQL_ROOT_PASSWORD equals to your new password.


Exit the editor and open docker-compose.yml file:

yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ vim  docker-compose.yml

Find MySQL section, in it you have a volume section. You need to change the first line:


This line tells Docker where to save Mysql data, this is the default folder. In case you already have a project with the same path, you will see its data in your new project. Change the path to: ${DATA_SAVE_PATH}/mysql/Blog:/var/lib/mysql. If you define a new path for each project you create you’ll have a clean database to start with, which is nicer then having all the databases.

Restart docker, if you used -d then run this command:

yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ docker-compose down

If you didn’t use -d, press Cntrl+C in the terminal. This will gracefully shut down Docker containers.

After termination run docker-compose up again:

yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ docker-compose up  nginx mysql phpmyadmin

We need to create a new database, you can do it in two ways:

Phpmyadmin — You can create it by opening phpmyadmin. Go to http://localhost:8080 in your browser. In the login page enter server: mysql, username: root and the password you inserted in this section first step. Once inside you can click on new in the upper left corner and create a database.


Another option is to create the database from mysql container:

yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ docker-compose exec mysql bash
root@c142a99dbf35:/# mysql -uroot -p

You will need to enter your password from step one of this section. Once inside mysql you can create a new database by typing:

mysql> create database blog;

This will create a new database named blog.

Now we need to update Laravel’s .env file to connect to our database. To do this open the file:

yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ cd ..
yavengy@statsmaestro:~/Desktop/New_Project/Blog$ sudo vim .env

We need to change 4 parameters here:

  • DB_HOST=mysql — this will tell Laravel that MySQL address is mysql docker container.
  • DB_DATABASE=blog — This is the name of our new database we created.
  • DB_USERNAME=root — our root user.
  • DB_PASSWORD=********* — your pasword from the beginning of the section.
Laravel’s .env file

Save the file and exit.

We are now ready to test Laravel’s connection to our database:

SSH into our Laradock workspace:

yavengy@statsmaestro:~/Desktop/New_Project/Blog/laradock-b$ docker-compose exec  workspace bash

Laravel has two default migrations which will create a couple of tables in your brand new database. If something went wrong with your setup process the migrations won’t execute correctly.

From inside the container run command:

root@8c7af2d39f68:/var/www# artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table

You can check these tables were created in your blog database. Now you can start working on your brand new Laravel project on Docker.

This is it, I hope I helped some of you with this tutorial and if you had problems with it feel free to write a comment and I’ll happily try to help.