Skip to content

Commit 21515a3

Browse files
committed
updated the docker example to propose several layouts
1 parent ab56eee commit 21515a3

File tree

42 files changed

+761
-107
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+761
-107
lines changed

.castor/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.castor.stub.php
2+
.env

.castor/castor.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
use Castor\Attribute\AsArgument;
4+
use Castor\Attribute\AsTask;
5+
use function Castor\context;
6+
use function Castor\finder;
7+
use function Castor\fs;
8+
use function Castor\request;
9+
use function Castor\io;
10+
use function Castor\run;
11+
12+
#[AsTask(name: 'run', description: 'Build, start and ensure examples are working as expected')]
13+
function _run(
14+
#[AsArgument(description: 'The name of the specific example to run', autocomplete: 'get_examples')]
15+
?string $example = null,
16+
): void
17+
{
18+
if (!fs()->exists('.env')) {
19+
io()->error('No .env file found. Please create one by copying the .env.dist file and filling in the required values.');
20+
return;
21+
}
22+
23+
stop();
24+
25+
if ($example) {
26+
io()->title("Building and running example: $example");
27+
28+
example($example);
29+
30+
return;
31+
}
32+
33+
io()->title('Building and running all examples');
34+
35+
foreach (get_examples() as $example) {
36+
example($example);
37+
}
38+
}
39+
40+
#[AsTask(description: 'Build the given example')]
41+
function build(
42+
#[AsArgument(description: 'The name of the specific example to run', autocomplete: 'get_examples')]
43+
?string $example = null,
44+
): void
45+
{
46+
if (!$example) {
47+
foreach (get_examples() as $example) {
48+
build($example);
49+
}
50+
return;
51+
}
52+
53+
io()->title("Building example: $example");
54+
55+
run('docker compose build', workingDirectory: $example);
56+
}
57+
58+
#[AsTask(description: 'Start the given example')]
59+
function start(
60+
#[AsArgument(description: 'The name of the specific example to run', autocomplete: 'get_examples')]
61+
string $example,
62+
): void
63+
{
64+
io()->title("Starting example: $example");
65+
66+
run('docker compose up -d', workingDirectory: $example);
67+
68+
sleep(2);
69+
}
70+
71+
#[AsTask(description: 'Test the given example')]
72+
function test(
73+
#[AsArgument(description: 'The name of the specific example to run', autocomplete: 'get_examples')]
74+
string $example,
75+
): void
76+
{
77+
io()->title("Testing example: $example");
78+
79+
$fails = 0;
80+
!assertResponse('/', 200) && $fails++;
81+
!assertResponse('/example/301-rio.html', 301) && $fails++;
82+
!assertResponse('/example/302-rio.html', 302) && $fails++;
83+
!assertResponse('/example/404-nginx.html', 404) && $fails++;
84+
!assertResponse('/example/404-rio.html', 404) && $fails++;
85+
!assertResponse('/example/410-rio.html', 410) && $fails++;
86+
87+
if ($fails > 0) {
88+
throw new \RuntimeException("Some tests were not successful for example \"$example\"");
89+
}
90+
91+
io()->writeln('');
92+
}
93+
94+
#[AsTask(description: 'Stop the given example')]
95+
function stop(
96+
#[AsArgument(description: 'The name of the specific example to run', autocomplete: 'get_examples')]
97+
?string $example = null,
98+
): void
99+
{
100+
if (!$example) {
101+
foreach (get_examples() as $example) {
102+
stop($example);
103+
}
104+
return;
105+
}
106+
107+
io()->title("Stopping example: $example");
108+
109+
run('docker compose stop', workingDirectory: $example, allowFailure: true);
110+
}
111+
112+
function get_examples(): iterable
113+
{
114+
/** @var Symfony\Component\Finder\Finder $examples */
115+
$examples = finder()
116+
->in(context()->workingDirectory)
117+
->notName('app')
118+
->directories()
119+
->depth(0)
120+
->sortByName()
121+
;
122+
123+
foreach ($examples as $example) {
124+
yield $example->getBasename();
125+
}
126+
}
127+
128+
function example(string $example): void
129+
{
130+
if (!fs()->exists("$example/docker-compose.yml")) {
131+
throw new \RuntimeException("The example directory \"$example\" does not exist or does not contain a docker-compose.yml file");
132+
}
133+
134+
build($example);
135+
start($example);
136+
test($example);
137+
stop($example);
138+
}
139+
140+
function assertResponse(string $url, int $expectedStatusCode): bool
141+
{
142+
$response = request('GET', 'http://127.0.0.1:8080' . $url, [
143+
'max_redirects' => 0,
144+
]);
145+
146+
$result = $response->getStatusCode() === $expectedStatusCode ? '✔️' : '';
147+
148+
io()->text("Asserting response for $url is status $expectedStatusCode: $result");
149+
150+
return $response->getStatusCode() === $expectedStatusCode;
151+
}

.env.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
REDIRECTIONIO_PROJECT_KEY=PASTE HERE YOUR REDIRECTION.IO PROJECT KEY

.github/workflows/ci.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Continuous Integration
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
schedule:
9+
- cron: "0 0 * * MON"
10+
11+
jobs:
12+
run-examples:
13+
name: Run all examples
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: 8.3
23+
tools: castor
24+
25+
- name: Setup .env
26+
run: "echo 'REDIRECTIONIO_PROJECT_KEY=${{ secrets.REDIRECTIONIO_PROJECT_KEY }}' > $GITHUB_WORKSPACE/.env"
27+
28+
- name: Build and run all examples
29+
run: castor run

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
.castor.stub.php

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2021 redirection.io
1+
Copyright (c) 2021-present redirection.io
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of
44
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# redirection.io nginx module Docker example
1+
# redirection.io Docker examples
22

33
It is quite common to use Docker in development or production environments. As
4-
this has been requested by several of our users, we have set up a short example
5-
of a Docker stack for the redirection.io agent and nginx module.
4+
this has been requested by several of our users, we have set up different examples
5+
to show how to integrate redirection.io in various Docker setups.
66

77
## Usage
88

@@ -11,71 +11,32 @@ of a Docker stack for the redirection.io agent and nginx module.
1111
git clone https://github.com/redirectionio/docker-example.git
1212
cd docker-example
1313
```
14-
* create an account and a project on [redirection.io](https://redirection.io), retrieve your `project key` in [redirection.io's manager](https://redirection.io/manager) and copy it in the nginx configuration file:
15-
* [for the apt-installed nginx module version](./services/nginx/etc/nginx/sites-enabled/default#L9)
16-
* [for the compiled nginx module version](./services/nginx-compiled/etc/nginx/nginx.conf#L35)
17-
```nginx
18-
redirectionio_project_key PUT HERE YOUR PROJECT KEY;
14+
* create an account and a project on [redirection.io](https://redirection.io), retrieve your `project key` in [redirection.io's manager](https://redirection.io/manager) and copy it
15+
* copy the `.env.dist` file to `.env` and paste your `project key` in it:
16+
```sh
17+
cp .env.dist .env
18+
```
19+
* choose one of the docker layouts (see below), and:
20+
```sh
21+
cd <your choice>
1922
```
20-
The `project key` can be found on the "Instances" screen of your project:
21-
simply click on "Setup on your infrastructure".
2223
* build the infrastructure:
2324
```sh
24-
docker-compose build
25+
docker compose build
2526
```
2627
* run it:
2728
```sh
28-
docker-compose up -d
29+
docker compose up -d
2930
```
31+
* open your browser and go to [http://localhost:8080/](http://localhost:8080/)
3032
31-
Head to:
32-
* [http://localhost:8080/](http://localhost:8080/) to use a nginx module installed from our packages repository.
33-
* [http://localhost:8081/](http://localhost:8081/) to use a nginx module compiled during the install.
34-
35-
## Explanations
36-
37-
The `service` directory contains three services:
38-
39-
* **redirectionio-agent**: a simple Dockerfile to get the agent running
40-
* **nginx**: a nginx Dockerfile based on the Ubuntu 20.04 image, with nginx and
41-
the redirection.io nginx module installed from our apt repository
42-
* **nginx-compiled**: a nginx Dockerfile based on the official nginx image,
43-
with all the directives to get the redirection.io nginx module built and
44-
loaded
45-
46-
Depending on your install requirements, the version of nginx that you are using
47-
and other specific nginx modules that you want to use, you can either use the
48-
`nginx` or the `nginx-compiled` services as examples.
49-
50-
### redirectionio-agent
51-
52-
The agent is installed using our [manual installation](https://redirection.io/documentation/developer-documentation/installation-of-the-agent#manual-installation) instructions. Note that we have enabled a `/var/lib/redirectionio` volume, used to store [redirection.io agent's cache data](https://redirection.io/documentation/developer-documentation/agent-configuration-reference#datadir).
53-
54-
### nginx
55-
56-
This is a standard Ubuntu 20.04 image, with the distribution-provided nginx package, and [libnginx-mod-redirectionio installed from our deb repository, as explained in our documentation](https://redirection.io/documentation/developer-documentation/nginx-module#debian-and-apt-based-distributions).
57-
58-
It defines a single VirtualHost, [for which redirection is enabled](./services/nginx/etc/nginx/sites-enabled/default#L8-L9).
59-
60-
### nginx-compiled
61-
62-
Nginx dynamic modules require binary compatibility to be properly loaded, which
63-
means that they have to be compiled with the exact same configuration directives
64-
like your `nginx` binary.
65-
66-
redirection.io offers APT and RPM repositories, with many versions of
67-
`libnginx-mod-redirectionio` to match classical distribution nginx packages.
68-
However, should your nginx install vary from these traditional layouts, you will
69-
be forced to compile our nginx module yourself, to match your own nginx version.
33+
## Available Docker layouts
7034
71-
This is what the [nginx Dockerfile](./services/nginx-compiled/Dockerfile) achieves. Basically:
72-
* it downloads the nginx sources in the same version like the installed `nginx` binary
73-
* it downloads and build the libredirectionio
74-
* it downloads the redirection.io nginx module
75-
* it builds this module with the same configure arguments the installed `nginx` binary was configured
76-
* it moves the build module in the right folder
77-
* it [loads the module](./services/nginx-compiled/etc/nginx/nginx.conf#L7) in the nginx configuration
78-
* it [enables redirection.io for the server](./services/nginx-compiled/etc/nginx/nginx.conf#L34-L35)
35+
* [**agent-as-reverse-proxy**](./agent-as-reverse-proxy/README.md): the redirection.io agent, installed from our repository, is used as a reverse proxy. This is the most simple and recommended setup.
36+
* [**apache-module**](./apache-module/README.md): a simple Apache setup, with redirection.io module installed from our apt repository
37+
* [**apache-module-custom**](./apache-module-custom/README.md): an Apache setup with the redirection.io module compiled from sources
38+
* [**nginx-module**](./nginx-module/README.md): a simple nginx setup, with redirection.io module installed from our apt repository
39+
* [**nginx-module-custom**](./nginx-module-custom/README.md): a nginx setup with the redirection.io module compiled from sources
7940
8041
## Help and troubleshooting
8142

agent-as-reverse-proxy/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Using the redirection.io agent as a reverse proxy in a Docker environment
2+
3+
This example shows how to use the redirection.io agent as a reverse proxy in a Docker environment. It is the most simple and versatile way to install redirection.io in your Docker stack, as it can be used with any backend service. The redirection.io agent act as a very fast reverse proxy, intercepting requests and responses between the client and the backend service.
4+
5+
> [!TIP]
6+
> You can read more about [the redirection.io agent as a reverse proxy in our documentation](https://redirection.io/documentation/developer-documentation/the-agent-as-a-reverse-proxy).
7+
8+
## Description
9+
10+
The `service` directory contains the **redirectionio-agent** service: a simple Dockerfile to get the agent running. The [`/etc/redirectionio/agent.yml`](./services/redirectionio-agent/etc/redirectionio/agent.yml) file is used to configure the agent and proxify the traffic to a backend service.
11+
12+
The `docker-compose.yml` file also mentions a `nginx` service, which is a simple Nginx server used as a backend service. You can replace it with your own backend service.
13+
14+
![The redirection.io agent as a reverse proxy](../app/agent-as-reverse-proxy.png)
15+
16+
### redirectionio-agent
17+
18+
The agent is installed using our [manual installation](https://redirection.io/documentation/developer-documentation/installation-of-the-agent#manual-installation) instructions. Note that we have enabled a `/var/lib/redirectionio` volume, used to store [redirection.io agent's cache data](https://redirection.io/documentation/developer-documentation/agent-configuration-reference#datadir).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: '3'
2+
3+
volumes:
4+
redirectionio-agent-data: {}
5+
6+
services:
7+
redirectionio-agent:
8+
build: services/redirectionio-agent
9+
env_file: "../.env"
10+
environment:
11+
- INSTANCE_NAME=docker-reverse-proxy
12+
ports:
13+
- "8080:80"
14+
volumes:
15+
- redirectionio-agent-data:/var/lib/redirectionio
16+
17+
# this is the application backend service
18+
# it can be any other HTTP service
19+
nginx:
20+
image: nginx
21+
volumes:
22+
- ../app:/usr/share/nginx/html

services/redirectionio-agent/Dockerfile renamed to agent-as-reverse-proxy/services/redirectionio-agent/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.12 as alpine
1+
FROM alpine:3.19 as alpine
22

33
WORKDIR /tmp
44

0 commit comments

Comments
 (0)