Contents
Contents
Intro
The set-up
Webserver image for Laravel
docker-compose.yml
Up and Running
Helper scripts (optional)
container...
For further actions, you may consider blocking this person and/or reporting abuse
Awesome article, thanks for it!
I thought that integrating also phpmyadmin would be very useful, so I added to my docker-compose.yaml this little snippet
That's it, now the useful phpmyadmin buddy joins the squad :)
This can be improved adding PMA_HOST and PMA_PORT to the .env file for complete customization
In order to help other users, i’ll share here some troubles i was facing while running
docker-compose build
.oniguruma
foundHow to solve: Add
libonig-dev
package to apt-get's installationlibzip
foundHow to solve: Add
libzip-dev
package to apt-get's installationHope it was helpful to someone! 😄
can you please provide me docker file and docker compose related full source code download link ? because i tried last 2 days but not getting success with docker file and docker compose to setup any php project.
Are you setting up a new Laravel project or trying to create docker environment for existing one?
If you are setting up new Laravel project, you can refer to my repo:
github.com/veevidify/laravel-apach...
and then do:
HI there, i am setting an new project with the latest laravel repo + your dockerfile and docker-compose.yml i get this error:
Step 13/14 : RUN useradd -G www-data,root -u $uid -d /home/devuser devuser
---> Running in e24b11b14717
useradd: invalid user ID '-d'
ERROR: Service 'laravel-app' failed to build: The command '/bin/sh -c useradd -G www-data,root -u $uid -d /home/devuser devuser' returned a non-zero code: 3
any solution ?
If you refer back to
docker-compose.yml
, this part:docker-compose
requiresUID
environment variable to be set. Check your.env
file, make sureIf you're unsure about host user id, type
id
in your terminal to double check.That worked fine thank you. but i have another question, if you permit, regarding your Dockerfile. how to copy npm from nodejs docker image like you did with composer.
the reason i ask this is because when i run "php artisan ui vue --auth" i get this message:
Vue scaffolding installed successfully.
Please run "npm install && npm run dev" to compile your fresh scaffolding.
Authentication scaffolding generated successfully.
There is a neat little trick to run a docker container as if it's a binary in your host. This is very useful for things like
composer
ornpm
which only modifies files statically. Example:Make sure all the parameters are what you need, e.g. uid, node version, etc.
Even though I copy
composer
binary into the my own app image for "encapsulation", if we intend to only ever use such binary for static files modification on the host, that wouldn't be necessary. Instead this trick makes it more clean once you start having 5 6 container runtime for these sorts of purposes.I conveniently "missed out" this part due to how varied people's use cases with
node
are.Thank you so much to reply me , i will check it and let you know if i am facing another issue .
Thank you again ..... !
Hello Sir, I facing 500 server error issue , can you please give me some tips. what happens over there ?
Trying to learn laravel and docker.
I cloned your repo and ran
docker-compose build
docker-compose up -d
docker-compose logs -f
docker ps gives me two containers. If I type localhost:8000 I see that the webserver is running but I get a Forbidden
You don't have permission to access this resource.
Apache/2.4.38 (Debian) Server at localhost Port 8000
error.
The log file gives : [authz_core:error] [pid 16] [client 172.19.0.1:43254] AH01630: client denied by server configuration: /var/www/html/public
and also
AH00112: Warning: DocumentRoot [/var/www/html/public] does not exist
I.m running on Windows10, under WSL.
Can you help this one?
Marcus
Hello - thank you for the great article. I am very very close to getting this working with an EXISTING Laravel 7 / PHP 7.4 project. But I am having a little difficulty ...
Everything works fine, I can run php artisan commands within the container, login to MySQL container and see tables, etc. But when I try to run in the browser with localhost:8000/ I get the following error: Exception -- The /var/www/html/bootstrap/cache directory must be present and writable.
Looking at the folder within the Docker container's Exec ...
ls bootstrap/cache -al
total 12
drwxrwxr-x 2 root nogroup 4096 Sep 27 16:23 .
drwxrwxr-x 3 root nogroup 4096 Mar 30 2018 ..
-rwxrwxr-- 1 root nogroup 14 Mar 30 2018 .gitignore
The same folder on my local machine is:
total 12
drwxrwxr-x 2 mark www-data 4096 Sep 27 11:23 .
drwxrwxr-x 3 mark www-data 4096 Mar 30 2018 ..
-rwxrwxr-- 1 mark www-data 14 Mar 30 2018 .gitignore
the folder is there and writeable ?? I am sure this is a permissions thing but unsure how to correct it.
Any idea how to possibly fix this?
tia
_mark
By the way, if doing this on your local machine, make sure you
change docker-composer.yml to use bind - otherwise when you run the command .container, no files will be listed inside!
volumes:
#- ./:/var/www/html
- type: bind
source: ./
target: /var/www/html
Thanks for this writting :)
I'm setting up a complete automated deployment of a laravel app using docker and I found that Laravel uses .env files instead of docker-compose.yml enviroment section.
Is there a way to make laravel to use docker-compose.yml environment section?
Thanks!
Right, so what
.env
essentially does is that it emulates Laravel reading the actual runtime environment variables. In other words Laravel treats.env
variables as if they're your actual host or build context's environment variables. Conveniently for us,docker-compose
is also able to read from.env
.So to put it clearly, both Laravel and docker read these variables in a build context, which are mocked by
.env
file for development purposes.In your deployment / build context, you would want to export these variables too. To name a few, Gitlab CI allows configuring them under Settings > CI/CD. Circle CI does also, under Build Settings > Environment Variables. If you only deploy from local (instead of remote CI), simply run deployment from within a container built with
.env
variables exported.Hello sir, there's any different configuration between setting up a new Laravel project or trying to create docker environment for existing one?
The difference lies in post containers startup.
So for existing project, we will create all the necessary docker files, such as Dockerfile, docker-compose, place all the configs in place.
Since we're volume mounting the app, depends on what your current setup is missing. If you havent run composer, and there is no vendor folder, you can go ahead and use the container to run composer install. If there is no app key, or passport keys in your setup, you will run respective artisan command to setup.
Similarly, since this setup aims at a new database container, you will start with an empty database (we mount it under var/ inside the project folder). If you have any migrations and seeders, you will need to run those to get a database setup. Alternatively if you have developed a dev database without those migrations and seeders, you can initiate and fill the database with a sql file mounted under run/dump/init.sql as I explained in the post.
Thank you for your response, I'm newbie in this scope. I have a Laravel application also have migrations and seeders for this application. I've done create necessary docker files and place the configs in place. But there's some problem at migration:
did I missed something?
that probably mean your database username and password are not set to what the docker container is built with.
Make sure your environment variables for the mysql service (specified in docker-compose.yml) are consistent with the process you are using to access the database (whatever you are trying that yields that error).
when execute the code php artisan migrate print a error
Database name seems incorrect: You're using the default database name
laravel
. This database does not exist.Great post, I found this very easy to set up and work's like a charm, much quicker than setting up on a vm.
Thank you for this amazing post.
I'm trying to dockerize an existing laravel application using your post. But my static files are returning 404.
stackoverflow.com/questions/626231...
can you solve this error on composer install?
devuser@b37b652e508b:/var/www/html$ composer install
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Package operations: 101 installs, 0 updates, 0 removals
[RuntimeException]
/var/www/html/vendor does not exist and could not be created.
Hello Sir, I facing 500 server error issue , can you please give me some tips. what happens over there ?
The fact that you get a response from the webserver means the docker environment works.
If you get
require_once ... vendor/autoload
error, then you'd probably missing composer dependencies.gets you inside the container.
then
okay i will check again .
Thank you .
RUN composer install:
0 0.554 Composer plugins have been disabled for safety in this non-interactive session. Set COMPOSER_ALLOW_SUPERUSER=1 if you want to allow plugins to run as root/super user.
0 0.554 Do not run Composer as root/super user! See getcomposer.org/root for details
0 0.559 Composer could not find a composer.json file in /var/www/html
0 0.559 To initialize a project, please create a composer.json file. See getcomposer.org/basic-usage
failed to solve: executor failed running [/bin/sh -c composer install]: exit code: 1
PS C:\xampp\htdocs\portal>
Hi there, the containers are running, but it seems that the volume mapping never worked. When I look in the container, no files are listed except the run folder.
git clone git@git.repo.url/laravel-project
fatal: repository 'git@git.repo.url/laravel-project' does not exist
why repository is not exist ????
This is just an example of how you set up YOUR OWN project, so "laravel-project" is just a fake example name. You can read it as :
git clone git@git.repo.url/MY-OWN-PROJECT-CREATED-AFTER-READING-THIS-ARTICLE
Awesome, thanks for it!
It helped me a lot
If I want to add SSL what is the way to do this?
I have this error when I login: GET localhost:8000/js/app.js net::ERR_ABORTED 404 (Not Found).
I need help, I could not solve it.
Hello,
Amazing Tut. thanks,
I didn't get the last point regarding Helpers scripts, Where can I write the bash script like this
docker exec -it laravel-app bash -c "sudo -u devuser /bin/bash"
Those are quite literally bash scripts (if your development is linux based) which wraps around your host's
docker
binary, to run commands inside a running container.You can have these scripts anywhere. I included them within the project folder itself to share with others working on the repo. Checkout the "TL;DR" section, and my repo on github: github.com/veevidify/laravel-apach...
My laravel application is working on the localhost. But while running through localhost:port , I get error of 403.
This is the error:
Forbidden
You don't have permission to access this resource.
Hello,
Is it possible to run the composer update and migrations directly from the Dockerfile?
I tried to
But this is not working
Hi,
The setup I had on the repo use volume mount for the source, so that any changes, including
composer install
(vendor
folder) persist on the host, for ease of development.If you simply want a running app with vendor packages inside the container, you need to add
composer.json
into the container during build stage. Refer to this part of the article:Note that by doing this, your host pc won't have the
vendor
packages persisted.