Skip to content

Commit 19efd5e

Browse files
committed
chore(doc): add documentation about cache
1 parent f6ae602 commit 19efd5e

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

.castor/docker.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ function push(): void
304304

305305
$targets = [];
306306

307+
/** @var string|null $registry */
308+
$registry = variable('registry');
309+
310+
if ($registry === null || $registry === '') {
311+
throw new \RuntimeException('You must define a registry to push images.');
312+
}
313+
307314
foreach ($composeFile as $file) {
308315
$path = variable('root_dir') . '/infrastructure/docker/' . $file;
309316
$content = file_get_contents($path);
@@ -351,9 +358,6 @@ function push(): void
351358
}
352359
}
353360

354-
/** @var string|null $registry */
355-
$registry = variable('registry');
356-
357361
$content = sprintf(<<<EOHCL
358362
group "default" {
359363
targets = [%s]
@@ -365,7 +369,7 @@ function push(): void
365369

366370

367371
foreach ($targets as $target) {
368-
$reference = str_replace('${REGISTRY:-}', $registry ?? '', $target['reference'] ?? '');
372+
$reference = str_replace('${REGISTRY:-}', $registry, $target['reference'] ?? '');
369373

370374
$content .= sprintf(<<<EOHCL
371375
target "%s" {

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,106 @@ services:
958958

959959
</details>
960960

961+
### How to use a docker registry to cache images layer
962+
963+
<details>
964+
965+
<summary>Read the cookbook</summary>
966+
967+
You can use a docker registry to cache images layer, it can be useful to speed up the build process during the CI and
968+
local development.
969+
970+
First you need a docker registry, in following examples we will use the GitHub registry (ghcr.io).
971+
972+
Then add the registry to the context variable of the `castor.php` file:
973+
974+
```php
975+
function create_default_variables(): Context
976+
{
977+
return [
978+
// [...]
979+
'registry' => 'ghcr.io/your-organization/your-project',
980+
];
981+
}
982+
```
983+
984+
Once you have the registry, you can push the images to the registry:
985+
986+
```bash
987+
castor docker:push
988+
```
989+
990+
This command will generate a bake file with the images to push from the `cache_from` directive of the `docker-compose.yml` file.
991+
If you want to add more images to push, you can add the `cache_from` directive to them.
992+
993+
```yaml
994+
services:
995+
my-service:
996+
build:
997+
cache_from:
998+
- "type=registry,ref=${REGISTRY:-}/my-service:cache"
999+
```
1000+
</details>
1001+
1002+
### How to use cached images in a GitHub action
1003+
1004+
<details>
1005+
1006+
<summary>Read the cookbook</summary>
1007+
1008+
If you are using a GitHub action to build your images, you can use the cached images from the registry to speed up the build process.
1009+
However there are few steps to make it works due to the docker binary limitations in GitHub actions.
1010+
1011+
#### Pushing images to the registry
1012+
1013+
To push images to the registry in a github action you will need to do this :
1014+
1015+
1. Ensure that the github token have the `write:packages` scope.
1016+
2. Install Docker buildx in the github action
1017+
1018+
```yaml
1019+
- name: Set up Docker Buildx
1020+
uses: docker/setup-buildx-action@v2
1021+
```
1022+
1023+
3. Login to the registry
1024+
1025+
```yaml
1026+
- name: Log in to registry
1027+
shell: bash
1028+
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
1029+
```
1030+
1031+
#### Using the cached images
1032+
1033+
Images built when using the Docker Buildx will have a different hash than the one built with the classic Docker build.
1034+
Then you will need to use a more recent version of the Docker binary to use the cached images by either:
1035+
1036+
* Use buildx in each GitHub action workflow step
1037+
1038+
```yaml
1039+
- name: Set up Docker Buildx
1040+
uses: docker/setup-buildx-action@v2
1041+
```
1042+
1043+
* Use the official Docker binary
1044+
```yaml
1045+
# Install official version of docker that correctly supports from-cache option in docker compose
1046+
- name: Set up Docker
1047+
uses: crazy-max/ghaction-setup-docker@v3
1048+
with:
1049+
set-host: true
1050+
1051+
# Docker socket path is different when using setup docker
1052+
- name: Set Docker Socket Host
1053+
run: echo "DOCKER_SOCKET_PATH=${DOCKER_HOST:5}" >> $GITHUB_ENV
1054+
```
1055+
1056+
The second option is faster (there is no need to transfer images between buildx and local docker), but it is not
1057+
officially supported by GitHub actions and may break in the future.
1058+
1059+
</details>
1060+
9611061
## Credits
9621062

9631063
- Created at [JoliCode](https://jolicode.com/)

0 commit comments

Comments
 (0)