Skip to content

Commit cd37f2e

Browse files
committed
feat: add Docker Compose support
- Add Dockerfile with PHP 8.3-fpm-alpine and dig command - Add docker-compose.yml with nginx and php-fpm services - Add nginx configuration optimized for the application - Add entrypoint script for automatic permissions handling - Add .dockerignore to optimize build context - Add Makefile for convenient Docker operations - Add comprehensive Docker documentation to README - Add Docker troubleshooting guide The Docker setup provides: - Easy one-command deployment - Automatic dig path configuration - Proper cache directory permissions - Development-friendly volume mounts - Optimized Alpine-based images
1 parent 790ce6f commit cd37f2e

8 files changed

Lines changed: 358 additions & 1 deletion

File tree

.dockerignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# DDEV
6+
.ddev/
7+
8+
# IDE
9+
.idea/
10+
.vscode/
11+
*.swp
12+
*.swo
13+
*~
14+
15+
# OS
16+
.DS_Store
17+
Thumbs.db
18+
19+
# Logs
20+
*.log
21+
error_log
22+
access_log
23+
24+
# Documentation
25+
README.md
26+
LICENSE
27+
28+
# Docker files (don't need to copy these into the image)
29+
docker-compose.yml
30+
Dockerfile
31+
.dockerignore
32+
33+
# Cache (will be created by Dockerfile)
34+
cache/*
35+
!cache/.gitkeep

Dockerfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
FROM php:8.3-fpm-alpine
2+
3+
# Install required packages
4+
RUN apk add --no-cache \
5+
bind-tools \
6+
&& docker-php-ext-install -j$(nproc) \
7+
opcache
8+
9+
# Configure PHP
10+
RUN { \
11+
echo 'opcache.memory_consumption=128'; \
12+
echo 'opcache.interned_strings_buffer=8'; \
13+
echo 'opcache.max_accelerated_files=4000'; \
14+
echo 'opcache.revalidate_freq=2'; \
15+
echo 'opcache.fast_shutdown=1'; \
16+
echo 'opcache.enable_cli=1'; \
17+
} > /usr/local/etc/php/conf.d/opcache-recommended.ini
18+
19+
# Set working directory
20+
WORKDIR /var/www/html
21+
22+
# Copy application files
23+
COPY . /var/www/html
24+
25+
# Create cache directory with proper permissions
26+
RUN mkdir -p cache && \
27+
chown -R www-data:www-data cache && \
28+
chmod 755 cache
29+
30+
# Copy entrypoint script
31+
COPY docker/entrypoint.sh /usr/local/bin/
32+
RUN chmod +x /usr/local/bin/entrypoint.sh
33+
34+
EXPOSE 9000
35+
36+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
37+
CMD ["php-fpm"]

Makefile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.PHONY: help build up down restart logs shell test clean
2+
3+
# Default target
4+
help:
5+
@echo "Available commands:"
6+
@echo " make build - Build Docker images"
7+
@echo " make up - Start containers"
8+
@echo " make down - Stop containers"
9+
@echo " make restart - Restart containers"
10+
@echo " make logs - View logs"
11+
@echo " make shell - Access PHP container shell"
12+
@echo " make test - Test dig command"
13+
@echo " make clean - Remove containers and images"
14+
15+
# Build Docker images
16+
build:
17+
docker compose build
18+
19+
# Start containers
20+
up:
21+
docker compose up -d
22+
@echo "Application is running at http://localhost:8080"
23+
24+
# Stop containers
25+
down:
26+
docker compose down
27+
28+
# Restart containers
29+
restart: down up
30+
31+
# View logs
32+
logs:
33+
docker compose logs -f
34+
35+
# Access PHP container shell
36+
shell:
37+
docker compose exec php sh
38+
39+
# Test dig command
40+
test:
41+
docker compose exec php dig google.com +short
42+
43+
# Clean up everything
44+
clean:
45+
docker compose down -v
46+
docker image rm opensource-digwebinterface-php || true

README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ A modern, secure web interface for DNS lookups using the `dig` command. Perfect
5757

5858
## 📋 Requirements
5959

60+
### For Docker Installation (Recommended)
61+
- **Docker** 20.10 or higher
62+
- **Docker Compose** v2 or higher
63+
64+
### For Manual Installation
6065
- **PHP 8.3** or higher with the following extensions:
6166
- `json` (for AJAX API)
6267
- `filter` (for input validation)
@@ -66,7 +71,43 @@ A modern, secure web interface for DNS lookups using the `dig` command. Perfect
6671

6772
## 🚀 Installation
6873

69-
### Using DDEV (Recommended for Development)
74+
### Using Docker Compose (Recommended)
75+
76+
1. **Clone the repository**:
77+
```bash
78+
git clone https://github.com/yourusername/opensource-digwebinterface.git
79+
cd opensource-digwebinterface
80+
```
81+
82+
2. **Start the containers**:
83+
```bash
84+
docker compose up -d
85+
```
86+
87+
3. **Access the interface**:
88+
```
89+
http://localhost:8080
90+
```
91+
92+
4. **View logs** (optional):
93+
```bash
94+
docker compose logs -f
95+
```
96+
97+
5. **Stop the containers**:
98+
```bash
99+
docker compose down
100+
```
101+
102+
#### Docker Features
103+
- **PHP 8.3** with FPM for optimal performance
104+
- **Nginx** web server with optimized configuration
105+
- **Alpine Linux** base for minimal image size
106+
- **dig command** pre-installed and configured
107+
- **Volume mounts** for easy development
108+
- **Automatic permissions** handling for cache directory
109+
110+
### Using DDEV (Alternative for Development)
70111

71112
1. **Clone the repository**:
72113
```bash
@@ -285,6 +326,71 @@ Add new DNS record types in `config/config.php`:
285326
],
286327
```
287328

329+
## 🐳 Docker Troubleshooting
330+
331+
### Common Issues
332+
333+
1. **Port 8080 already in use**:
334+
```bash
335+
# Change the port in docker-compose.yml
336+
ports:
337+
- "8081:80" # Use port 8081 instead
338+
```
339+
340+
2. **Permission denied errors**:
341+
```bash
342+
# Rebuild with proper permissions
343+
docker compose down
344+
docker compose build --no-cache
345+
docker compose up -d
346+
```
347+
348+
3. **dig command not working**:
349+
```bash
350+
# Test dig inside container
351+
docker compose exec php dig google.com
352+
353+
# Check dig path
354+
docker compose exec php which dig
355+
```
356+
357+
4. **Changes not reflecting**:
358+
```bash
359+
# Restart services
360+
docker compose restart
361+
362+
# Or rebuild if needed
363+
docker compose down
364+
docker compose up -d --build
365+
```
366+
367+
### Docker Commands Reference
368+
369+
```bash
370+
# Start services
371+
docker compose up -d
372+
373+
# Stop services
374+
docker compose down
375+
376+
# View logs
377+
docker compose logs -f
378+
docker compose logs -f php # PHP logs only
379+
docker compose logs -f nginx # Nginx logs only
380+
381+
# Execute commands in container
382+
docker compose exec php sh # Shell access
383+
docker compose exec php dig example.com # Run dig command
384+
docker compose exec php php -v # Check PHP version
385+
386+
# Rebuild images
387+
docker compose build
388+
docker compose build --no-cache # Force rebuild
389+
390+
# Remove everything (including volumes)
391+
docker compose down -v
392+
```
393+
288394
## 🤝 Contributing
289395

290396
Contributions are welcome! Please:

docker-compose.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: '3.8'
2+
3+
services:
4+
nginx:
5+
image: nginx:alpine
6+
container_name: dig-nginx
7+
ports:
8+
- "8080:80"
9+
volumes:
10+
- ./docker/nginx.conf:/etc/nginx/conf.d/default.conf:ro
11+
- .:/var/www/html:ro
12+
depends_on:
13+
- php
14+
networks:
15+
- dig-network
16+
restart: unless-stopped
17+
18+
php:
19+
build:
20+
context: .
21+
dockerfile: Dockerfile
22+
container_name: dig-php
23+
volumes:
24+
- .:/var/www/html
25+
- ./cache:/var/www/html/cache:rw
26+
environment:
27+
- PHP_FPM_PM=dynamic
28+
- PHP_FPM_PM_MAX_CHILDREN=5
29+
- PHP_FPM_PM_START_SERVERS=2
30+
- PHP_FPM_PM_MIN_SPARE_SERVERS=1
31+
- PHP_FPM_PM_MAX_SPARE_SERVERS=3
32+
networks:
33+
- dig-network
34+
restart: unless-stopped
35+
# Ensure DNS works properly in container
36+
dns:
37+
- 8.8.8.8
38+
- 8.8.4.4
39+
40+
networks:
41+
dig-network:
42+
driver: bridge

docker/build.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
# Build and run script for Docker deployment
3+
4+
echo "Building Docker images..."
5+
docker compose build
6+
7+
echo "Starting services..."
8+
docker compose up -d
9+
10+
echo "Waiting for services to be ready..."
11+
sleep 5
12+
13+
echo "Testing dig command in container..."
14+
docker compose exec php dig google.com +short
15+
16+
echo ""
17+
echo "Services are running!"
18+
echo "Access the application at: http://localhost:8080"
19+
echo ""
20+
echo "To stop services: docker compose down"
21+
echo "To view logs: docker compose logs -f"

docker/entrypoint.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Ensure cache directory exists with proper permissions
5+
mkdir -p /var/www/html/cache
6+
chown -R www-data:www-data /var/www/html/cache
7+
chmod 755 /var/www/html/cache
8+
9+
# Update dig path in config if needed
10+
DIG_PATH=$(which dig)
11+
if [ -f /var/www/html/config/config.php ]; then
12+
sed -i "s|'dig_path' => '[^']*'|'dig_path' => '$DIG_PATH'|g" /var/www/html/config/config.php
13+
fi
14+
15+
# Execute the original command
16+
exec "$@"

docker/nginx.conf

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
server {
2+
listen 80;
3+
server_name localhost;
4+
root /var/www/html;
5+
index index.php;
6+
7+
# Charset
8+
charset utf-8;
9+
10+
# Logs
11+
access_log /var/log/nginx/access.log;
12+
error_log /var/log/nginx/error.log;
13+
14+
# Main location
15+
location / {
16+
try_files $uri $uri/ /index.php?$query_string;
17+
}
18+
19+
# API location
20+
location /api {
21+
try_files $uri $uri/ /api/query.php?$query_string;
22+
}
23+
24+
# PHP processing
25+
location ~ \.php$ {
26+
try_files $uri =404;
27+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
28+
fastcgi_pass php:9000;
29+
fastcgi_index index.php;
30+
include fastcgi_params;
31+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
32+
fastcgi_param PATH_INFO $fastcgi_path_info;
33+
34+
# Increase timeouts for DNS queries
35+
fastcgi_read_timeout 30s;
36+
fastcgi_send_timeout 30s;
37+
}
38+
39+
# Deny access to hidden files
40+
location ~ /\. {
41+
deny all;
42+
}
43+
44+
# Security headers
45+
add_header X-Frame-Options "SAMEORIGIN" always;
46+
add_header X-Content-Type-Options "nosniff" always;
47+
add_header X-XSS-Protection "1; mode=block" always;
48+
49+
# Gzip compression
50+
gzip on;
51+
gzip_vary on;
52+
gzip_min_length 1024;
53+
gzip_types text/plain text/css text/javascript application/javascript application/json;
54+
}

0 commit comments

Comments
 (0)