You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

docker-compose何时调用Dockerfile?Apache URL重写问题求助

Hey there! Let's break down your two questions clearly and walk through practical solutions for each:

1. When does docker-compose invoke a Dockerfile?

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 build field in docker-compose.yml: If you define a build path 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 a build configuration. 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.

2. Fixing URL Rewrite Issues with Apache in Your Setup

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:

  1. Create a Dockerfile in your project root directory:
    FROM php:5.6-apache
    RUN a2enmod rewrite  # Enables Apache's rewrite module
    
  2. Update your docker-compose.yml's web service to use build instead of image:
    web:
      build: .  # Points to the directory with your new Dockerfile
      volumes:
        - ./:/var/www/html/
      environment:
        - ALLOW_OVERRIDE=true
      ports:
        - "80:80"
      links:
        - db
    
  3. 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 have AllowOverride All (the image's startup script converts ALLOW_OVERRIDE=true to 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 .htaccess file 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:05:04