我有一个Yii 2 + nginx + php + redis + other services的项目我想让每个php,nginx,redis等服务位于一个单独的容器中
不工作:从Yii 2(另一个容器)访问redis?
配置:
文件:/前端/配置/main.php
return [
'components' => [
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => '127.0.0.1',
'port' => 6379,
'database' => 1,
],
停靠-撰写.yml
version: '3'
services:
nginx:
restart: always
container_name: mnginx
build:
context: ./
dockerfile: ./docker/nginx/nginx.docker
ports:
- "80:80"
links:
- php-fpm
volumes:
- ./:/var/www/html/ztt.loc
php-fpm:
restart: always
container_name: mphp
ports:
- "9000:9000"
build:
context: ./
dockerfile: ./docker/php/php-fpm.docker
volumes:
- ./:/var/www/html/ztt.loc
redis:
build:
context: ./
dockerfile: ./docker/redis_data/redis.docker
container_name: mredis
restart: always
expose:
- 6379
ports:
- 6379:6379
memcached:
image: memcached:1.6.17
container_name: mymemcached
restart: always
command: "-p 11211 -m 256 -c 8092"
ports:
- "11211:11211"
/标记/nginx/nginx.标记
FROM nginx:1.20-alpine
COPY ./docker/nginx/conf/nginx.conf /etc/nginx/nginx.conf
COPY ./docker/nginx/conf/ztt.loc.conf /etc/nginx/conf.d/ztt.loc.conf
/docker/php/php-fpm.文档编辑器
FROM php:7.4.30-fpm
COPY ./docker/php/conf/php-fpm.conf /usr/local/etc/php-fpm.conf
COPY ./docker/php/conf/www.conf /usr/local/etc/php-fpm.d/www.conf
COPY ./docker/php/conf/php.ini /usr/local/etc/php/php.ini
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && \
install-php-extensions zip memcached
/docker/nginx/配置文件/nginx.配置文件
user nginx nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
worker_rlimit_nofile 8192;
events {
worker_connections 4096;
}
http {
include /etc/nginx/mime.types;
include /etc/nginx/fastcgi.conf;
index index.html index.htm index.php;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128;
include /etc/nginx/conf.d/*.conf;
}
/docker/nginx/conf/ztt.位置配置文件
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name ztt.loc;
root /var/www/html/ztt.loc/frontend/web;
error_log /var/log/nginx/ztt.loc.log;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm:9000;
try_files $uri =404;
}
location ~* /\. {
deny all;
}
}
/文档编辑器/redis_data/redis.文档编辑器
FROM redis:6.2.7
COPY ./docker/redis_data/conf/redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
/docker/redis_data/配置文件/redis.配置文件
bind 127.0.0.1
protected-mode no
port 6379
tcp-backlog 511
timeout 60
tcp-keepalive 300
daemonize no
pidfile /var/run/redis_6379.pid
loglevel notice
#logfile "/usr/local/var/log/redis.log"
databases 6
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
我什么时候去:******
页面结果:
Database Exception – yii\db\Exception
Failed to open redis DB connection (tcp://127.0.0.1:6379, database=1): 111 - Connection refused
Error Info: Connection refused
它的ubuntu 20.04在本地机器上;我安装了redis-cli;我可以从这个Redis客户端访问。
2条答案
按热度按时间qrjkbowd1#
问题是你还没有在redis容器中公开端口,这意味着你的机器和其他容器都不能访问它。你还需要把它绑定到你的本地端口port,这样你就可以通过指向localhost来访问它。你可以在docker's docs中进一步阅读
请注意端口6379绑定和公开的差异 *
vngu2lb82#
将您docker-compose.yml更改为
并在文件中将连接URL更改为172.18.0.103:/frontend/config/main.php