Docker-Compose部署项目后www服务启动失败及浏览器访问问题
Let's break down your problems one by one—they’re likely connected, so fixing one might resolve the other:
1. WWW Service Failed to Start (Port Conflict Error)
That driver failed programming external connectivity error is almost always a port conflict: the port you’re trying to map from your container to the host is already being used by another process on your machine.
Here’s how to fix it:
- First, check the
portssection in yourdocker-compose.ymlfor the www service (e.g., if it’s80:80, you’re trying to use host port 80). - Verify if that port is occupied:
# For Linux/macOS sudo lsof -i :[YOUR_PORT] # For Windows netstat -ano | findstr :[YOUR_PORT] - If another process is using the port, either stop that process or change the host port in your docker-compose config (e.g., switch from
80:80to8080:80so you access vialocalhost:8080). - Also, make sure old containers aren’t lingering—run
docker-compose down -vto fully clean up previous containers and volumes before restarting.
2. Can’t Access the Project via Browser
This is probably tied to the first issue, but there’s a critical typo in your SQL script that might be breaking your app’s database connection too:
Looking at your script:
CREATE USER 'mgsv_user'@'localhost' IDENTIFIED BY 'mgsvpass';
GRANT SELECT, INSERT, CREATE, DROP ON mgsv.* TO 'mgsvuser'@'localhost';
You created a user named mgsv_user (with an underscore) but granted permissions to mgsvuser (no underscore)—that’s a mismatch! Plus, using localhost for the user’s host won’t work in Docker: your app runs in a separate container, so it won’t connect to the database via "localhost".
Fix your SQL script like this:
CREATE DATABASE mgsv; -- Use '%' to allow connections from any container on the Docker network CREATE USER 'mgsv_user'@'%' IDENTIFIED BY 'mgsvpass'; -- Match the username exactly to what you created GRANT SELECT, INSERT, CREATE, DROP ON mgsv.* TO 'mgsv_user'@'%'; FLUSH PRIVILEGES; -- Ensure permissions take effect immediately USE mgsv; CREATE TABLE IF NOT EXISTS `userinfo` ( `id` int(10) NOT NULL AUTO_INCREMENT, `email` text NOT NULL, -- Add your remaining table fields here PRIMARY KEY (`id`) );
Then, double-check your docker-compose.yml to ensure your app can reach the database correctly. Here’s a sample config snippet to reference:
services: db: image: mysql:5.7 # Or your preferred MySQL/MariaDB version environment: MYSQL_ROOT_PASSWORD: your_root_password_here MYSQL_DATABASE: mgsv MYSQL_USER: mgsv_user MYSQL_PASSWORD: mgsvpass volumes: - ./fixed-script.sql:/docker-entrypoint-initdb.d/init.sql # Mount your corrected script networks: - app-network www: build: . # Path to your app's Dockerfile ports: - "8080:80" # Adjusted port to avoid conflicts environment: DB_HOST: db # Use the database service name as the host (Docker DNS handles this) DB_USER: mgsv_user DB_PASSWORD: mgsvpass DB_NAME: mgsv depends_on: - db networks: - app-network networks: app-network: driver: bridge
Final Steps to Redeploy
- Save your corrected SQL script
- Update your
docker-compose.ymlwith the port and environment fixes - Run
docker-compose down -vto wipe old containers/volumes - Run
docker-compose up --buildto rebuild and start everything - Check the www service logs with
docker-compose logs wwwto confirm no startup errors
内容的提问来源于stack exchange,提问作者user3523406

