Docker部署Invoice Ninja遇阻求助:APP_KEY生成报错与本地访问异常问题
Hey Luis, let's work through each of your issues to get Invoice Ninja running smoothly on your local machine:
1. Is the SQL Error When Generating APP_KEY Normal?
That error is expected if you're running the key generation command on your local machine instead of inside the Docker container. Here's why:
- The
php artisan key:generatecommand tries to connect to the database to validate the environment, but your local machine doesn't have the MySQL instance running (it’s isolated inside the Dockerdbcontainer). - This isn’t a critical failure, but it’s not the correct way to generate the key. The proper approach is to run the command within the web container where it can access the database service via Docker’s internal network.
2. Fixing Page Not Found & 502 Bad Gateway Errors
These issues stem from misconfigured environment variables, Docker network settings, or host file mappings. Let’s resolve them step by step:
Step 1: Generate APP_KEY Correctly
- First, start just the database container to ensure it’s fully initialized:
docker-compose up -d db - Wait 30-60 seconds for MySQL to boot, then enter the web container:
docker-compose exec web bash - Inside the container, run the key generation command (this will work because the container can reach the
dbservice):php artisan key:generate - Exit the container with
exitonce done.
Step 2: Verify Critical .env Configurations
Open your .env file and double-check these key settings:
DB_HOST=db(not localhost—Docker Compose uses service names for internal container communication)- Ensure
DB_DATABASE,DB_USERNAME, andDB_PASSWORDmatch the environment variables set for thedbservice in yourdocker-compose.yml(default values are usuallyninja,ninja,ninja) - Keep
APP_URL=http://in5.test:8000as you configured it.
Step 3: Fix Host File & Docker Compose Settings
- Host File: Edit your local hosts file (Linux/macOS:
/etc/hosts; Windows:C:\Windows\System32\drivers\etc\hosts) and add this line:127.0.0.1 in5.test - Docker Compose Ports: Confirm your
webservice has the correct port mapping to expose the container to your local machine:ports: - "8000:80" - Extra Hosts: The
extra_hostsconfiguration lets the container access services on your local machine. If you don’t need this functionality, you can remove the line entirely—it doesn’t affect your ability to access the container from your host.
Step 4: Reset & Restart Services
- Stop all running containers to clear stale state:
docker-compose down - Restart services in detached mode:
docker-compose up -d - Verify both
dbandwebcontainers are running:docker-compose ps - If errors persist, check the web container logs for detailed troubleshooting info:
docker-compose logs web
Step 5: Fix File Permissions
Ensure local mounted directories have the correct ownership for the container to access files. Run this from your repo directory:
chown -R 1500:1500 ./docker/app/public
The 1500:1500 UID/GID matches the user used inside the Invoice Ninja Docker container.
Final Access Test
Once all services are up, open your browser and navigate to http://in5.test:8000—you should see the Invoice Ninja setup page.
内容的提问来源于stack exchange,提问作者Luis Enrique

