Home > Software engineering >  The problem Docker PHP-Redis and healthcheck
The problem Docker PHP-Redis and healthcheck

Time:09-24

I have two services. Radish and PHP. How to make php wait for the launch of redis and write about it.I tried using healthcheck-s but apparently I am not setting it up correctly.

Docker-compose.yml

version: '2'
services:
  php:
    build:
      context: docker/web
      dockerfile: Dockerfile-php-7.0
    container_name: php
    ports:
      - "8280:80"
    links:
      - redis:redis
      redis:
         build: docker/cache
     container_name: redis

Dockerfile-php-7.0

FROM php:7.0
RUN pecl install redis \
&& docker-php-ext-enable redis
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php"]
EXPOSE 80


 index.php    
                                                                                  
<?php
echo 'Starting';
$redis = new Redis();
$redis->connect(getenv('host'), getenv('6379'));
var_dump($redis->incr('foo'));
?>

dockerfile

FROM redis:3.2-alpine
COPY conf/redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
EXPOSE 6379

Don't be afraid to scold me. I am just starting to learn docker. I would be very grateful for any help !!!

CodePudding user response:

Here is the docker-compose.yml file that you can use:

version: '2'
services:
  php:
    build:
      context: docker/web
      dockerfile: Dockerfile-php-7.0
    container_name: php
    ports:
      - "8280:80"
    depends_on:
      - redis
  redis:
    build: docker/cache
    container_name: redis

I also change the PHP script (index.php) as follows:

<?php
echo 'Starting';
$redis = new Redis();
$redis->connect('redis', '6379');
var_dump($redis->incr('foo'));

Now the whole stack works in my machine. I can connect redis container from php.

The php container log shows the following

docker logs -f php
Startingint(1)
  • Related