【问题标题】:memcached not working in docker-composememcached 在 docker-compose 中不起作用
【发布时间】:2017-11-14 18:14:37
【问题描述】:

我在 docker-compose 中遇到了 memcached 的问题。这是 docker-compose.yml:

nginx:
    container_name: nginx
    image: nginx:latest
    ports:
        - 127.0.0.2:8000:80
    volumes:
        - ./htdocs:/htdocs
        - ./nginx.conf:/etc/nginx/conf.d/nginx.conf
    links:
        - php
php:
    container_name: php
    build: ./php
    volumes:
        - ./htdocs:/htdocs
        - ./php/php.ini:/usr/local/etc/php/php.ini  
    links:
        - mysql
        - memcached
mysql:
    container_name: mysql
    image: mysql:latest
    ports:
        - 127.0.0.2:8001:3306
    volumes:
        - ./my.cnf:/etc/mysql/my.cnf
        - ./db:/var/lib/mysql
    environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=db
        - MYSQL_USER=root
        - MYSQL_PASSWORD=root
memcached:
    container_name: memcached
    image: memcached:latest
    ports:
        - "11211:11211"

这是我的 php 代码:

error_reporting(E_ALL & ~E_NOTICE);

$memcached = new Memcached; 

$memcached->addServer('0.0.0.0', 11211);

echo '<pre>'; print_r($memcached->getServerList()); echo '</pre>';

if($memcached->getStats() === false) {
    echo 'returned false';
} else {
    echo '<pre>'; print_r($memcached->getStats()); echo '</pre>';
}

结果是:

Array
(
     [0] => Array
        (
            [host] => 0.0.0.0
            [port] => 11211
            [type] => TCP
        )
)

returned false

为什么 memcached 看不到服务器? (getStats 没有返回任何内容)命令“docker ps”返回运行 docker memcached:lastest 作为端口“0.0.0.0:11211->11211/tcp”的列表。对不起我的英语不好。

【问题讨论】:

  • for php memcached 应该在主机 "memcached" 端口 11211 上运行。
  • $memcached->addServer('memcached', 11211) 及其工作:) 谢谢:)
  • 您也应该将它用于其他链接容器。在 PHP 中连接 mysql 时,使用链接名称比使用某些 IP 更好。

标签: php docker-compose memcached


【解决方案1】:

只需将主机0.0.0.0 替换为memcached

Host memcached 链接到 memcached 容器的 IP。

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2020-06-10
    • 2020-10-10
    相关资源
    最近更新 更多