如何连接memcached laravel和docker



我已经在docker中创建了memcached容器docker-compose.yml:

#store cache
memcached:
image: memcached
container_name: memcached
ports:
- "11211:11211"

.env

MEMCACHED_HOST=memcached
MEMCACHED_PORT=11211
CACHE_DRIVER=memcached

并且在我的CCD_ 1文件部分CCD_

'memcached' => [
'driver' => 'memcached',
'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
'sasl' => [
env('MEMCACHED_USERNAME'),
env('MEMCACHED_PASSWORD'),
],
'options' => [
// Memcached::OPT_CONNECT_TIMEOUT  => 2000,
],
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],

在一个方法中,我做了以下例子:

$user = Cache::remember('user', 60, function() {
return AppModelsUser::first();
}); */
$c = new Memcached();
$c->addServer("memcached", 11211);
var_dump( $c->getAllKeys() );

但我得到了错误:

Error: Class 'Memcached' not found 

我不知道这是否是我的容器中的问题,因为当我用以下命令进入容器时:

docker-compose exec --user 0 memcached bash 

我在容器中运行了stats命令,但我得到了一个错误,即没有找到

root@520aaa151216:/# stats
bash: stats: command not found

我已执行:

php artisan cache:clear
composer dump-autoload 

但是memcached错误仍在继续。

这是我的码头文件

FROM php:7.3-fpm
LABEL version="1.0"
#install pdftk
RUN mkdir -p /usr/share/man/man1mkdir -p /usr/share/man/man1
# Install dependencies
RUN apt-get update && apt-get upgrade -y && apt-get install -y 
build-essential 
libpng-dev 
libjpeg62-turbo-dev 
libfreetype6-dev 
libzip-dev 
zip 
locales 
vim 
unzip 
git 
jpegoptim optipng pngquant gifsicle 
curl 
npm 
supervisor 
pdftk
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql bcmath mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
RUN pecl install xdebug-3.0.4  && docker-php-ext-enable xdebug
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy existing application directory contents
WORKDIR /var/www
ADD ./ /var/www
# install laravel
RUN COMPOSER_MEMORY_LIMIT=-1 composer install
RUN composer dump-autoload
RUN php artisan config:cache
RUN php artisan route:cache
RUN php artisan route:clear
#npm install
#USER root
RUN npm i npm@latest -g
RUN npm install --global yarn
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Copy supervisor configuration
ADD docker/supervisor.conf /etc/supervisor/conf.d/supervisor.conf
#Expose ports
EXPOSE 9000
EXPOSE 9003
#Run commands
CMD  ["/usr/bin/supervisord"]

在此链接中找到:https://github.com/devilbox/docker-php-fpm/blob/master/Dockerfiles/mods/Dockerfile-7.3

我在我的案卷中添加了:

# Install Memcached for php 7
RUN set -eux 
&& pecl install memcached 
&& docker-php-ext-enable memcached 
&& true

最新更新