I'm blown away. This is great stuff. The "aha!" moment for me was when the php container image didn't have the proper mysql data access functions and you just changed the image to one that had that installed and ready to go already. This changes deployment completely.
My friend. I want to thank you with all my heart for making this video. I am not sure if I will be using docker but you presented the best argument for using it then any video I have tried to watch. I appreciate you! Keep up the great content!
First , many thanks for this great work. it helped me A LOT ! my contirnution to all who are stuck with some connecticty issues from php to the mysql db. I used to use the IP adress (of the host ) in my php code to join the db. as many of you it was not working. Now i switch the servername in my php code from ip to docker alias. this alias is known from the apache part as all containers are in the same network. now it works and goodbye mysqli_connect errors !
Hey man im getting into cybersecurity and so im trying to learn how to create web applications so i have a further understanding of how to break them and your content man has been a blessing. This is a great in depth video and you provide awesome explanations. Def gonna recommend you to anyone who asks how i learned to create LAMP stacks.
Some notes: 1. This is for the v1 of docker Compose (EOL June 2023). The commands are slightly different in v2. Also the `docker-compose.yml` is suggested to be named `compose.yaml` 2. You can set a simple volume in mysql instead of entry point stuff, to persist the db data. The volume will store all the db data in a location outside the container.
Wonderful, it is the first time I got an idea about how could I transfer or migrate my code and data from an host to another and running the docker with all configs by just one command.
Hi, thx for the video, in the part you specify ports, is there any syntax that would let me redirect an incoming connection to a specific container based on the domain the user has used ? Similar to Apache's domain based virtual hosts but instead of having apache using a specific /html path depending on the domain used, Docker would redirect the user to the right container instead.
After watching like 15 tutorials how to run lamp stack using docker, this is the first time i get it, very simply explained thank you so much! One little question, if I want to use composer - should i run it in container? I thought its the matter of local system, but composer images on docker hub dissapointed me. Have a great day!
@@codeadamca Sorry for my annoying questions, I still can't get composer woking, I have composer in my windows PATH so I can initialize it locally, but what if I want composer to be installed with all dependecies that my project needs (based on composer.json) when someone pulls my project from GitHub repository, i think vendor folder should'nt be commited to github due to its size. I would appreciate your advice sir, have a great day!
I have a question. How did phpmyadmin was able to connect to mysql even before you setup the network between them? Could you please clarify if possible?
Excellent tutorial. I have been playing around with docker and wondered what it would take to have self-hosted environment through there as opposed to mamp pro. so this was helpful to see the initial setup. But I am confused about dumping the db, when you mention restarting the service it db would disappear (thus needing to dump the db). Is that correct for in unexpected scenarios such as a power outage? Once everything boots back up all content not in the dumped database would be gone?
Hello Adam! Thank you for this tutorial. I used your Github repo, ran it without any edits and I get this error (as others have reported): Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 3 Apparently, the php image ( image: php:8.1.1-apache) from Dockerhub does not have mysqli enabled for PHP. Can you please advise us on the proper way to enable it?
I had the same problem and I solved it this way: Firstly I created folder (in my case I named it "php_custom"), then inside it I made Dockerfile with following content: FROM php:8.1.1-apache RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli Next, in docker-compose.yml below "www" I added "build" prop with value linked to folder with Dockerfile, like this: www: depends_on: - db image: php:8.1.1-apache build: ./php_custom volumes: - "./:/var/www/html" ports: - 80:80 - 443:443 networks: - lamp-docker This should work with "--build" flag in "docker-compose up" command. The flag is needed only once unless you want to rebuild docker again
Hi Sir How about if the PHP file is a form and dynamically use to key in data into db.? Is data will be gone too after down the container? How to make data persistent?
Why would you architect anlamp server with your db inside of yhe container rather than outside - with this method you're using writing start data back into the db every restart? It seems like simply storing the actual db file outside the docker container would be better.
Thank you for the video and excellent teaching but I am having a problem trying to retain the new records in the db. the new records don't show when I restart the containers. Do I have to export the dump.sql everytime I shut down ?
Hello, thank you for the video. After I changed image value of www to php:8.1.1-apache and execute docker-compose up, I get the same error "connection refused". Any recommendations?
I would need to see your docker-compose.yml to know for sure. You can view mine from here: github.com/codeadamca/php-docker Try using my file and see if that fixes it. If it does, look for the type in yours.
This really helped a lot, thanks Adam. I am very new to Docker (having always used XAMPP) for local PHP dev. The only issue I have is when trying to use .htaccess overwrites. Not sure if there is a way to set apache config to allow .htaccess using some env variable in the docker-compose file?
Here is a good link to adding custom configuration to your apache setup: www.cloudreach.com/en/technical-blog/containerize-this-how-to-use-php-apache-mysql-within-docker-containers/
Not working the same way for me. still receiving the error: atal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 3
2 ปีที่แล้ว +2
I tried connecting to the database in three different ways: new mysqli, mysqli_connect and new PDO, and all ways didn't work for me. It's very frustrating. 🤥🤥
I tried to migrate my existing Laravel application that used xamp to docker. How do I run the migrations that I had in my application to the new lam_docker db ? if that makes sense ?
When I run 'php artisan migrate' , in the terminal, I get this error: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = lamp_docker and table_name = migrations and table_type = 'BASE TABLE')
It's a hard one to debug when I can't see it. But it sounds like the PHP server is not able to connect to the MySQ server. In this video we use separate servers for PHP, MySQ
Thank you for this video. I had the same problem the mysqli_connect error disappeared not. My solution is a combination between your video and the video of arthur man Part 8
I would have to see your docker-compose.yml to know for sure. But I would guess you need to make sure your mysql:latest image (named db) is referenced from your phpmyadmin/phpmyadmin image properly (depends_on: - db and - PMA_HOST=db). If you want, a copy of my files are available here: github.com/codeadamca/php-docker
Make sure you are using the service name from your docker-compose.yml file as the host in your PHP MySQL connection. In my example, the host should be "db". And make sure the username, password, and database name in your PHP MySQL connection are the same as your MySQL environment variables defined in the docker-compose.yml file. In my example they are lamp_demo, password, and lamp_demo. Hope that helps!
Also make sure the environment variables in the MySQL portion of the docker-compose.yml match the PHPMyAdmin environment variables, also in the docker-compose.yml file. You could also try using some different ports in case your computer is already using one of the specified ports.
Finally checked some other video and it magically worked, now I'm wondering how to configure apache so the server goes to the public folder where is the entry point index.php I've tried using volumes for that, but failed because when making PROJECT_ROOT constant - my project name is replaced by var/www/html Would you have any suggestions sir?
I believe I found the same question on Stack Overflow, maybe this will help: stackoverflow.com/questions/51393494/how-to-change-the-document-root-in-php7-1-apache-from-docker-compose-yml
@@codeadamca Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line
Shouldn't it be setup so you don't have to dump from MySQL, but rather it does it by default. Sure would suck to shutdown and lose all the data. I'm told that doing it the way you are means it is not setup correctly, and you could lose valuable data because of it.
Excellent, just excellent! I kept getting this error at localhost:80 even after changing the php image: Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 3 I'm running it on ubuntu 20.04, and after a whole lotta googling, changing to this image instead fixed my problem: thecodingmachine/php:8.1.1-v4-apache
There are many different way to connect to a database and execute a query. You can use a series of mysql functions (mostly deprecated), mysqli functions (i stands for improved), or PDO. Here is a good page describing the difference and the syntax for each: www.w3schools.com/php/php_mysql_connect.asp
I finally got it working thanks to another tutorial. In the root of the project, add file "php.Dockerfile" with this content: FROM php:8.1.10-apache RUN docker-php-ext-install mysqli pdo pdo_mysql In docker-compose.yml replace "image: php:8.1.10-apache" with build: dockerfile: php.Dockerfile context: . This install mysqli when running the container.
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 3 ?
Finding it hard getting my version of docker after installation. I use the docker version and docker info, I only see the 19.xx.xx and 20.xx.xx, nothing related to 2, or 3 or 4 anywhere, Any ideas the command you used to obtain the version: 3 you used in the video.? Thanks in anticipation.
The version in the docker-compose.yml is the schema version, not the version of the Docker program. You should be safe using version 3. It's kind of comparable to using VSCode and PHP. VSCode is at version 1.69.1. But I'm using it to write PHP 8. The are unrelated. For more info check out: docs.docker.com/get-started/08_using_compose/
Thanks for leaving those errors in the video, it's the biggest struggle I have with docker is finding solutions to little errors like that.
I'm blown away. This is great stuff. The "aha!" moment for me was when the php container image didn't have the proper mysql data access functions and you just changed the image to one that had that installed and ready to go already. This changes deployment completely.
Watching the troubleshooting process is so helpful, thanks for the extra effort!
Very good pace, good tone and a true step-by-step guide
My friend. I want to thank you with all my heart for making this video. I am not sure if I will be using docker but you presented the best argument for using it then any video I have tried to watch. I appreciate you! Keep up the great content!
Best thing is, he never cut the video, if there is a bug the he shows it. This is what we mostly face in real life
I really appreciate your thoroughness on this tutorial. You are a great teacher Adam!
Brilliant! Video paints a thousand words to actually see the real life process flow. VERY very cool docker stuff & thanks for sharing.
Thanks!!
First , many thanks for this great work. it helped me A LOT ! my contirnution to all who are stuck with some connecticty issues from php to the mysql db. I used to use the IP adress (of the host ) in my php code to join the db. as many of you it was not working. Now i switch the servername in my php code from ip to docker alias. this alias is known from the apache part as all containers are in the same network. now it works and goodbye mysqli_connect errors !
this help me a lot understand each thing inside of docker compose, thank u very much, now i can create my own docker compose for my projects :)
Hey man im getting into cybersecurity and so im trying to learn how to create web applications so i have a further understanding of how to break them and your content man has been a blessing. This is a great in depth video and you provide awesome explanations. Def gonna recommend you to anyone who asks how i learned to create LAMP stacks.
Great! Thanks!
awesome video man! really helped me with my first time with docker :)
Glad I could help!
Some notes:
1. This is for the v1 of docker Compose (EOL June 2023).
The commands are slightly different in v2. Also the `docker-compose.yml` is suggested to be named `compose.yaml`
2. You can set a simple volume in mysql instead of entry point stuff, to persist the db data.
The volume will store all the db data in a location outside the container.
Thanks, that's probably why my mailcow didn't work as well. Guess it's time for a newer video
Thank you so much for the video. Really helped me get started with Docker.
This was a big help, thanks!
thanks, so patient and clear!
excelent video man, help me a lot!
Great video and instructions! Thank you for making this.
You are so welcome!
Great video, very nice to follow
Would have been nice to see you do the Dockerfile version of the fix, even though you had an image ready to go :)
Wonderful, it is the first time I got an idea about how could I transfer or migrate my code and data from an host to another and running the docker with all configs by just one command.
Great! Docker is awesome!
amazing explanation!
Hi, thx for the video, in the part you specify ports, is there any syntax that would let me redirect an incoming connection to a specific container based on the domain the user has used ? Similar to Apache's domain based virtual hosts but instead of having apache using a specific /html path depending on the domain used, Docker would redirect the user to the right container instead.
After watching like 15 tutorials how to run lamp stack using docker, this is the first time i get it, very simply explained thank you so much!
One little question, if I want to use composer - should i run it in container? I thought its the matter of local system, but composer images on docker hub dissapointed me.
Have a great day!
As long as you are running composer on a connected volume, Docker should sync the files.
@@codeadamca Sorry for my annoying questions, I still can't get composer woking, I have composer in my windows PATH so I can initialize it locally, but what if I want composer to be installed with all dependecies that my project needs (based on composer.json) when someone pulls my project from GitHub repository, i think vendor folder should'nt be commited to github due to its size.
I would appreciate your advice sir, have a great day!
I have a question. How did phpmyadmin was able to connect to mysql even before you setup the network between them? Could you please clarify if possible?
Great content.
Excellent tutorial. I have been playing around with docker and wondered what it would take to have self-hosted environment through there as opposed to mamp pro. so this was helpful to see the initial setup. But I am confused about dumping the db, when you mention restarting the service it db would disappear (thus needing to dump the db). Is that correct for in unexpected scenarios such as a power outage? Once everything boots back up all content not in the dumped database would be gone?
Hello Adam! Thank you for this tutorial.
I used your Github repo, ran it without any edits and I get this error (as others have reported):
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 3
Apparently, the php image ( image: php:8.1.1-apache) from Dockerhub does not have mysqli enabled for PHP. Can you please advise us on the proper way to enable it?
I had the same problem and I solved it this way:
Firstly I created folder (in my case I named it "php_custom"), then inside it I made Dockerfile with following content:
FROM php:8.1.1-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
Next, in docker-compose.yml below "www" I added "build" prop with value linked to folder with Dockerfile, like this:
www:
depends_on:
- db
image: php:8.1.1-apache
build: ./php_custom
volumes:
- "./:/var/www/html"
ports:
- 80:80
- 443:443
networks:
- lamp-docker
This should work with "--build" flag in "docker-compose up" command. The flag is needed only once unless you want to rebuild docker again
Hi Sir
How about if the PHP file is a form and dynamically use to key in data into db.? Is data will be gone too after down the container? How to make data persistent?
Is there a way to do it in a single container instead of multiple pls and thanks
Thank you!
I had a sqlyog and mysql workbench how can i connect to the database using that software? Thank you
How does docker populate the database with data? Where is that data saved? and why does docker miss the data if you don't define volume?😅
I s there such a video with Nginx, fpm-php, sql ...?
How to php.ini file update in the local machine and how it is mount
You may also create a Tutorial with all the best practices for a LAMP stack production azure k8s cluster, including CI/CD pipe line from Docker..
I don't even know what half those terms mean!! :)
Why would you architect anlamp server with your db inside of yhe container rather than outside - with this method you're using writing start data back into the db every restart? It seems like simply storing the actual db file outside the docker container would be better.
Very Good Adam.👍 Wondering as per best practices, we won't allow empty passwords to connect mysql
Yes, good advice!
Thank you for the video and excellent teaching but I am having a problem trying to retain the new records in the db. the new records don't show when I restart the containers. Do I have to export the dump.sql everytime I shut down ?
Each time you fire up the server, you will need to run the migrations script.
Hello, thank you for the video. After I changed image value of www to php:8.1.1-apache and execute docker-compose up, I get the same error "connection refused". Any recommendations?
I would need to see your docker-compose.yml to know for sure. You can view mine from here:
github.com/codeadamca/php-docker
Try using my file and see if that fixes it. If it does, look for the type in yours.
This really helped a lot, thanks Adam. I am very new to Docker (having always used XAMPP) for local PHP dev. The only issue I have is when trying to use .htaccess overwrites. Not sure if there is a way to set apache config to allow .htaccess using some env variable in the docker-compose file?
Here is a good link to adding custom configuration to your apache setup:
www.cloudreach.com/en/technical-blog/containerize-this-how-to-use-php-apache-mysql-within-docker-containers/
Not working the same way for me. still receiving the error: atal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 3
I tried connecting to the database in three different ways: new mysqli, mysqli_connect and new PDO, and all ways didn't work for me. It's very frustrating. 🤥🤥
Same for me.
A few others have had a similar issue. But I can't duplicate it.
I have also same problem and tried all of them everytime it throw an exception , could find drive for pdo or mysqli. Did anyone find a soluyion?
thank you...
I tried to migrate my existing Laravel application that used xamp to docker. How do I run the migrations that I had in my application to the new lam_docker db ? if that makes sense ?
When I run 'php artisan migrate' , in the terminal, I get this error: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = lamp_docker and table_name = migrations and table_type = 'BASE TABLE')
It's a hard one to debug when I can't see it. But it sounds like the PHP server is not able to connect to the MySQ server. In this video we use separate servers for PHP, MySQ
Thank you for this video. I had the same problem the mysqli_connect error disappeared not. My solution is a combination between your video and the video of arthur man Part 8
If somebody is interested in my Docker File please write. 😉
@@berndseifert6966 Hello, I'm trying to solve it, I like the base of this tutorial, but I can't find the error,
could you show me your docker-compose?
Thanks for adding this! Can you add a link to this video? I think others have had the same issue.
Hello, when creating the database i'm getting an error saying no privileges!
I would have to see your docker-compose.yml to know for sure. But I would guess you need to make sure your mysql:latest image (named db) is referenced from your phpmyadmin/phpmyadmin image properly (depends_on: - db and - PMA_HOST=db). If you want, a copy of my files are available here:
github.com/codeadamca/php-docker
From my side still mysqli_connect is undefined where my image is this: image: php:8.1.1-apache
same
You may have to enable php_mysqli. Check out this StackOverflow question:
stackoverflow.com/questions/46879196/mysqli-not-found-dockerized-php
Sir, would you please help me, phpmyadmin throwing connection refused every time i provide username and password
Make sure you are using the service name from your docker-compose.yml file as the host in your PHP MySQL connection. In my example, the host should be "db".
And make sure the username, password, and database name in your PHP MySQL connection are the same as your MySQL environment variables defined in the docker-compose.yml file. In my example they are lamp_demo, password, and lamp_demo.
Hope that helps!
Also make sure the environment variables in the MySQL portion of the docker-compose.yml match the PHPMyAdmin environment variables, also in the docker-compose.yml file. You could also try using some different ports in case your computer is already using one of the specified ports.
Finally checked some other video and it magically worked, now I'm wondering how to configure apache so the server goes to the public folder where is the entry point index.php
I've tried using volumes for that, but failed because when making PROJECT_ROOT constant - my project name is replaced by var/www/html
Would you have any suggestions sir?
I believe I found the same question on Stack Overflow, maybe this will help:
stackoverflow.com/questions/51393494/how-to-change-the-document-root-in-php7-1-apache-from-docker-compose-yml
@@codeadamca Thanks!!
it's getting to my nerves plz help me out with this error
What is the error you are receiving?
@@codeadamca Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line
@@codeadamca i am running the same application from your git hub account it giving me error
Amazing video, but 720p in 2022? Squinted the whole video to read the text.
Shouldn't it be setup so you don't have to dump from MySQL, but rather it does it by default. Sure would suck to shutdown and lose all the data. I'm told that doing it the way you are means it is not setup correctly, and you could lose valuable data because of it.
This is a very long video.
Sorry... It is right from scratch! :)
Excellent, just excellent!
I kept getting this error at localhost:80 even after changing the php image:
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 3
I'm running it on ubuntu 20.04, and after a whole lotta googling, changing to this image instead fixed my problem: thecodingmachine/php:8.1.1-v4-apache
There are many different way to connect to a database and execute a query. You can use a series of mysql functions (mostly deprecated), mysqli functions (i stands for improved), or PDO.
Here is a good page describing the difference and the syntax for each:
www.w3schools.com/php/php_mysql_connect.asp
Good job!! Thanks
@@codeadamca I tried connecting in three different ways: new mysqli, mysqli_connect and new PDO, and all ways didn't work for me.
I finally got it working thanks to another tutorial. In the root of the project, add file "php.Dockerfile" with this content:
FROM php:8.1.10-apache
RUN docker-php-ext-install mysqli pdo pdo_mysql
In docker-compose.yml replace "image: php:8.1.10-apache" with
build:
dockerfile: php.Dockerfile
context: .
This install mysqli when running the container.
thanks bro, it works! got the same error as above back then.
Thank you! Helped me a ton :)
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:3 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 3
?
Check out my fix I just posted above.
Finding it hard getting my version of docker after installation. I use the docker version and docker info, I only see the 19.xx.xx and 20.xx.xx, nothing related to 2, or 3 or 4 anywhere, Any ideas the command you used to obtain the version: 3 you used in the video.?
Thanks in anticipation.
The version in the docker-compose.yml is the schema version, not the version of the Docker program. You should be safe using version 3.
It's kind of comparable to using VSCode and PHP. VSCode is at version 1.69.1. But I'm using it to write PHP 8. The are unrelated.
For more info check out:
docs.docker.com/get-started/08_using_compose/
@@codeadamca Sorry, I'm using Ubuntu, VSCode and I'm trying to setup php with LAMP.
@@codeadamca oh I get it. Thanks
Adam please - $connect = mysqli_connect - in 2023?
I tend to use the mysqli series of functions when teaching PHP as they are easier for new programmers to grasp.