docker-compose何时调用Dockerfile?Apache URL重写问题求助
Hey there! Let's break down your two questions clearly and walk through practical solutions for each:
Docker-compose only uses a Dockerfile in specific scenarios, all tied to building custom images instead of pulling pre-built ones from a registry:
- When your service uses the
buildfield indocker-compose.yml: If you define abuildpath pointing to a directory that contains a Dockerfile, docker-compose will run the Dockerfile to build the image when you start the service (if the image doesn't exist yet). Example:web: build: ./web-service # This directory has a Dockerfile # ... other configs - When you run
docker-compose build: This command explicitly triggers a build using the Dockerfile for any service that has abuildconfiguration. It'll rebuild the image even if it already exists, unless you add flags to skip unchanged layers. - When you run
docker-compose up --build: This tells compose to first build the image via the Dockerfile (if needed) before starting the containers, ensuring you're using the latest version of your custom image.
If your service only specifies an image field (like your current web service using php:5.6-apache), docker-compose never uses a Dockerfile—it just pulls that pre-built image from Docker Hub.
Your Apache service is running, but URL rewrite isn't working? Let's go through the most common fixes step by step:
Step 1: Enable mod_rewrite (Critical!)
The official php:5.6-apache image does not enable the rewrite module by default. To fix this, you need to create a custom Dockerfile to enable it:
- Create a
Dockerfilein your project root directory:FROM php:5.6-apache RUN a2enmod rewrite # Enables Apache's rewrite module - Update your
docker-compose.yml'swebservice to usebuildinstead ofimage:web: build: . # Points to the directory with your new Dockerfile volumes: - ./:/var/www/html/ environment: - ALLOW_OVERRIDE=true ports: - "80:80" links: - db - Rebuild and restart your services:
docker-compose down && docker-compose build && docker-compose up -d
Step 2: Verify the ALLOW_OVERRIDE Setting
Your ALLOW_OVERRIDE=true environment variable should configure Apache to allow .htaccess files to override settings, but let's confirm it's working:
- Run this command to check the Apache site config inside the container:
docker exec -it $(docker-compose ps -q web) cat /etc/apache2/sites-available/000-default.conf - Look for the
<Directory /var/www/html>block—it should haveAllowOverride All(the image's startup script convertsALLOW_OVERRIDE=trueto this). If it doesn't, you can manually add this line to your Dockerfile:RUN sed -i '/<Directory \/var\/www\/html\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/sites-available/000-default.conf
Step 3: Check .htaccess Permissions and Rules
- Make sure your
.htaccessfile is in your project root (since you're mounting./to/var/www/html/), and that Apache's user (www-data) can read it:chmod 644 .htaccess - Double-check your rewrite rules are correct. A basic test rule would look like this:
RewriteEngine On # Redirects /test to index.php RewriteRule ^test$ index.php [L]
Step 4: Restart the Apache Container
After making any config changes, restart the web service to apply them:
docker-compose restart web
内容的提问来源于stack exchange,提问作者Exitos

