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

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

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

问题

<?= file_get_contents( get_template_directory_uri() . "/images/scroll_to_explore.svg" ) ?>
version: '3.3'

services:
  db:
    image: mysql:latest
    volumes:
      - ./database.sql:/docker-entrypoint-initdb.d/init.sql # 预先填充数据库
      - db_data:/var/lib/mysql # 在Docker存储中保留数据库数据
    restart: always
    env_file:
      - .env
    environment:
      DOCKER_COMPOSE_YML_LOCATION: ${PWD}
    container_name: wp_db
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - "8080:80"
    volumes:
      - /sessions
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      DOCKER_COMPOSE_YML_LOCATION: ${PWD}
      UPLOAD_LIMIT: 300M
    container_name: wp_phpmyadmin
    depends_on:
      - db
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      # 调试模式
      WORDPRESS_DEBUG: 1

      # Docker wp配置设置
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
      DOCKER_COMPOSE_YML_LOCATION: ${PWD}
    volumes:
      # 主题、插件和上传
      - ./wp-content:/var/www/html/wp-content
      # 配置
      - .htaccess:/var/www/html/.htaccess
      # PHP配置
      - ./php-extra.ini:/usr/local/etc/php/conf.d/php-extra.ini
    container_name: wp_wordpress
volumes:
  db_data: {}
allow_url_fopen: 1

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

英文:

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

<?= 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:

Warning: file_get_contents(http://0.0.0.0:8000/wp-content/themes/my_theme/images/scroll.svg): 
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:

version: '3.3'

services:
  db:
    image: mysql:latest
    volumes:
      - ./database.sql:/docker-entrypoint-initdb.d/init.sql # prepopulate database
      - db_data:/var/lib/mysql # persist database data inside docker storage
    restart: always
    env_file:
      - .env
    environment:
      DOCKER_COMPOSE_YML_LOCATION: ${PWD}
    container_name: wp_db
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - "8080:80"
    volumes:
      - /sessions
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      DOCKER_COMPOSE_YML_LOCATION: ${PWD}
      UPLOAD_LIMIT: 300M
    container_name: wp_phpmyadmin
    depends_on:
      - db
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      # debug mode
      WORDPRESS_DEBUG: 1

      # docker wp config settings
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
      DOCKER_COMPOSE_YML_LOCATION: ${PWD}
    volumes:
      # Theme, plugins and uploads
      - ./wp-content:/var/www/html/wp-content
      # Configurations
      - .htaccess:/var/www/html/.htaccess
      # PHP Configuration
      - ./php-extra.ini:/usr/local/etc/php/conf.d/php-extra.ini
    container_name: wp_wordpress
volumes:
  db_data: {}

This is my php-extra.ini:

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:

确定