Repository for Docker setup files to run projects locally in containers.
This project provides a boilerplate structure and dockerfiles for essential services such as apache
, php
and redis
for running projects locally.
To run any project in Docker, just add that project in www
directory and add vhost for same in apache/httpd-vhosts.conf
file.
Make sure you have Docker installed. Follow the steps mentioned on the official site if you don't already have it.
- Pull the repo
- Open terminal (Git Bash / CMD) in the project directory
- Run
docker-compose up -d --build apache
(--build
flag would be needed only when one of the dockerfiles indocker-compose.yml
have been updated). - Once that is completed run
docker ps
. It should show you the apache, fpm & redis containers running. - (Optional) Run any one-off command to build/install project dependencies if required. Refer this section for more details.
- Access the project in browser based on vhost setup in apache.
- Once done with development, you can run
docker-compose down
which will stop the running containers of your project
Note that in one of the above steps you might get a notification asking for permission whether your project directory can be shared with docker environment which you should allow. Since we are sharing the code directory to the docker container as a volume to prevent rebuilding of image everytime a change happens.
Also note that all the specified docker commands are to be run from the project directory where the docker-compose.yml file is present, since it has all the configuration logic.
- To use composer for any purpose (installation, removal etc), instead of running the usual
composer
command we will rundocker-compose run --rm composer
. Example:docker-compose run --rm composer install
to install project's dependencies. - To use npm for any purpose, instead of running the usual
npm
command we will rundocker-compose run --rm npm
. Example:docker-compose run --rm npm run build
wherebuild
command is defined in project'spackage.json
file. - To use
php artisan
commands use the followingdocker-compose run --rm artisan
. Example: To put the app in maintenance mode in laravel, we will rundocker-compose run --rm artisan down
.
Since our project is containerized, the above commands will spawn a container for npm / composer / php artisan, execute the required command on your codebase/working-directory and then remove that container, so we do not need any of the above dependencies to be installed on our system.
There might be some file permission error in php on WSL2/linux for file writes. Follow below steps to fix.
- Find the user id of php-fpm. Run
docker exec -it fpm id -u www-data
where fpm is the php-fpm container name. - Change the user ownership. Run
sudo chown -R 82 <dir_path>
where82
is the user id ofwww-data
obtained in step 1 &dir_path
would be the path of the folder where php needs to write files. - Set read/write/execute permissions for group. Run
sudo chmod g+rwx <dir_path>
wheredir_path
would be the path used in step 2. This step is needed if you want to edit/delete files indir_path
, because we changed the user ownership to another userwww-data
in step 2.
Cannot use any IDE/Editor for coding unless it has support for WSL2. Try below Editors/IDEs.
- Use VSCode with Remote-WSL extension (free)
- Use PhpStorm v2020.1.2 which has support for WSL2 (paid)