Hinweis
Auf GitHub gehostete Runner werden aktuell nicht auf GitHub Enterprise Server unterstützt. Weitere Informationen zur geplanten zukünftigen Unterstützung findest Du in der GitHub public roadmap.
Introduction
This 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.
Hinweis
Wenn bei deinen Workflows Docker-Containeraktionen, Auftragscontainer oder Dienstcontainer verwendet werden, musst du einen Linux-Runner nutzen:
- Wenn du GitHub-gehostete Runner verwendest, musst du einen Ubuntu-Runner verwenden.
- Wenn du selbst gehostete Läufer verwendest, musst du einen Linux-Rechner als deinen Läufer verwenden und Docker muss installiert sein.
Prerequisites
Du solltest damit vertraut sein, wie Service-Container mit GitHub Actions arbeiten und die Netzwerkunterschiede kennen zwischen dem Laufen lassen von Aufträgen direkt auf dem Läufer oder in einem Container. Weitere Informationen finden Sie unter Informationen zu Service-Containern.
You may also find it helpful to have a basic understanding of YAML, the syntax for GitHub Actions, and Redis. For more information, see:
- Writing workflows
- Getting Started with Redis in the Redis documentation
Running jobs in containers
Das Konfigurieren von Aufträgen für die Ausführung in einem Container vereinfacht die Netzwerkkonfigurationen zwischen dem Auftrag und den Dienstcontainern. Docker-Container im gleichen benutzerdefinierten Bridge-Netzwerk exponieren gegenseitig alle Ports, sodass Du keinen der Servicecontainer-Ports dem Docker-Host zuordnen musst. Mit der im Workflow konfigurierten Kennzeichnung kannst Du vom Auftrags-Container her auf den Dienst-Container zugreifen.
Du kannst diese Workflowdatei in das .github/workflows
-Verzeichnis deines Repositorys kopieren und nach Bedarf ändern.
name: Redis container example on: push jobs: # Label of the container job container-job: # Containers must run in Linux based operating systems runs-on: ubuntu-latest # Docker Hub image that `container-job` executes in container: node:20-bookworm-slim # Service containers to run with `container-job` services: # Label used to access the service container redis: # Docker Hub image image: redis # Set health checks to wait until redis has started options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v4 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see http://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to Redis # Runs a script that creates a Redis client, populates # the client with data, and retrieves data run: node client.js # Environment variable used by the `client.js` script to create a new Redis client. env: # The hostname used to communicate with the Redis service container REDIS_HOST: redis # The default Redis port REDIS_PORT: 6379
name: Redis container example
on: push
jobs:
# Label of the container job
container-job:
# Containers must run in Linux based operating systems
runs-on: ubuntu-latest
# Docker Hub image that `container-job` executes in
container: node:20-bookworm-slim
# Service containers to run with `container-job`
services:
# Label used to access the service container
redis:
# Docker Hub image
image: redis
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
uses: actions/checkout@v4
# Performs a clean installation of all dependencies in the `package.json` file
# For more information, see http://docs.npmjs.com/cli/ci.html
- name: Install dependencies
run: npm ci
- name: Connect to Redis
# Runs a script that creates a Redis client, populates
# the client with data, and retrieves data
run: node client.js
# Environment variable used by the `client.js` script to create a new Redis client.
env:
# The hostname used to communicate with the Redis service container
REDIS_HOST: redis
# The default Redis port
REDIS_PORT: 6379
Configuring the container job
Dieser Workflow konfiguriert einen Auftrag, der im Container node:20-bookworm-slim
ausgeführt wird, und nutzt den Runner ubuntu-latest
als Docker-Host für den Container. Weitere Informationen zum Container node:20-bookworm-slim
findest Du im Knotenimage auf Docker Hub.
Der Workflow konfiguriert einen Dienstcontainer mit der Bezeichnung redis
. Alle Dienste müssen in einem Container ausgeführt werden, daher erfordert jeder Dienst die Angabe des Containers image
. Dieses Beispiel verwendet das redis
-Containerimage und enthält Optionen für Integritätsüberprüfungen, um die Ausführung des Diensts sicherzustellen. Füge dem Imagenamen ein Tag an, um eine Version anzugeben, z. B. redis:6
. Weitere Informationen findest du im Redis-Image für Docker Hub.
jobs: # Label of the container job container-job: # Containers must run in Linux based operating systems runs-on: ubuntu-latest # Docker Hub image that `container-job` executes in container: node:20-bookworm-slim # Service containers to run with `container-job` services: # Label used to access the service container redis: # Docker Hub image image: redis # Set health checks to wait until redis has started options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
jobs:
# Label of the container job
container-job:
# Containers must run in Linux based operating systems
runs-on: ubuntu-latest
# Docker Hub image that `container-job` executes in
container: node:20-bookworm-slim
# Service containers to run with `container-job`
services:
# Label used to access the service container
redis:
# Docker Hub image
image: redis
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
Configuring the steps for the container job
Der Workflow führt die folgenden Schritte aus:
- Auschecken des Repository auf dem Läufer
- Installieren von Abhängigkeiten
- Ausführen eines Script, um einen Client zu erstellen
steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v4 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see http://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to Redis # Runs a script that creates a Redis client, populates # the client with data, and retrieves data run: node client.js # Environment variable used by the `client.js` script to create a new Redis client. env: # The hostname used to communicate with the Redis service container REDIS_HOST: redis # The default Redis port REDIS_PORT: 6379
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
uses: actions/checkout@v4
# Performs a clean installation of all dependencies in the `package.json` file
# For more information, see http://docs.npmjs.com/cli/ci.html
- name: Install dependencies
run: npm ci
- name: Connect to Redis
# Runs a script that creates a Redis client, populates
# the client with data, and retrieves data
run: node client.js
# Environment variable used by the `client.js` script to create a new Redis client.
env:
# The hostname used to communicate with the Redis service container
REDIS_HOST: redis
# The default Redis port
REDIS_PORT: 6379
Das client.js-Skript sucht nach den Umgebungsvariablen REDIS_HOST
und REDIS_PORT
, um den Client zu erstellen. Der Workflow legt diese beiden Umgebungsvariablen im Rahmen des Schritts „Mit Redis verbinden“ fest, um sie dem client.js-Skript zur Verfügung zu stellen. Weitere Informationen zum Skript findest du unter Testen des Redis-Dienstcontainers.
The 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.
Running jobs directly on the runner machine
When 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.
Du kannst diese Workflowdatei in das .github/workflows
-Verzeichnis deines Repositorys kopieren und nach Bedarf ändern.
name: Redis runner example on: push jobs: # Label of the runner job runner-job: # You must use a Linux environment when using service containers or container jobs runs-on: ubuntu-latest # Service containers to run with `runner-job` services: # Label used to access the service container redis: # Docker Hub image image: redis # Set health checks to wait until redis has started options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 ports: # Maps port 6379 on service container to the host - 6379:6379 steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v4 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see http://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to Redis # Runs a script that creates a Redis client, populates # the client with data, and retrieves data run: node client.js # Environment variable used by the `client.js` script to create # a new Redis client. env: # The hostname used to communicate with the Redis service container REDIS_HOST: localhost # The default Redis port REDIS_PORT: 6379
name: Redis runner example
on: push
jobs:
# Label of the runner job
runner-job:
# You must use a Linux environment when using service containers or container jobs
runs-on: ubuntu-latest
# Service containers to run with `runner-job`
services:
# Label used to access the service container
redis:
# Docker Hub image
image: redis
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps port 6379 on service container to the host
- 6379:6379
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
uses: actions/checkout@v4
# Performs a clean installation of all dependencies in the `package.json` file
# For more information, see http://docs.npmjs.com/cli/ci.html
- name: Install dependencies
run: npm ci
- name: Connect to Redis
# Runs a script that creates a Redis client, populates
# the client with data, and retrieves data
run: node client.js
# Environment variable used by the `client.js` script to create
# a new Redis client.
env:
# The hostname used to communicate with the Redis service container
REDIS_HOST: localhost
# The default Redis port
REDIS_PORT: 6379
Configuring the runner job
Im Beispiel wird der Runner ubuntu-latest
als Docker-Host verwendet.
Der Workflow konfiguriert einen Dienstcontainer mit der Bezeichnung redis
. Alle Dienste müssen in einem Container ausgeführt werden, daher erfordert jeder Dienst die Angabe des Containers image
. Dieses Beispiel verwendet das redis
-Containerimage und enthält Optionen für Integritätsüberprüfungen, um die Ausführung des Diensts sicherzustellen. Füge dem Imagenamen ein Tag an, um eine Version anzugeben, z. B. redis:6
. Weitere Informationen findest du im Redis-Image für Docker Hub.
The workflow maps port 6379 on the Redis service container to the Docker host. For more information about the ports
keyword, see Informationen zu Service-Containern.
jobs: # Label of the runner job runner-job: # You must use a Linux environment when using service containers or container jobs runs-on: ubuntu-latest # Service containers to run with `runner-job` services: # Label used to access the service container redis: # Docker Hub image image: redis # Set health checks to wait until redis has started options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 ports: # Maps port 6379 on service container to the host - 6379:6379
jobs:
# Label of the runner job
runner-job:
# You must use a Linux environment when using service containers or container jobs
runs-on: ubuntu-latest
# Service containers to run with `runner-job`
services:
# Label used to access the service container
redis:
# Docker Hub image
image: redis
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps port 6379 on service container to the host
- 6379:6379
Configuring the steps for the runner job
Der Workflow führt die folgenden Schritte aus:
- Auschecken des Repository auf dem Läufer
- Installieren von Abhängigkeiten
- Ausführen eines Script, um einen Client zu erstellen
steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v4 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see http://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to Redis # Runs a script that creates a Redis client, populates # the client with data, and retrieves data run: node client.js # Environment variable used by the `client.js` script to create # a new Redis client. env: # The hostname used to communicate with the Redis service container REDIS_HOST: localhost # The default Redis port REDIS_PORT: 6379
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
uses: actions/checkout@v4
# Performs a clean installation of all dependencies in the `package.json` file
# For more information, see http://docs.npmjs.com/cli/ci.html
- name: Install dependencies
run: npm ci
- name: Connect to Redis
# Runs a script that creates a Redis client, populates
# the client with data, and retrieves data
run: node client.js
# Environment variable used by the `client.js` script to create
# a new Redis client.
env:
# The hostname used to communicate with the Redis service container
REDIS_HOST: localhost
# The default Redis port
REDIS_PORT: 6379
Das client.js-Skript sucht nach den Umgebungsvariablen REDIS_HOST
und REDIS_PORT
, um den Client zu erstellen. Der Workflow legt diese beiden Umgebungsvariablen im Rahmen des Schritts „Mit Redis verbinden“ fest, um sie dem client.js-Skript zur Verfügung zu stellen. Weitere Informationen zum Skript findest du unter Testen des Redis-Dienstcontainers.
Der Hostname ist localhost
oder 127.0.0.1
.
Testing the Redis service container
You 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.
You 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.
Füge deinem Repository eine neue Datei namens client.js mit dem folgenden Code hinzu.
const redis = require("redis"); // Creates a new Redis client // If REDIS_HOST is not set, the default host is localhost // If REDIS_PORT is not set, the default port is 6379 const redisClient = redis.createClient({ url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}` }); redisClient.on("error", (err) => console.log("Error", err)); (async () => { await redisClient.connect(); // Sets the key "octocat" to a value of "Mona the octocat" const setKeyReply = await redisClient.set("octocat", "Mona the Octocat"); console.log("Reply: " + setKeyReply); // Sets a key to "species", field to "octocat", and "value" to "Cat and Octopus" const SetFieldOctocatReply = await redisClient.hSet("species", "octocat", "Cat and Octopus"); console.log("Reply: " + SetFieldOctocatReply); // Sets a key to "species", field to "dinotocat", and "value" to "Dinosaur and Octopus" const SetFieldDinotocatReply = await redisClient.hSet("species", "dinotocat", "Dinosaur and Octopus"); console.log("Reply: " + SetFieldDinotocatReply); // Sets a key to "species", field to "robotocat", and "value" to "Cat and Robot" const SetFieldRobotocatReply = await redisClient.hSet("species", "robotocat", "Cat and Robot"); console.log("Reply: " + SetFieldRobotocatReply); try { // Gets all fields in "species" key const replies = await redisClient.hKeys("species"); console.log(replies.length + " replies:"); replies.forEach((reply, i) => { console.log(" " + i + ": " + reply); }); await redisClient.quit(); } catch (err) { // statements to handle any exceptions } })();
const redis = require("redis");
// Creates a new Redis client
// If REDIS_HOST is not set, the default host is localhost
// If REDIS_PORT is not set, the default port is 6379
const redisClient = redis.createClient({
url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`
});
redisClient.on("error", (err) => console.log("Error", err));
(async () => {
await redisClient.connect();
// Sets the key "octocat" to a value of "Mona the octocat"
const setKeyReply = await redisClient.set("octocat", "Mona the Octocat");
console.log("Reply: " + setKeyReply);
// Sets a key to "species", field to "octocat", and "value" to "Cat and Octopus"
const SetFieldOctocatReply = await redisClient.hSet("species", "octocat", "Cat and Octopus");
console.log("Reply: " + SetFieldOctocatReply);
// Sets a key to "species", field to "dinotocat", and "value" to "Dinosaur and Octopus"
const SetFieldDinotocatReply = await redisClient.hSet("species", "dinotocat", "Dinosaur and Octopus");
console.log("Reply: " + SetFieldDinotocatReply);
// Sets a key to "species", field to "robotocat", and "value" to "Cat and Robot"
const SetFieldRobotocatReply = await redisClient.hSet("species", "robotocat", "Cat and Robot");
console.log("Reply: " + SetFieldRobotocatReply);
try {
// Gets all fields in "species" key
const replies = await redisClient.hKeys("species");
console.log(replies.length + " replies:");
replies.forEach((reply, i) => {
console.log(" " + i + ": " + reply);
});
await redisClient.quit();
}
catch (err) {
// statements to handle any exceptions
}
})();
The 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.
The 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.
When 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:
Reply: OK
Reply: 1
Reply: 1
Reply: 1
3 replies:
0: octocat
1: dinotocat
2: robotocat