Docker Apache Adminer PHP Setup
October 22, 2024 ยท AI Conversations
version: '3.8'
services:
web:
image: php:8.3-apache
container_name: apache_php
volumes:
- ./src:/var/www/html
- ./apache/vhost.conf:/etc/apache2/sites-enabled/000-default.conf
ports:
- "8080:80"
networks:
- app-network
environment:
- APACHE_LOG_DIR=/var/log/apache2
depends_on:
- db
db:
image: mysql:8.0
container_name: mysql_db
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: mydb
MYSQL_USER: user
MYSQL_PASSWORD: userpassword
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
networks:
- app-network
adminer:
image: adminer
container_name: adminer
depends_on:
- db
ports:
- "8081:8080"
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
db_data:
### Steps:
1. **Save this file** as `docker-compose.yml` in your project directory.
2. **Create the required folder structure:**
– Create a `src/` folder for your PHP project files.
– Create an `apache/` folder and add your `vhost.conf` Apache virtual host configuration file.
Example `vhost.conf`:
<VirtualHost *:80>
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
3. **Run the Docker containers**:
Open a terminal in the directory containing the `docker-compose.yml` file and run:
docker-compose up -d
4. **Access your services**:
– Visit `http://localhost:8080` for the Apache/PHP server.
– Visit `http://localhost:8081` for Adminer to manage your MySQL database.
This setup will give you a local development environment running PHP 8.3, Apache, MySQL, and Adminer.
### 1. **Library Dockerfile (`lib.Dockerfile`)**
This Dockerfile will handle installing any necessary libraries or dependencies (e.g., custom PHP extensions or OS-level packages).
# lib.Dockerfile
FROM debian:buster-slim
# Install system libraries
RUN apt-get update && apt-get install -y
libonig-dev
libzip-dev
libfreetype6-dev
libjpeg62-turbo-dev
libpng-dev
unzip
&& rm -rf /var/lib/apt/lists/*
# Optional: Add more libraries as required for your project
# Create a volume for shared libraries (if needed)
VOLUME /usr/local/lib
# Define workdir
WORKDIR /usr/src/app
### 2. **PHP Dockerfile (`php.Dockerfile`)**
This Dockerfile will extend the PHP image and include the library layer defined in the `lib.Dockerfile`.
# php.Dockerfile
FROM php:8.3-apache
# Copy libraries from lib Docker container (assuming you build them first)
COPY --from=lib:latest /usr/local/lib /usr/local/lib
# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
&& docker-php-ext-install pdo pdo_mysql mbstring zip exif pcntl gd
# Enable Apache mod_rewrite (optional but often necessary for Laravel or similar frameworks)
RUN a2enmod rewrite
# Copy application source code
COPY ./src /var/www/html
# Set working directory
WORKDIR /var/www/html
# Expose port 80 for Apache
EXPOSE 80
### 3. **docker-compose.yml**
You can use `docker-compose.yml` to build and link these Dockerfiles.
version: '3.8'
services:
lib:
build:
context: .
dockerfile: lib.Dockerfile
container_name: custom_lib
web:
build:
context: .
dockerfile: php.Dockerfile
container_name: apache_php
volumes:
- ./src:/var/www/html
- ./apache/vhost.conf:/etc/apache2/sites-enabled/000-default.conf
ports:
- "8080:80"
networks:
- app-network
depends_on:
- db
- lib
db:
image: mysql:8.0
container_name: mysql_db
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: mydb
MYSQL_USER: user
MYSQL_PASSWORD: userpassword
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
networks:
- app-network
adminer:
image: adminer
container_name: adminer
depends_on:
- db
ports:
- "8081:8080"
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
db_data:
### Steps:
1. **Place the Dockerfiles:**
– Save `lib.Dockerfile` and `php.Dockerfile` in your project root directory.
2. **Run the build and bring up the services:**
– Run the following to build and start the containers:
docker-compose up --build -d
3. **Access your application:**
– PHP/Apache server: `http://localhost:8080`
– Adminer: `http://localhost:8081`
By separating the libraries from PHP, you can maintain modularity and reuse the library container across other PHP environments.