DEV Community

Deploying Your Laravel App on Docker, With NGINX and MySQL

Brian Baliach on August 07, 2019

The original post was also uploaded here: Laravel Application on Docker If you are a django developer, you can find a really comprehensive tutoria...
Collapse
 
sylviomigliorucci profile image
Sylvio Migliorucci • Edited

Hi Brian, i made all tutorial steps. i am not so good at docker yet, but a i get a error like this:

"Step 2/11 : COPY composer.lock composer.json /var/www/
ERROR: Service 'app' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder391229134/composer.lock: no such file or directory"

Can you help me?

Collapse
 
baliachbryan profile image
Brian Baliach

Hello there Sylvio. The error indicates that there is no composer.lock file
Please check your project's root folder and confirm that the file 'composer.lock' is there

Collapse
 
eyeseaevan profile image
eyeseaevan • Edited

What worked for me was removing the command in app.dockerfile that validates the composer-setup file hash starting with: "&& php -r "if(hash_file...", or changing it to the correct hash (see the entry under "4. RUN" in the article), and then executing the docker-compose up command from the root directory of your laravel app.

Collapse
 
palaciosdiego profile image
Diego Palacios

Hi Brian, it take me so long but finally i could make my app work, i only have a doubt, how i can reload changes in my code to the app container without rebuilding the whole thing. Thanks!

Collapse
 
baliachbryan profile image
Brian Baliach

Hello there Diego! Well, if you're on your development environment, you could set up a new volume in your docker-compose file that you can link to your project's root directory. That way, any changes you make to code will be reflected automatically in your docker container

Collapse
 
deulizealand profile image
DeUliZeaLand

Hi brian , can you give me some example to do that ?

Thread Thread
 
baliachbryan profile image
Brian Baliach

Hello there DeUliZeaLand, Highlighting from part of my post: When we were defining the Laravel App container inside our docker-compose file, we defined a volumes entry: ./storage:/var/www/storage whereby the left side before the colon represents our host machine and the right side after the colon represents our docker machine. So in this case, instead of just using the storage folder, you can adjust this entry to become: .:/var/www which will now link you entire project to your docker container. Again, only do this in your development environment, not your production environment.

Thread Thread
 
elerocks profile image
elerocks

Hello Brian,
can you elaborate why it isn't good idea to do it in production environment?

Thread Thread
 
baliachbryan profile image
Brian Baliach

Hello there, for starters, we have different configurations for our production and development environment. As such, we only want to persist the necessary files for production in our production environment. Because of this, we'd rather not create a volume that persists our entire project folder, but rather we want the volume to persist only our necessary files. That's why we have used commands like COPY so that we only copy necessary files that we have no problem discarding when re-creating our docker container and we've only persisted necessary data like the Storage folder which store user data.

Thread Thread
 
deulizealand profile image
DeUliZeaLand

Thanks for your explanation Brian. It's very great tutorial. Hope you make some other tutorial again docker

Collapse
 
luudanhnhan1102 profile image
Luudanhnhan1102

Hi Brian, can you help me to figure it out this error :

Step 11/11 : RUN php artisan optimize
---> Running in 3f514d7b9d8f
Command "optimize" is not defined.
ERROR: Service 'app' failed to build: The command '/bin/sh -c php artisan optimize' returned a non-zero code: 1

I have searched this bug on google but cant find an answer to this problems... I hope you will answer me...
Thank you so much...

Collapse
 
baliachbryan profile image
Brian Baliach

Hello there Ludan

As a temporary fix for your error, comment out that line (php artisan optimize) then run docker-compose up then try and running the command after your container has been deployed

Collapse
 
luudanhnhan1102 profile image
Luudanhnhan1102

thank you so much for this advice <3

Collapse
 
baliachbryan profile image
Brian Baliach

Hello there Patrick! I'm thankful for the appreciation! The reason why php artisan commands are run after is because php artisan needs a connection to the database, which is often not set up until the mysql container finishes building and is running. That's why we run these commands when the mysql container is up and running.

Collapse
 
castraea profile image
cAstraea

Having some issues with the session :/ Authentication is not working. Is there something should be aware about when using docker containers? The user data is okay but the redirect after login doesn't happen.

Collapse
 
janbalik profile image
Jose

Brian, you are the best! I have been looking for a complete guide to make my application work with a dockerized Nginx + Laravel + MySQL for a lot of time. I have tried many ones, but none of them really works. They probably were good, but I'm a total novice with Laravel + Docker and VPS. But, at least, I found this. Wow! It's so good! So perfectly explained even for me and completely functional. It has been the base to do my own modifications to adapt it to my needs and make everything work properly in my Droplet at Digital Ocean. You are my HERO!!! You saved me!!! Thanks, thanks, thanks a lot! I'm even thinking about making a Spanish version from your tutorial, of course referencing you, because it is my native language and I didn't find such a really good guide anywhere in Spanish. Please, keep sharing such good content!! I am really really grateful to you!! THANKS!!!!

Collapse
 
baliachbryan profile image
Brian Baliach

Hello there Edmond!
I'm glad you enjoyed the post:-)
Anyways, the docket container isn't running because of a certain mis-configuration

Could you run the following command and see if any errors are printed to your console:

docker run -a

Collapse
 
reykario profile image
Reynald Karisma

Nice Tutorial!

I have following your example for my production docker environment. However when i tried to create another database container for testing it cannot connect to the testing database. I tried to track the problem and it lead me to the environment variable in docker-compose.yml in app container. When i try to remove the enviroment variable the apps cannot connect to the database at all.

Can you explain how the environment variable working ? and can we made that value dynamic (consider my condition i required the apps to connected into different container database)

Collapse
 
tombombadilll profile image
Thomas Eriksson

Hi, great tutorial. Could you show how you add a domain name to this. I have tried a lot but didn't get it working. Eg. example.dev.com.

I have several sites and want to use a domain instead of localhost:8990

Thanks!

Collapse
 
baliachbryan profile image
Brian Baliach

Hello there Thomas!

If your multiple sites are on different domains, then you need make sure that your server supports cross-origin requests (since these requests are blocked by default by browsers). You can find a very comprehensive tutorial online.

Meanwhile,
Using our nginx server, go to the vhost.conf file we just created, and edit the server_name directive. Modify it to be your domain name and save. At this point, I assume that you have changed your A records and pointed your domain to your server's IP address. Leave the listen port to be 80 (and 443 if you have ssl). Save your configurations and re-create your docker container.

Collapse
 
bumasoft profile image
bumasoft

I got a composer error: [RuntimeException]

Could not scan for classes inside "app/Models" which does not appear to be a file nor a folder

I believe you have an error in the app.dockerfile. the "COPY . /var/www" line should be before the composer install line. Anyway, great intro to dockerizing a Laravel app, thanks!

Collapse
 
baliachbryan profile image
Brian Baliach • Edited

Thanks Bumasoft for pointing out this. I have actually copied the necessary composer files: composer.lock and composer.json before I ran composer install. Therefore the error being thrown might not be related to the order of COPY . /var/www. Could you provide more details about the error you have?

Collapse
 
shaneparsons profile image
Shane Parsons

Nice writeup!

Is there an ETA on the "next post"? I'm curious about what the "few minor changes" for production are?

Collapse
 
baliachbryan profile image
Brian Baliach

Thanks Shane Parsons! I'm not quite sure exactly about the next post, but I'll give you this: By end of October:-)

Collapse
 
youngestdj_ profile image
Samuel

Hi, could you write the tutorial on how to deploy to production please?

Collapse
 
kumarm1234 profile image
chithirakumar

I have domain on test.com
but this have nginx server and port is 8000 and something many ports...
so how i put on production server on my laravel application