`file_get_contents` 在 Docker 中为什么不起作用?我该如何解决这个问题?

huangapple go评论88阅读模式
英文:

Why `file_get_contents` don't work in Docker? How can I solve this issue?

问题

  1. <?= file_get_contents( get_template_directory_uri() . "/images/scroll_to_explore.svg" ) ?>
  1. version: '3.3'
  2. services:
  3. db:
  4. image: mysql:latest
  5. volumes:
  6. - ./database.sql:/docker-entrypoint-initdb.d/init.sql # 预先填充数据库
  7. - db_data:/var/lib/mysql # 在Docker存储中保留数据库数据
  8. restart: always
  9. env_file:
  10. - .env
  11. environment:
  12. DOCKER_COMPOSE_YML_LOCATION: ${PWD}
  13. container_name: wp_db
  14. phpmyadmin:
  15. image: phpmyadmin/phpmyadmin
  16. restart: always
  17. ports:
  18. - "8080:80"
  19. volumes:
  20. - /sessions
  21. environment:
  22. PMA_HOST: db
  23. MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
  24. DOCKER_COMPOSE_YML_LOCATION: ${PWD}
  25. UPLOAD_LIMIT: 300M
  26. container_name: wp_phpmyadmin
  27. depends_on:
  28. - db
  29. wordpress:
  30. depends_on:
  31. - db
  32. image: wordpress:latest
  33. ports:
  34. - "8000:80"
  35. restart: always
  36. environment:
  37. # 调试模式
  38. WORDPRESS_DEBUG: 1
  39. # Docker wp配置设置
  40. WORDPRESS_DB_HOST: db:3306
  41. WORDPRESS_DB_USER: ${MYSQL_USER}
  42. WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
  43. WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
  44. DOCKER_COMPOSE_YML_LOCATION: ${PWD}
  45. volumes:
  46. # 主题、插件和上传
  47. - ./wp-content:/var/www/html/wp-content
  48. # 配置
  49. - .htaccess:/var/www/html/.htaccess
  50. # PHP配置
  51. - ./php-extra.ini:/usr/local/etc/php/conf.d/php-extra.ini
  52. container_name: wp_wordpress
  53. volumes:
  54. db_data: {}
  1. allow_url_fopen: 1

我尝试了在 php-extra.ini 中添加自定义PHP配置,但没有帮助。

英文:

I need to embed svg from php. And for this I used

  1. <?= file_get_contents( get_template_directory_uri() . "/images/scroll_to_explore.svg" ) ?>

When I served project locally, all were fine. But now project migrated to Docker. And I got next errors everywhere I have same code:

  1. Warning: file_get_contents(http://0.0.0.0:8000/wp-content/themes/my_theme/images/scroll.svg):
  2. Failed to open stream: Connection refused in /var/www/html/wp-content/themes/my_theme/parts/front/cover.php

This is my docker-compose.yml:

  1. version: '3.3'
  2. services:
  3. db:
  4. image: mysql:latest
  5. volumes:
  6. - ./database.sql:/docker-entrypoint-initdb.d/init.sql # prepopulate database
  7. - db_data:/var/lib/mysql # persist database data inside docker storage
  8. restart: always
  9. env_file:
  10. - .env
  11. environment:
  12. DOCKER_COMPOSE_YML_LOCATION: ${PWD}
  13. container_name: wp_db
  14. phpmyadmin:
  15. image: phpmyadmin/phpmyadmin
  16. restart: always
  17. ports:
  18. - "8080:80"
  19. volumes:
  20. - /sessions
  21. environment:
  22. PMA_HOST: db
  23. MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
  24. DOCKER_COMPOSE_YML_LOCATION: ${PWD}
  25. UPLOAD_LIMIT: 300M
  26. container_name: wp_phpmyadmin
  27. depends_on:
  28. - db
  29. wordpress:
  30. depends_on:
  31. - db
  32. image: wordpress:latest
  33. ports:
  34. - "8000:80"
  35. restart: always
  36. environment:
  37. # debug mode
  38. WORDPRESS_DEBUG: 1
  39. # docker wp config settings
  40. WORDPRESS_DB_HOST: db:3306
  41. WORDPRESS_DB_USER: ${MYSQL_USER}
  42. WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
  43. WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
  44. DOCKER_COMPOSE_YML_LOCATION: ${PWD}
  45. volumes:
  46. # Theme, plugins and uploads
  47. - ./wp-content:/var/www/html/wp-content
  48. # Configurations
  49. - .htaccess:/var/www/html/.htaccess
  50. # PHP Configuration
  51. - ./php-extra.ini:/usr/local/etc/php/conf.d/php-extra.ini
  52. container_name: wp_wordpress
  53. volumes:
  54. db_data: {}

This is my php-extra.ini:

  1. allow_url_fopen: 1

I tried to add custom php configuration to php-extra.ini but it didn't help

答案1

得分: 0

检查你的配置。你真的不应该尝试通过 0.0.0.0 连接... 这是一个特殊的地址,用于配置服务器监听任何接口。

如果你需要的文件在磁盘上,无需通过 HTTP 加载。如果你确实需要通过 HTTP 加载它,请考虑使用 127.0.0.1localhost

英文:

Check your configuration. You really shouldn't try to connect via 0.0.0.0... it's a special address for configuring the server to listen on any interface.

If this file you need is on disk, there's no need to load via HTTP anyway. If you do need to load it over HTTP, consider 127.0.0.1 or localhost instead.

huangapple
  • 本文由 发表于 2023年7月6日 15:59:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定