Local docker environment for PHP development with components:
- PHP 7.3
- Nginx
- MySQL 8
- Redis
We'll follow important docker rule - one service per container. Also, we'll mount docker volumes to keep code and data on our host machine and use it in containers, so our containers will work for "read-only" purpose and can be stopped, deleted or replaced without losing data.
.
├── containers # Docker containers
│ ├── mysql
│ ├── nginx
│ ├── php
| └── redis
├── logs # Different logs
└── www # Project files
Just run
docker-compose up -d
Open the browser and go to localhost:8080. You'll see:
Check MySQL connection, run localhost:8080/db.php
In new versions of PHP and MySQL you may see an error:
SQLSTATE[HY000] [2002] Connection refused
Just connect to a MySQL container
docker exec -it docker-php bash
Then connect to mysql from command line
mysql -uroot -psecret hellodb
And run commands:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'secret';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'secret';
ALTER USER 'default'@'%' IDENTIFIED WITH mysql_native_password BY 'secret';