PHP 7 Docker容器卷挂载配置及localhost访问异常排查
Alright, let’s walk through the most likely issues with your docker-compose.yml setup—since your container starts up fine but browser access throws an error, these are the top things to check:
1. Incorrect Volume Mount Path
The official PHP7 Apache container uses /var/www/html as its default document root. If your volume is mounted to the wrong directory (like /var/www or a custom path you made up), Apache won’t find your index.php file at all.
- Double-check your
volumessection in docker-compose.yml—it should look like this:volumes: - ./your-local-volume-dir:/var/www/html - Verify the file exists inside the container with:
docker exec -it <your-container-name> ls /var/www/html
2. File Permission Problems
Linux-based containers are strict about file permissions. If your local index.php is owned by root or lacks read permissions for the Apache user (www-data), the server can’t load it—leading to 500 errors or blank pages.
- Check permissions inside the container:
docker exec -it <your-container-name> ls -l /var/www/html/index.php - Fix this either by adjusting local file permissions, or adding a
userdirective to your docker-compose.yml:
Or run this command after starting the container:user: "www-data:www-data"docker exec -it <your-container-name> chown www-data:www-data /var/www/html/index.php
3. Using the Wrong PHP Image
If you’re using the php:7-cli image instead of php:7-apache, there’s no web server listening on port 80 at all! The CLI image is only for running PHP scripts directly, not serving web pages.
- Make sure your
imageline in docker-compose.yml specifies an Apache variant:image: php:7-apache
4. Port Conflict on Your Host
Even though you mapped 55211:80, port 55211 might already be taken by another process on your Windows machine.
- Check for port conflicts in PowerShell:
netstat -ano | findstr :55211 - If you see results, kill the conflicting process or pick a different host port (like 55212:80).
5. Apache Doesn’t Prioritize index.php
Some default Apache configurations only look for index.html first. Even if index.php exists, Apache might not load it unless you explicitly set the directory index.
- Add a
.htaccessfile to your local volume directory with this line:DirectoryIndex index.php index.html - Or customize the Apache config in your docker-compose setup if you’re using a custom config file.
If you share your actual docker-compose.yml content, we can pinpoint the exact issue faster—but these are the most common fixes for your scenario.
内容的提问来源于stack exchange,提问作者klewis

