Docker

PHP Web Applications' database configurations in Windows 10 Pro and Windows 10 Home using Docker

Windows 10 and Windows 10 Pro database configuration for PHP Web Apps using Docker

If you wish to develop in Windows 10, there are configurations you need to know if you want to develop PHP Web Applications using Docker.

Note:
Make sure you have Docker in your machine.
If you are using Windows 10 Home, install Docker Toolbox (Docker documentation for installing Docker Toolbox). If you are using Windows 10 Pro, install Docker Desktop Docker documentation for installing Docker Desktop.

Connecting to database

If you are using MySQL for development, you should have something like below in your docker-compose.yml file.

...
...
mysql:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: <your-database-name>
      MYSQL_USER: <your-user>
      MYSQL_PASSWORD: <your-user-password>
      MYSQL_ROOT_PASSWORD: <your-root-password>
      ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
...
...

Connecting to Database using Pure PHP would be something like below:

For Docker Desktop:

<?php
$HOST = "mysql";
$USER = <your-user>;  //or can be root
$PASS = <your-user-password>; //or <your-root-password> if you used root as $USER
$DB_NAME = <your-db-name>;

$conn = mysqli_connect($HOST, $USER, $PASS, $DB_NAME)or die("Database connection failed: " . mysqli_connect_error());
?>

$HOST is mysql because the we use the mysql docker container we put in our docker.compose.yml file.


For Docker Toolbox:

<?php
$HOST = "192.168.99.100:3306";
$USER = <your-user>;  //or can be root
$PASS = <your-user-password>; //or <your-root-password> if you used root as $USER
$DB_NAME = <your-db-name>;

$conn = mysqli_connect($HOST, $USER, $PASS, $DB_NAME)or die("Database connection failed: " . mysqli_connect_error());
?>

$HOST is 192.168.99.100 because if we open Docker Desktop, we will see there a message saying "Docker is configured to use the default machine with IP 192.168.99.100 ...".

Then, we added port 3306 since we defined in our ports in docker-compose.yml file like below:

ports:
      - "3306:3306"

Using Laravel Framework

Your .env file's database configuration should be like below:

For Docker Desktop:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=<your-database-name>
DB_USERNAME=<your-user>
DB_PASSWORD=<your-user-password>

For Docker Toolbox:

DB_CONNECTION=mysql
DB_HOST=192.168.99.100
DB_PORT=3306
DB_DATABASE=<your-database-name>
DB_USERNAME=<your-user>
DB_PASSWORD=<your-user-password>
Registration Login
Sign in with social account
or
Lost your Password?
Registration Login
Sign in with social account
or
A password will be send on your post
Registration Login
Registration