Docker端口未正常暴露求助——附docker-compose.yml配置
Hey there, let's break down your current docker-compose.yml first, and cover some common pitfalls that might be causing whatever issue you were about to describe (since your question got cut off):
Your Current Docker Compose Configuration
version: '3' services: app: build: . ports: - "8000:8000" volumes: - .:/srv/redditaurus environment: - REDDIT_KEY=${REDDIT_KEY} - REDDIT_SECRET=${REDDIT_SECRET} links: - mysql:mysql mysql: image: mysql environment: MYSQL_ROOT_PASSWORD: root ports: - "3306:3306" # volumes: # - ./mysql:/var/lib/mysql/ nginx: image: nginx ports: - "80:80"
Key Observations & Fixes for Common Issues
Based on what you've shared, here are some things to check if you're having trouble extending this setup:
MySQL Data Persistence: You've commented out the volume mount for MySQL. This means every time you stop/start the MySQL container, all your database data will be lost. Uncomment those lines to persist your database data locally:
volumes: - ./mysql:/var/lib/mysql/Nginx Isn't Configured to Proxy to Your App: Right now, the Nginx container is running but has no idea about your
appservice. If you're trying to access your app via port 80 (instead of 8000), you need to add a custom Nginx config and mount it into the container. For example:- Create a
nginx.conffile in your project root:server { listen 80; server_name localhost; location / { proxy_pass http://app:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } - Update the Nginx service in your
docker-compose.ymlto mount this config:nginx: image: nginx ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf depends_on: - app
- Create a
linksis Deprecated: Whilelinksstill works in Compose v3, the modern approach is to use a custom network for service communication. Add a network definition at the bottom of your file and attach all services to it:networks: app-network: driver: bridgeThen add
networks: - app-networkto each service (app, mysql, nginx).Environment Variables: Make sure you have a
.envfile in your project root with your actual Reddit API keys, or that you've exported those variables in your shell before runningdocker-compose up. If not, your app might fail to connect to the Reddit API.
If you can share more details about the specific issue you're facing (like what happens when you try to do X), I can give more targeted advice!
内容的提问来源于stack exchange,提问作者Mansoor Syed

