I have a project with Yii2 nginx php redis other services What I want so that each php, nginx, redis, etc... service located in a separate container
that does not work: access to redis from Yii2 (another container)?
configurations:
file: /frontend/config/main.php
return [
'components' => [
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => '127.0.0.1',
'port' => 6379,
'database' => 1,
],
docker-compose.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"
/docker/nginx/nginx.docker
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.docker
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/conf/nginx.conf
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.loc.conf
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;
}
}
/docker/redis_data/redis.docker
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/conf/redis.conf
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
when i am going to: http://ztt.loc/site/index
page result:
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
Its ubuntu 20.04 on local machiene; I am installed redis-cli; and i am has access from this redis-client.
CodePudding user response:
Problem is that you haven't exposed the port in the redis container, it means its not accessible to your machine neither to other containers. You also need to bind it to your local port port so you can access it by pointing to localhost. You can read further in docker's docs
redis:
build:
context: ./
dockerfile: ./docker/redis_data/redis.docker
container_name: mredis
restart: always
expose:
- 6379
ports:
- 6379:6379
Note the difference in the binding and exposing of port 6379*
CodePudding user response:
change your docker-compose.yml to
version: '3'
networks:
myztt:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16
gateway: 172.18.0.1
services:
nginx:
restart: always
container_name: mnginx
build:
context: ./
dockerfile: ./docker/nginx/nginx.docker
ports:
- "80:80"
networks:
myztt:
ipv4_address: 172.18.0.100
volumes:
- ./:/var/www/html/ztt.loc
php-fpm:
restart: always
container_name: mphp
build:
context: ./
dockerfile: ./docker/php/php-fpm.docker
volumes:
- ./:/var/www/html/ztt.loc
networks:
myztt:
ipv4_address: 172.18.0.101
redis:
build:
context: ./
dockerfile: ./docker/redis_data/redis.docker
container_name: mredis
restart: always
networks:
myztt:
ipv4_address: 172.18.0.103
and change connection url to 172.18.0.103 in file: /frontend/config/main.php
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => '172.18.0.103',
'port' => 6379,
'database' => 1,
],
],