How to start your PHP, MySQL, Nginx based web development in Docker?
For my PHP, MySQL, Nginx, Linux, based web app development, I used to use Vagrant. Vagrant was much easier than a VirtualBox in terms of performance and reusability. But the tech industry is moving toward Docker, so did I move.
UPDATE: Please use this new repo and Follow the new article linked bellow.
New Repository: Laravel Docker App
Please follow this article instead: A Complete Docker based development environment for Laravel & PHP
DEPRICATED.
THIS ARTICLE AND THE LINKED GITHUB REPO BELLOW IS OBSOLETE NOW IN FAVOUR OF THE NEW REPOSITORY MENTIONED ABOVE.
I started with a famous Docker image Laradock
After planning to move my development environment from Vagrant to Docker, for many months I used Laradock for my personal practice and development.
Though the name suggests Docker for Laravel, it had a lot of images in it and through configuration, a lot of work and any PHP based development can be done with Laradock.
But if you don’t need advanced features like Elasticsearch, Jenkins, Kibana, MongoDB, Postgres, etc, then Laradock is an overkill.
Decided to build my own Docker for LEMP Stack Development
Just recently I decided to build my own Docker Image, which shall be
- light-weight from Laradock and
- obviously very simple to use.
- Must have all the features for modern web app development
- Most of all, this Must support Laravel and WordPress
So I created one, named it LEMP Dock and shared it in Github.com as an Open-source project.
Features of LEMP Dock:
LEMP Dock is composed of multiple Docker Images with a docker-compose.yml
file. The Docker images used are:
- mysql:5.7
- nginx:alpine
- php:7.2-fpm-stretch
- redis
In the php:7.2-fpm-stretch
image, composer
, git
and nodejs
and npm
is added additionally.
Volumes feature of Docker is used to share data between the host computer (your laptop/desktop) and the container.
For MySQL and Redis data, we have a data/mysql
and data/redis
directory which is mapped with the respective container.
nginx:alpine
image also mapped multiple files and directories with the container to control the nginx configuration and enabling ability to host multiple sites in the nginx via a .conf
file.
Files and folders are hosted in images/nginx/
directory. This is the code sample.
Where to put my source code for this Docker image?
A directory named www
is given to put your source code in there. Put your source code there. All directories in the www
will be available in your containers /var/www
directory.
In the next section, I’ll discuss how to use this LEMP Dock and add a new site here.
How to use LEMP Dock:
Use of LEMP Dock is very easy. I mean, I build this docker image for easy use.
Prerequisite: To run docker in your laptop or PC running any os (Windows, Mac, Linux), need a Docker Engine installed. You can Download Docker from this link and install. This is a very simple process.
Here are the steps to use LEMP Dock for your development purpose.
- Clone the repo to your development machine
git clone git@github.com:arifulhb/lempdock.git
2. Go to thelempdoc
directory and build the container
cd lempdock
lemp/build
lemp/run
Now your docker is already running.
You can execute docker ps
to see all the running containers.
Execute command docker image ls
to see all downloaded docker images.
Now you can access the site in your browser http://localhost
This will load the /var/www/html/index.php
file.
4. How to ssh to the container?
If you want to use the composer
inside of the container, you’ll need to ssh
to the container. You can execute docker exec -it lempdock_php-fpm_1 bash
or just write lemp/ssh
. The ssh
bash file in thelemp
directory already has the command for easy use.
5. To stop the docker containers
Execute command lemp/stop
to stop the containers and lemp/distroy
to remove all the LEMP Dock images. No worries, your data will be stored in your data
directory and source code will be in your www
directory.
How to add a new site in LEMP Dock
Here I’ll demonstrate the full process of adding a new Laravel site in the container.
Start the container and ssh
into it.
lemp/run
lemp/ssh
By default, you’ll be in the /var/www/
directory. Just create a new Laravel project here now
composer create-project --prefer-dist laravel/laravel blog
composer install
You’ll have a new /var/www/blog
directory with Laravel framework installed. Now you need to set your nginx server.
Exit from your container with command exit
and move to nginx/sites directory cd images/nginx/sites
. Copy default config file to a new blog config file.
cp default.conf blog.conf
Open your blog.conf
file with your favorite text editor and change following lines. Follow the instruction in @todo
section and you’ll complete this nginx server configure part.
Only one last step is remaining. Open your host machines /etc/hosts
file and add the line below at bottom of the file
127.0.0.1 blog.local
Now you should be able to access your awesome http://blog.local
site in your browser.
Connecting to MySQL Database
The default user is root
and password is also root
.
Inside the container, MySQL hostname is mysql
, NOT 127.0.0.1
, but when you want to access the MySQL database from your host machine, your host will be 127.0.0.1
and port will be 33066
Default settings to connect from SequelPro or MySQL Workbench
host: 127.0.0.1
user: root
pass: root
port: 33066
Default settings to connect from PHP App inside the container
host: mysql
user: root
pass: root
port: 3306
Conclusion
There are still a few things to test and confirm. For example, Laravel echo.
The whole process of developing, running the docker, executing any lemp/*
command is tested in MacOS only. If anyone wants to test this in Windows and find any bug, please create an issue in the GitHub.
I appreciate and Thank you for reading until here.
I am not perfect and I hope you guys can find some issues and improvement opportunity in this LEMP Dock Docker Image. Please let me know your comments on this and I’ll try to improve it.
If you want to contribute to the project, please do so. I’ll add a roadmap or your guys can make a Roadmap PR as well.