DEV Community

Cover image for How to use Redis with WordPress (docker-compose)
Alexandre Plennevaux
Alexandre Plennevaux

Posted on

26

How to use Redis with WordPress (docker-compose)

I'm putting this here because it took me a long while to figure this out.

I wanted to improve WordPress's performance by using Redis object caching. My environment is defined in a docker compose.

version: '3.6'
services:
  redis:
      image: 'redis:alpine'
      ports:
        - '6379:6379'
      restart: always
      expose:
        - '6379'

  wordpress:
      image: 'wordpress:latest'
Enter fullscreen mode Exit fullscreen mode

There is a handy WordPress plugin for that, but of course, it needs redis to be installed and configured properly. That's the part I struggled with as it could not connect to my redis container.

Normally, Redis should work out-of-the-box with WordPress as long as it is accessible via localhost:6379.

The thing is, in a docker setup the redis instance is accessible via its container name.

So in the above example, in order for WordPress to find it, you need to modify the constant WP_REDIS_HOST to "redis" in your wp-config.php file.

define('WP_REDIS_HOST', 'redis');
define('WP_REDIS_PORT', '6379');
Enter fullscreen mode Exit fullscreen mode

Additional constants are available. More information is available here.

That's it, really. I hope this helps someone.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (2)

Collapse
 
trainingcity profile image
John Downing

Looks like this recipe may longer work. I am running into an issues today with the latest plugin release. FYI:
wordpress.org/support/topic/redis-...

Collapse
 
uebmaster profile image
Uebmaster

i did it and works with default theme, but when i try to install hub theme (themeforest.net/item/hub-responsiv...) i got a message like the link has expired.

but if i use a solution like easyengine.io, which also works under docker, i can install the theme without problem, what am i doing wrong?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay