Saturday, January 15, 2022

Setting up Docker on Mac M1 to run Apache/PHP/Maria DB

  1. Follow the instructions here: How To Run Your Entire Development Environment in Docker Containers on macOS 

    However you'll need to install MariaDB instead of MySQL 8.0 as at the time of writing MySQL doesn't run on the Mac M1 processor. (Anyway MariaDB is better).

  2. To install MariaDB follow the instructions here: Apple M1 Chip to work with MariaDB docker image

    The command to use to launch the docker MariaDB server container is: 

    docker run --restart always --name mysql-localhost --net dev-network -v /Users/[your_username]/Develop/mysql_data/8.0:/var/lib/mysql -v /Users/[your_username]/Develop/docker_configs/mysql:/etc/mysql/conf.d -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=[Root DB Password] mariadb

    This is the command from the instructions from step 1 but specifying that we're using MariaDB instead of mysql:8.0 

  3. At this point PHP should be working and you should be able to access the database using your database management application eg: Sequel Ace.  So check that you can do this. However you'll probably find that PHP is not able to connect to the MariaDB database. So there are 2 more things that you'll need to do.

  4. Find the IP address of the mariaDB server:
     

    docker inspect mysql-localhost | grep IPAddress

    This is the address that you'll need to use for the server name in the mysqli_connect statement.

  5. Create a DB user for php to use as I believe that the recent versions of MariaDB do not allow PHP to connect using the root user. See: How do I create a user with the same privileges as root in MySQL/MariaDB?