Docker-local
Docker-local copied to clipboard
The docker-compose setup for local development
Docker-local
Repository for Docker setup files to run projects locally in containers.
Overview
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.
Pre-requisites
Make sure you have Docker installed. Follow the steps mentioned on the official site if you don't already have it.
Development Setup
- Pull the repo
- Open terminal (Git Bash / CMD) in the project directory
- Run
docker-compose up -d --build apache(--buildflag would be needed only when one of the dockerfiles indocker-compose.ymlhave 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 downwhich 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.
Running one-off tasks
- To use composer for any purpose (installation, removal etc), instead of running the usual
composercommand we will rundocker-compose run --rm composer. Example:docker-compose run --rm composer installto install project's dependencies. - To use npm for any purpose, instead of running the usual
npmcommand we will rundocker-compose run --rm npm. Example:docker-compose run --rm npm run buildwherebuildcommand is defined in project'spackage.jsonfile. - To use
php artisancommands 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.
Some gotchas on WSL2 (windows Subsystem for Linux)
Issue #1
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-datawhere fpm is the php-fpm container name. - Change the user ownership. Run
sudo chown -R 82 <dir_path>where82is the user id ofwww-dataobtained in step 1 &dir_pathwould 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_pathwould 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-datain step 2.
Issue #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)