DEV Community

Cover image for The beauty of Docker for local Laravel development

The beauty of Docker for local Laravel development

Andrew Schmelyun on May 06, 2019

tl;dr Want to take the fast track and skip the detailed tutorial below? Install Docker for your OS, clone this repo, add your Laravel ap...
Collapse
 
skyeun profile image
Skyeun • Edited

Hi! I used your work as an inspiration for my Symfony 4 container, and it work perfectly. But php container (I think it's him) create a html folder inside of my /var/www folder, and I'm unable to understand why. Any clues?

Here is the link to my repo: github.com/guillotjulien/symfony4-...

Thanks :)

EDIT:

Just add working_dir: /var/www to php service :)

Collapse
 
sushil_shrestha_5873d20c4 profile image
Sushil Shrestha

Hi! Thank you for great blog tutorials. It really helped me to get started. I have also watched you youtube video which took me here.

I am able to deploy container for laravel succesfully and now I want to install image GD library for PHP. I simply tried adding RUN docker-php-ext-install gd. But it didnot worked. Also, I want to upload large images 5 MB for which I need to make changes to NGINX config. I have tried modifying nginx.dockerfile but its now working. Can you please help me through it?

Collapse
 
tzalejo profile image
tzalejo

could you solve it?

Collapse
 
_devlim profile image
I'M DEVLIM ⛱️

Files generate in /src folder when execute artisan command inside php container is own by root,
example, running following command

docker-compose exec php php /var/www/artisan make:controller ExampleController
Enter fullscreen mode Exit fullscreen mode

will generate ExampleController.php on host machine and file is own by root.

Any idea how to avoid this?

Collapse
 
robertocifuentes profile image
Roberto Cifuentes

I'm sorry if this is too late, but hopefully will help others, here's my workaround to solve the file owner issue, first you have to find-out whats your uid (normaly 1000):

  • in the terminal type id. The result will be something like:
$ id
uid=1000(roberto) gid=1000(roberto) grou [...]
Enter fullscreen mode Exit fullscreen mode
  • execute the php service as your uid:gid in your docker-compose.yml
services:
  php:
    [...]
    user: '1000:1000'
    [...]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
fotonmoton profile image
Gregory

Also, you even don not need Dockerfile for php container!
If you run:

docker run --rm php:7.2-fpm-alpine php -m | grep -i pdo

Enter fullscreen mode Exit fullscreen mode

you can see that php modules already exists:

PDO
pdo_sqlite
Enter fullscreen mode Exit fullscreen mode
Collapse
 
fotonmoton profile image
Gregory

my bad, pdo_mysql do not exists on this image, unfortunately you should write Dockerfile if you want database access :)

Collapse
 
lalustine7 profile image
Lalustine • Edited

Thank you for your good tutorial.Can you help me in this issue? I tried everything but no use. I am running on window 10 but linux containers.

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = default and table_name = migrations)

thepracticaldev.s3.amazonaws.com/i...

Collapse
 
lideo profile image
lideo • Edited

I don't know if you were able to solve this or not, but this is what worked for me:

In your .env file change this

DB_HOST=localhost
Enter fullscreen mode Exit fullscreen mode

for this

DB_HOST=mysql
Enter fullscreen mode Exit fullscreen mode

I guess it's because you need to specify the name of the container running the database server and in docker-compose.yml the name for the mysql service is container_name: mysql.

Edit: I see now that the article clearly states that the value for DB_HOST needs to be mysql. For some reason my brain totally skipped that part...

Collapse
 
aschmelyun profile image
Andrew Schmelyun

In your .env file for your Laravel application, what are your values for the database credentials?

Collapse
 
tkoop profile image
Tim Koop

I think I've got this set up, but it's slow on my Windows machine. When using WSL 2, Microsoft recommends you "store your files in the WSL file system", whatever that means, for performance reasons. I've played around with the volumes in docker-compose.yml, but I can't get it working. How would I do this? Thanks!
docs.microsoft.com/en-us/windows/w...

Collapse
 
aschmelyun profile image
Andrew Schmelyun

Hey Tim!

So essentially when it says "Store your files in the WSL file system" it means in the Linux partition that Windows creates when using WSL.

If you open up a Windows Terminal session in Bash, by default it should put you in a directory called like /mnt/c/Windows. Run cd ~ to get to the home directory in the WSL file system, and then from there you can use any directory under that one to create your Docker-ized application and it will run way faster.

For instance, I have mine at ~/Sites/example.com. Let me know if that helps!

Collapse
 
tkoop profile image
Tim Koop • Edited

So in your docker-compose.yml file, what do your volumes look like? I changed mine from
volumes:
- .src:/var/www/html
to
volumes:
- ~/site:/var/www/html
And now I get a 404 error, even thought there is an index.html file there.

Thread Thread
 
tkoop profile image
Tim Koop

To answer my own question, the answer is to run docker-compose inside wsl, not from the Windows prompt.

Collapse
 
amax123 profile image
amax-123

Dont know what i'm doing wrong but i typed in the files by hand and built the docker. On running the containers mysql and php run fine but nginx exits with the following log: Any ideas?

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/02/01 11:26:42 [emerg] 1#1: invalid variable name in /etc/nginx/nginx.conf:32
nginx: [emerg] invalid variable name in /etc/nginx/nginx.conf:32
Enter fullscreen mode Exit fullscreen mode


`

Collapse
 
m_ghorbani94 profile image
Mohammad-GHorbani

Please add composer to php container.

Collapse
 
zokipokidev profile image
Zoran Panev

Adding composer container will be better solution :)

Collapse
 
mrzer0 profile image
Yan Naing (ရန်နိင်)

Hi Andrew,
Appreciate your tutorial. It is very good and clear to understand. I have tried both of your tutorial form youtube. They are ok.

But I am trying to install all php extensions recommended by laravel. BCMath, Ctype, Fileinfo, JSON, Mbstring, OpenSSL, PDO, Tokenizer, XML. Some of them can be install easily. But some aren't. I have faced various errors. Would you mind showing how to install them.

Collapse
 
shaqaruden profile image
Chad Gregory • Edited

OK i cloned your repo installed a laravel app and ran the docker-compose command from the github repo. When I try to run a composer command using the container I run into this error

dcr composer install
Creating training_composer_run ... done
Error response from daemon: unable to find user laravel: no matching entries in passwd file
Enter fullscreen mode Exit fullscreen mode
Collapse
 
shaqaruden profile image
Chad Gregory

had to build all the containers

Collapse
 
ossycodes profile image
Ossycodes

Hy Andrew, I have issues running this on windows please help
Creating mysql ... done
Creating php ... error

ERROR: for php Cannot create container for service php: b'Drive sharing failed for an unknown reason'

ERROR: for php Cannot create container for service php: b'Drive sharing failed for an unknown reason'
Encountered errors while bringing up the project.

Collapse
 
monneratrj profile image
Marcelo Monnerat Castello • Edited

You have to go on Settings and SHARE the driver you are trying to set up your project in it...

Collapse
 
fotonmoton profile image
Gregory

Thanks for good docker-compose.yml example!

But I'm curious about SERVICE_TAGS: dev and SERVICE_NAME: mysql env variables for mysql container, for what they are?

Collapse
 
shaneparsons profile image
Shane Parsons

Did you ever figure this out? I've noticed it outside of this tutorial as well, and am also curious.

Collapse
 
gbelot2003 profile image
Gerardo Belot • Edited

nice one, I like the nginx implementation so easy, but exactly how and when do I use "composer install"? I can't work without installing the packages!!

Collapse
 
isagarjadhav profile image
isagarjadhav

Adding composer to Dockerfile to make it container specific.

Install Composer

RUN curl -sS getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

You should have composer in php container so that you can run composer commands via docker cli and install specific laravel version. Laravel 8 requires composer 2.

Collapse
 
slither366 profile image
David Fernando Flores Lujan

Hello friends, I follow all the tips step by step but I had the next problem, please can you help me:

$ docker-compose build
The Compose file '.\docker-compose.yml' is invalid because:
services.nginx.networks contains an invalid type, it should be an array, or an object

Collapse
 
aschmelyun profile image
Andrew Schmelyun

Hey David!

Check your spacing on that file. It should be indented appropriately, and possibly even have to use spaces instead of tabs. If you need to, you can compare it against the current docker-compose.yml file in the repository for that tutorial: github.com/aschmelyun/docker-compo...

Collapse
 
lusekero profile image
Lusekero

I am new to docker and laravel but I like the clarity and simplicity of this project structure. Is there any chance you can show how to do this with apache server instead of nginx? (Laravel + Docker + Apache + MySQL) in this project structure?

Collapse
 
xavoski profile image
Xavoski

Hi,
Thanks for your post and git repo. Great work.
I am facing a chalenge to run npm commands in npm container.
I started an project with Laravel and Vue, but i can't run npm commands to compile Vue components.
I installed npm on my machine, runned "npm run watch" and it works great, but how can i run this command using the npm container?
After docker-compose up -d, all containers are done well, but only three remain running "nginx, mysql and php", that is ok, but how can i run and exec comands in npm?
Sory my verbosity and of course my english.

Collapse
 
xavoski profile image
Xavoski

Well, i start with my apologies.
I ran npm command using:
$ docker-compose run --rm npm run watch
But i will use this to ask if this (keep run watch open in terminal) is the best way to recompile Vue components?

Collapse
 
gayathr96194558 profile image
Gayathri

Hi Andrew,
Thanks for sharing this method. This is probably a concise guide on local development environment set up with Docker for running our Laravel applications. I also would like to suggest a post that will be helpful for readers on hiring Laravel developers: Read more.

Collapse
 
georstoy profile image
Stoyan Georgiev

Hi, Andrew!
From the positivity of the other comments, I understand that your guide works fine for others.
Yet I am getting this permission issue when I visit my localhost:

The stream or file "/var/www/storage/logs/laravel-2019-08-30.log" could not be opened: failed to open stream: Permission denied

thepracticaldev.s3.amazonaws.com/i...

I'm still learning and this got me confused.
Can you tell me what user:group and privileges to give to my project folder?

Collapse
 
francoislp profile image
François • Edited

Hello Stoyan,

Have a look at Laravel permissions related to /storage directory (which contains logs) and /bootstrap/cache directory.
You will find plenty of documentation/stack overflows issues about it. :)

Edit :
laravel.com/docs/6.x/installation#...

Collapse
 
stivncastillo profile image
Stiven Castillo

Hi, it don't work for me

nginx | 2019/10/02 22:02:42 [emerg] 1#1: invalid log level "access_log" in /etc/nginx/conf.d/default.conf:6
nginx | nginx: [emerg] invalid log level "access_log" in /etc/nginx/conf.d/default.conf:6

Collapse
 
devararishivian profile image
Devvara Rishivian

Hi Andrew, nice articles.
But I have question, how about deploy to live server?

Collapse
 
devellopah profile image
Islam Ibakaev

Hi, Andrew! I have this error in browser

This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500

Beautiful article, btw!

Collapse
 
sanjayojha profile image
Sanjay Ojha

Thank you for this article. I was thinking of changing my local development environment which is WAMP server installed on my Windows machine.

Collapse
 
epse profile image
Stef Pletinck

You can also use Laravel Homestead, which I've found to be incredibly useful

Collapse
 
marcellusgraceman profile image
marcellusGraceman

is it possible to add phpmyadmin..that way whenever you are running a container or a laravel project..you can use phpmyadmin to see data in database? if so, how to do it?

Thank you for your assistance.
Appreciate you

Collapse
 
marcellusgraceman profile image
marcellusGraceman

I just saw on your YouTube Channel that you did a video on it..thank you

Collapse
 
mzcoderhub profile image
Galang YP

Any tutorial about rest api laravel and mongodb using jwt? I just searching for that lol

Collapse
 
baicai profile image
baicai

I have similar php develop env with docker,
My demo is here demo-docker-compose

Collapse
 
lalustine7 profile image
Lalustine

Hello, thank you for the great tutorial. Can you help me with migration? i am getting error.

Collapse
 
ossycodes profile image
Ossycodes

same here, how did you fix this?

Collapse
 
deulizealand profile image
DeUliZeaLand

Hi, Thank you for the tutorial. It was super clear and easy to follow.

Keep make tutorial like this, Appreciate it a lot.

Collapse
 
yano profile image
Yano

You saved my day Andrew! Thank you for sharing your knowledge.

Collapse
 
iamndeleva profile image
iamndeleva

How do u link laravel files when using phpstorm

Collapse
 
aschmelyun profile image
Andrew Schmelyun

That's the best part about using this method, you don't have to "link" any files. Just edit them normally like you would any files on your computer, and any changes you make will be available inside the Docker container immediately.

Collapse
 
lasg125 profile image
Luis Alberto Sánchez

Thank you for sharing, it works perfect for my project!

Collapse
 
moonsn profile image
Moonsn

The MySQL data will be delete, maybe need set volume data as well as Nginx.

Collapse
 
aschmelyun profile image
Andrew Schmelyun

You're right, good suggestion! There's a few good methods I've come across in this thread: stackoverflow.com/questions/391751...

Collapse
 
codymoorhouse profile image
Cody Moorhouse

Just wanted to say thanks Andrew for this very detailed an thorough walk through, was amazing!

Collapse
 
ahkar02468 profile image
Ahkar02468

Hey Andrew, there are no folders with the name of container I created what do I miss. But php,mysql and ngnix are up and running with no error.

Collapse
 
urbinod profile image
Binod Manandhar

When I run docker-compose exec php php /var/www/artisan migrate
I get error as
Could not open input file: /var/www/artisan

Collapse
 
azmeer profile image
azmeer

Fantastic tutorial. It is very clear and thank you. I noticed Laravel Sail has made our life easier with Docker. Perhaps you could update this document (a new thread?) with Sail.