{"meta":{"title":"Creating Redis service containers","intro":"You can use service containers to create a Redis client in your workflow. This guide shows examples of creating a Redis service for jobs that run in containers or directly on the runner machine.","product":"GitHub Actions","breadcrumbs":[{"href":"/en/actions","title":"GitHub Actions"},{"href":"/en/actions/tutorials","title":"Tutorials"},{"href":"/en/actions/tutorials/use-containerized-services","title":"Use containerized services"},{"href":"/en/actions/tutorials/use-containerized-services/create-redis-service-containers","title":"Create Redis service containers"}],"documentType":"article"},"body":"# Creating Redis service containers\n\nYou can use service containers to create a Redis client in your workflow. This guide shows examples of creating a Redis service for jobs that run in containers or directly on the runner machine.\n\n## Introduction\n\nThis guide shows you workflow examples that configure a service container using the Docker Hub `redis` image. The workflow runs a script to create a Redis client and populate the client with data. To test that the workflow creates and populates the Redis client, the script prints the client's data to the console.\n\n> \\[!NOTE]\n> If your workflows use Docker container actions, job containers, or service containers, then you must use a Linux runner:\n>\n> * If you are using GitHub-hosted runners, you must use an Ubuntu runner.\n> * If you are using self-hosted runners, you must use a Linux machine as your runner and Docker must be installed.\n\n## Prerequisites\n\nYou should be familiar with how service containers work with GitHub Actions and the networking differences between running jobs directly on the runner or in a container. For more information, see [Communicating with Docker service containers](/en/actions/using-containerized-services/about-service-containers).\n\nYou may also find it helpful to have a basic understanding of YAML, the syntax for GitHub Actions, and Redis. For more information, see:\n\n* [Writing workflows](/en/actions/learn-github-actions)\n* [Getting Started with Redis](https://redis.io/learn/howtos/quick-start) in the Redis documentation\n\n## Running jobs in containers\n\nConfiguring jobs to run in a container simplifies networking configurations between the job and the service containers. Docker containers on the same user-defined bridge network expose all ports to each other, so you don't need to map any of the service container ports to the Docker host. You can access the service container from the job container using the label you configure in the workflow.\n\nYou can copy this workflow file to the `.github/workflows` directory of your repository and modify it as needed.\n\n```yaml copy\nname: Redis container example\non: push\n\njobs:\n  # Label of the container job\n  container-job:\n    # Containers must run in Linux based operating systems\n    runs-on: ubuntu-latest\n    # Docker Hub image that `container-job` executes in\n    container: node:20-bookworm-slim\n\n    # Service containers to run with `container-job`\n    services:\n      # Label used to access the service container\n      redis:\n        # Docker Hub image\n        image: redis\n        # Set health checks to wait until redis has started\n        options: >-\n          --health-cmd \"redis-cli ping\"\n          --health-interval 10s\n          --health-timeout 5s\n          --health-retries 5\n\n    steps:\n      # Downloads a copy of the code in your repository before running CI tests\n      - name: Check out repository code\n        uses: actions/checkout@v6\n\n      # Performs a clean installation of all dependencies in the `package.json` file\n      # For more information, see https://docs.npmjs.com/cli/ci.html\n      - name: Install dependencies\n        run: npm ci\n\n      - name: Connect to Redis\n        # Runs a script that creates a Redis client, populates\n        # the client with data, and retrieves data\n        run: node client.js\n        # Environment variable used by the `client.js` script to create a new Redis client.\n        env:\n          # The hostname used to communicate with the Redis service container\n          REDIS_HOST: redis\n          # The default Redis port\n          REDIS_PORT: 6379\n```\n\n### Configuring the container job\n\nThis workflow configures a job that runs in the `node:20-bookworm-slim` container and uses the `ubuntu-latest`  GitHub-hosted runner as the Docker host for the container. For more information about the `node:20-bookworm-slim` container, see the [node image](https://hub.docker.com/_/node) on Docker Hub.\n\nThe workflow configures a service container with the label `redis`. All services must run in a container, so each service requires that you specify the container `image`. This example uses the `redis` container image, and includes health check options to make sure the service is running. Append a tag to the image name to specify a version, e.g. `redis:6`. For more information, see the [redis image](https://hub.docker.com/_/redis) on Docker Hub.\n\n```yaml copy\njobs:\n  # Label of the container job\n  container-job:\n    # Containers must run in Linux based operating systems\n    runs-on: ubuntu-latest\n    # Docker Hub image that `container-job` executes in\n    container: node:20-bookworm-slim\n\n    # Service containers to run with `container-job`\n    services:\n      # Label used to access the service container\n      redis:\n        # Docker Hub image\n        image: redis\n        # Set health checks to wait until redis has started\n        options: >-\n          --health-cmd \"redis-cli ping\"\n          --health-interval 10s\n          --health-timeout 5s\n          --health-retries 5\n```\n\n### Configuring the steps for the container job\n\nThe workflow performs the following steps:\n\n1. Checks out the repository on the runner\n2. Installs dependencies\n3. Runs a script to create a client\n\n```yaml copy\nsteps:\n  # Downloads a copy of the code in your repository before running CI tests\n  - name: Check out repository code\n    uses: actions/checkout@v6\n\n  # Performs a clean installation of all dependencies in the `package.json` file\n  # For more information, see https://docs.npmjs.com/cli/ci.html\n  - name: Install dependencies\n    run: npm ci\n\n  - name: Connect to Redis\n    # Runs a script that creates a Redis client, populates\n    # the client with data, and retrieves data\n    run: node client.js\n    # Environment variable used by the `client.js` script to create a new Redis client.\n    env:\n      # The hostname used to communicate with the Redis service container\n      REDIS_HOST: redis\n      # The default Redis port\n      REDIS_PORT: 6379\n```\n\nThe *client.js* script looks for the `REDIS_HOST` and `REDIS_PORT` environment variables to create the client. The workflow sets those two environment variables as part of the \"Connect to Redis\" step to make them available to the *client.js* script. For more information about the script, see [Testing the Redis service container](#testing-the-redis-service-container).\n\nThe hostname of the Redis service is the label you configured in your workflow, in this case, `redis`. Because Docker containers on the same user-defined bridge network open all ports by default, you'll be able to access the service container on the default Redis port 6379.\n\n## Running jobs directly on the runner machine\n\nWhen you run a job directly on the runner machine, you'll need to map the ports on the service container to ports on the Docker host. You can access service containers from the Docker host using `localhost` and the Docker host port number.\n\nYou can copy this workflow file to the `.github/workflows` directory of your repository and modify it as needed.\n\n```yaml copy\nname: Redis runner example\non: push\n\njobs:\n  # Label of the runner job\n  runner-job:\n    # You must use a Linux environment when using service containers or container jobs\n    runs-on: ubuntu-latest\n\n    # Service containers to run with `runner-job`\n    services:\n      # Label used to access the service container\n      redis:\n        # Docker Hub image\n        image: redis\n        # Set health checks to wait until redis has started\n        options: >-\n          --health-cmd \"redis-cli ping\"\n          --health-interval 10s\n          --health-timeout 5s\n          --health-retries 5\n        ports:\n          # Maps port 6379 on service container to the host\n          - 6379:6379\n\n    steps:\n      # Downloads a copy of the code in your repository before running CI tests\n      - name: Check out repository code\n        uses: actions/checkout@v6\n\n      # Performs a clean installation of all dependencies in the `package.json` file\n      # For more information, see https://docs.npmjs.com/cli/ci.html\n      - name: Install dependencies\n        run: npm ci\n\n      - name: Connect to Redis\n        # Runs a script that creates a Redis client, populates\n        # the client with data, and retrieves data\n        run: node client.js\n        # Environment variable used by the `client.js` script to create\n        # a new Redis client.\n        env:\n          # The hostname used to communicate with the Redis service container\n          REDIS_HOST: localhost\n          # The default Redis port\n          REDIS_PORT: 6379\n```\n\n### Configuring the runner job\n\nThe example uses the `ubuntu-latest`  GitHub-hosted runner as the Docker host.\n\nThe workflow configures a service container with the label `redis`. All services must run in a container, so each service requires that you specify the container `image`. This example uses the `redis` container image, and includes health check options to make sure the service is running. Append a tag to the image name to specify a version, e.g. `redis:6`. For more information, see the [redis image](https://hub.docker.com/_/redis) on Docker Hub.\n\nThe workflow maps port 6379 on the Redis service container to the Docker host. For more information about the `ports` keyword, see [Communicating with Docker service containers](/en/actions/using-containerized-services/about-service-containers#mapping-docker-host-and-service-container-ports).\n\n```yaml copy\njobs:\n  # Label of the runner job\n  runner-job:\n    # You must use a Linux environment when using service containers or container jobs\n    runs-on: ubuntu-latest\n\n    # Service containers to run with `runner-job`\n    services:\n      # Label used to access the service container\n      redis:\n        # Docker Hub image\n        image: redis\n        # Set health checks to wait until redis has started\n        options: >-\n          --health-cmd \"redis-cli ping\"\n          --health-interval 10s\n          --health-timeout 5s\n          --health-retries 5\n        ports:\n          # Maps port 6379 on service container to the host\n          - 6379:6379\n```\n\n### Configuring the steps for the runner job\n\nThe workflow performs the following steps:\n\n1. Checks out the repository on the runner\n2. Installs dependencies\n3. Runs a script to create a client\n\n```yaml copy\nsteps:\n  # Downloads a copy of the code in your repository before running CI tests\n  - name: Check out repository code\n    uses: actions/checkout@v6\n\n  # Performs a clean installation of all dependencies in the `package.json` file\n  # For more information, see https://docs.npmjs.com/cli/ci.html\n  - name: Install dependencies\n    run: npm ci\n\n  - name: Connect to Redis\n    # Runs a script that creates a Redis client, populates\n    # the client with data, and retrieves data\n    run: node client.js\n    # Environment variable used by the `client.js` script to create\n    # a new Redis client.\n    env:\n      # The hostname used to communicate with the Redis service container\n      REDIS_HOST: localhost\n      # The default Redis port\n      REDIS_PORT: 6379\n```\n\nThe *client.js* script looks for the `REDIS_HOST` and `REDIS_PORT` environment variables to create the client. The workflow sets those two environment variables as part of the \"Connect to Redis\" step to make them available to the *client.js* script. For more information about the script, see [Testing the Redis service container](#testing-the-redis-service-container).\n\nThe hostname is `localhost` or `127.0.0.1`.\n\n## Testing the Redis service container\n\nYou can test your workflow using the following script, which creates a Redis client and populates the client with some placeholder data. The script then prints the values stored in the Redis client to the terminal. Your script can use any language you'd like, but this example uses Node.js and the `redis` npm module. For more information, see the [npm redis module](https://www.npmjs.com/package/redis).\n\nYou can modify *client.js* to include any Redis operations needed by your workflow. In this example, the script creates the Redis client instance, adds placeholder data, then retrieves the data.\n\nAdd a new file called *client.js* to your repository with the following code.\n\n```javascript copy\nconst redis = require(\"redis\");\n\n// Creates a new Redis client\n// If REDIS_HOST is not set, the default host is localhost\n// If REDIS_PORT is not set, the default port is 6379\nconst redisClient = redis.createClient({\n  url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`\n});\n\nredisClient.on(\"error\", (err) => console.log(\"Error\", err));\n\n(async () => {\n  await redisClient.connect();\n\n  // Sets the key \"octocat\" to a value of \"Mona the octocat\"\n  const setKeyReply = await redisClient.set(\"octocat\", \"Mona the Octocat\");\n  console.log(\"Reply: \" + setKeyReply);\n  // Sets a key to \"species\", field to \"octocat\", and \"value\" to \"Cat and Octopus\"\n  const SetFieldOctocatReply = await redisClient.hSet(\"species\", \"octocat\", \"Cat and Octopus\");\n  console.log(\"Reply: \" + SetFieldOctocatReply);\n  // Sets a key to \"species\", field to \"dinotocat\", and \"value\" to \"Dinosaur and Octopus\"\n  const SetFieldDinotocatReply = await redisClient.hSet(\"species\", \"dinotocat\", \"Dinosaur and Octopus\");\n  console.log(\"Reply: \" + SetFieldDinotocatReply);\n  // Sets a key to \"species\", field to \"robotocat\", and \"value\" to \"Cat and Robot\"\n  const SetFieldRobotocatReply = await redisClient.hSet(\"species\", \"robotocat\", \"Cat and Robot\");\n  console.log(\"Reply: \" + SetFieldRobotocatReply);\n\n  try {\n    // Gets all fields in \"species\" key\n    const replies = await redisClient.hKeys(\"species\");\n    console.log(replies.length + \" replies:\");\n    replies.forEach((reply, i) => {\n        console.log(\"    \" + i + \": \" + reply);\n    });\n    await redisClient.quit();\n  }\n  catch (err) {\n    // statements to handle any exceptions\n  }\n})();\n```\n\nThe script creates a new Redis client using the `createClient` method, which accepts a `host` and `port` parameter. The script uses the `REDIS_HOST` and `REDIS_PORT` environment variables to set the client's IP address and port. If `host` and `port` are not defined, the default host is `localhost` and the default port is 6379.\n\nThe script uses the `set` and `hset` methods to populate the database with some keys, fields, and values. To confirm that the Redis client contains the data, the script prints the contents of the database to the console log.\n\nWhen you run this workflow, you should see the following output in the \"Connect to Redis\" step confirming you created the Redis client and added data:\n\n```shell\nReply: OK\nReply: 1\nReply: 1\nReply: 1\n3 replies:\n    0: octocat\n    1: dinotocat\n    2: robotocat\n```"}