@@ -958,6 +958,106 @@ services:
958
958
959
959
</details>
960
960
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
+
961
1061
# # Credits
962
1062
963
1063
- Created at [JoliCode](https://jolicode.com/)
0 commit comments