Kindly go through this documentation before getting into it.
Kindly install Docker and Docker Compose on your local machine.
Add the necessary configurations for your MySQL service in the docker-compose.yml
file. Here’s an example:
version: "3.8"
services:
db:
container_name: edums
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: your_db
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "3306:3306" # Exposes MySQL port 3306 to the host machine
networks:
- app-network
networks:
app-network:
driver: bridge
Run the following command in your terminal to start the containers in detached mode:
docker-compose up -d
To ensure your MySQL container is running, use the following command. You should see your container listed, and its name should be edums
if you specified it in your docker-compose.yml
file:
docker ps
You can connect to the MySQL server from within the Docker container using:
NOTE: To be able to do this, you need to have the MySQL client installed. Check if it's installed:
mysql --version
If it is not installed, you can install it using:
sudo apt update
sudo apt install mysql-client
To access your database running in the Docker container:
docker exec -it edums mysql -u root -p
To connect to your MySQL container from your local machine, run:
mysql -h 127.0.0.1 -P 3306 -u root -p
You can specify your own port for the PHP server. For example, to run it on port 8000, use:
php -S localhost:8000
If you encounter errors related to SQL mode, you can set it using:
docker exec -it edums mysql -u root -p -e "SET GLOBAL sql_mode='';"
If you encounter the following error while running your application:
Uncaught mysqli_sql_exception: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA...
You can resolve it by allowing function creation without strict mode:
docker exec -it edums mysql -u root -p -e "SET GLOBAL log_bin_trust_function_creators = 1;"
When you're done testing or if you want to stop the services, use:
docker-compose down
- You could run this script to get you up and running after setting up docker
./script.sh
-
Docker Version: Ensure that your Docker and Docker Compose versions are up-to-date.
-
MySQL Client: Make sure you have the MySQL client installed on your host machine if you want to connect from there.
-
Environment Variables: Update the
MYSQL_DATABASE
,MYSQL_USER
, andMYSQL_PASSWORD
values in thedocker-compose.yml
file as per your requirements. -
Error Handling: If you encounter issues, check the logs with:
docker-compose logs