Set up Laravel Project With Docker

Gerry Sabar
3 min readAug 30, 2019

--

In this article I assume you have installed docker and docker-compose successfully regardless your environment is. Otherwise, you can visit docker documentation on how to do that. To set up a Laravel project with Docker, first we need to create a Laravel project. There are few ways we can do that and you can refer to Laravel documentation at installation section. In this article I’m using composer create project and named it as laravel-docker

composer create-project --prefer-dist laravel/laravel laravel-docker

you don’t need to create the project in var/www/html since this will be a stand-alone application but you do it if you like to do so. Usually this will be in home directory.

After you successfully create a new Laravel project, next go to laravel-docker folder and let’s create a file named “Dockerfile” with code as follow:

Next, let’s create a new file named “docker-compose.yml” at the same level with Dockerfile with code as follow:

create a new directory named “php” inside laravel-docker and create a file named local.ini this file will be used for your php settings. For now let’s add setting for upload max file & post size :

upload_max_filesize=40M
post_max_size=40M

from root project directory (laravel-docker) create a new directory named mysql and create a file named my.cnf. This will be used as mysql setting in your Docker. Let’s add this code:

[mysqld]
general_log = 1
general_log_file = /var/lib/mysql/general.log

Last, let’s create a directory named nginx and add a file named conf.d for nginx server configuration with code as follow:

Now we’re ready for the Docker configuration. Let’s add .env file for Laravel configuration by copying from default laravel for now using this command in terminal:

$ cp .env.example .env

Now let’s make docker run with this command:

$ sudo docker-compose up -d

After you successfully run, we need to add encryption key for our Laravel encryption. We add it using this command:

$ sudo docker-compose exec app php artisan key:generate

Now if you access localhost:8888 you can see something like this:

Now let’s add connection to database by adjusting the setting in .env by adding MySQL credential from we have provided before in docker-compose.yml. Please take a note that DB_HOST is using ip address from Docker. How to know Docker MySQL IPAddress? you can execute this command:

$ sudo docker inspect YOUR_MYSQL_DOCKER_ID | grep "IPAddress"

you’ll get the IPAddress then put it in DB_HOST in your .env file. For example my Docker IPAddress is 172.25.0.4 so according to our previous settings my MySQL connection in .env file will be as follow:

DB_CONNECTION=mysql
DB_HOST=172.25.0.4
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=YOUR_MYSQL_PASSWORD

Let’s test it by running laravel migrations using this command:

$ docker-compose exec app php artisan migrate

You can see Laravel executes default migration file. Congratulation, now you’ve successfully set up Laravel in Docker. Working code for this project can be accessed here .

--

--