This project is a PHP-MySQL web application designed to run in a Dockerized environment. The setup includes a web server with PHP and a MySQL database, all managed through Docker Compose.
Before starting, ensure you have the following installed:
- Git: To clone the project repository.
- Docker: To build and run containerized services.
- Docker Compose: To manage multi-container Docker applications.
Clone the project from the GitHub repository:
git clone https://github.com/username/project.git
cd projectEnsure the project directory contains the following files:
project/
├── Dockerfile
├── docker-compose.yml
├── init.sql
├── index.php
├── db_connect.php
└── Other project files (HTML, CSS, JS, etc.)
The Dockerfile is set up to use a PHP-Apache image and includes mysqli support:
FROM php:apache
WORKDIR /var/www/html
COPY . .
RUN docker-php-ext-install mysqliThe docker-compose.yml defines two services: web (PHP-Apache server) and db (MySQL database):
version: '3.8'
services:
web:
build: .
ports:
- "8080:80"
volumes:
- .:/var/www/html
depends_on:
- db
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: adm
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
volumes:
db_data:The init.sql file initializes the MySQL database and creates a users table:
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);Run the following commands to build and start the containers:
docker-compose down --volumes # Clean up any existing volumes
docker-compose up --build- Open your web browser and navigate to http://localhost:8080.
- The application should be running.
To interact with the database:
docker exec -it mysql mysql -u root -pEnter the password root (as specified in the docker-compose.yml file).
Run the following commands to verify the setup:
SHOW DATABASES;
USE adm;
SHOW TABLES;You should see a users table in the adm database.
If the application is not working, check the logs:
docker-compose logsTo debug inside a container, use:
- Web container:
docker exec -it web bash - Database container:
docker exec -it mysql mysql -u root -p
To stop and remove all containers, networks, and volumes:
docker-compose down --volumes
docker container prune -f
docker volume prune -f- Ensure
init.sqlis properly mapped indocker-compose.ymlfor database initialization. - The default ports are
8080for the web application and3306for MySQL.
This README.md should provide clear instructions for setting up and running your Dockerized web application. 🚀