diff --git a/TODO.md b/TODO.md index 1fbff205..9727497a 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,4 @@ - [ ] Docs - [ ] Test with helm chart - [ ] Play With Docker generation -- [ ] Custom app compose and dockerfile examples +- [x] Custom app compose and dockerfile examples diff --git a/build/nginx/Dockerfile b/build/nginx/Dockerfile index ac236016..db062b05 100644 --- a/build/nginx/Dockerfile +++ b/build/nginx/Dockerfile @@ -33,7 +33,7 @@ RUN cd apps/frappe && \ fi \ && yarn -# Build assets stored in frappe-bench/sites/assets +# Build assets they're stored in frappe-bench/sites/assets RUN echo "frappe" >sites/apps.txt \ && yarn --cwd apps/frappe run production \ && rm sites/apps.txt diff --git a/custom_app/README.md b/custom_app/README.md new file mode 100644 index 00000000..7ebd919c --- /dev/null +++ b/custom_app/README.md @@ -0,0 +1,38 @@ +This is basic configuration for building images and testing custom apps that use Frappe. + +You can see that there's four files in this folder: + +- `backend.Dockerfile`, +- `frontend.Dockerfile`, +- `docker-bake.hcl`, +- `compose.override.yaml`. + +Python code will `backend.Dockerfile`. JS and CSS (and other fancy frontend stuff) files will be built in `frontend.Dockerfile` if required and served from there. + +`docker-bake.hcl` is reference file for cool new [Buildx Bake](https://github.com/docker/buildx/blob/master/docs/reference/buildx_bake.md). It helps to build images without having to remember all build arguments. + +`compose.override.yaml` is [Compose](https://docs.docker.com/compose/compose-file/) override that replaces images from [main compose file](https://github.com/frappe/frappe_docker/blob/main/compose.yaml) so it would use your own images. + +To get started, install Docker and [Buildx](https://github.com/docker/buildx#installing). Then copy all content of this folder (except this README) to your app's root directory. Also copy `compose.yaml` in the root of this repository. + +Before the next step—to build images—replace "custom_app" with your app's name in `docker-bake.hcl`. After that, let's try to build: + +```bash +FRAPPE_VERSION= docker buildx bake +``` + +If something goes wrong feel free to leave an issue. + +To test if site works, setup `.env` file (check [example](<(https://github.com/frappe/frappe_docker/blob/main/example.env)>)) and run: + +```bash +docker-compose up -d +docker-compose exec backend \ + bench new-site 127.0.0.1 \ + --mariadb-root-password 123 \ + --admin-password admin \ + --install-app +docker-compose restart backend +``` + +Cool! You just containerized your app! diff --git a/custom_app/backend.Dockerfile b/custom_app/backend.Dockerfile new file mode 100644 index 00000000..45a7e379 --- /dev/null +++ b/custom_app/backend.Dockerfile @@ -0,0 +1,8 @@ +ARG FRAPPE_VERSION +FROM frappe/frappe-worker:${FRAPPE_VERSION} + +ARG APP_NAME +COPY --chown=frappe . ../apps/${APP_NAME} + +RUN echo "frappe\ncomfort" >/home/frappe/frappe-bench/sites/apps.txt \ + && ../env/bin/pip install --no-cache-dir -e ../apps/${APP_NAME} diff --git a/custom_app/compose.override.yaml b/custom_app/compose.override.yaml new file mode 100644 index 00000000..002e0795 --- /dev/null +++ b/custom_app/compose.override.yaml @@ -0,0 +1,21 @@ +services: + configurator: + image: custom_app/worker:${VERSION} + + backend: + image: custom_app/worker:${VERSION} + + frontend: + image: custom_app/nginx:${VERSION} + + queue-short: + image: custom_app/worker:${VERSION} + + queue-default: + image: custom_app/worker:${VERSION} + + queue-long: + image: custom_app/worker:${VERSION} + + scheduler: + image: custom_app/worker:${VERSION} diff --git a/custom_app/docker-bake.hcl b/custom_app/docker-bake.hcl new file mode 100644 index 00000000..12aa7d0e --- /dev/null +++ b/custom_app/docker-bake.hcl @@ -0,0 +1,25 @@ +APP_NAME="custom_app" + +variable "FRAPPE_VERSION" {} + +group "default" { + targets = ["backend", "frontend"] +} + +target "backend" { + dockerfile = "backend.Dockerfile" + tags = ["custom_app/worker:latest"] + args = { + "FRAPPE_VERSION" = FRAPPE_VERSION + "APP_NAME" = APP_NAME + } +} + +target "frontend" { + dockerfile = "frontend.Dockerfile" + tags = ["custom_app/nginx:latest"] + args = { + "FRAPPE_VERSION" = FRAPPE_VERSION + "APP_NAME" = APP_NAME + } +} diff --git a/custom_app/frontend.Dockerfile b/custom_app/frontend.Dockerfile new file mode 100644 index 00000000..1d6e8f2b --- /dev/null +++ b/custom_app/frontend.Dockerfile @@ -0,0 +1,57 @@ +ARG FRAPPE_VERSION +FROM node:14-bullseye-slim as prod_node_modules + +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ + git \ + build-essential \ + python \ + ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /root/frappe-bench +RUN mkdir -p sites/assets + +ARG FRAPPE_VERSION +RUN git clone --depth 1 -b ${FRAPPE_VERSION} https://github.com/frappe/frappe apps/frappe + +RUN cd apps/frappe \ + && if [ "$(uname -m)" = "aarch64" ]; then \ + yarn remove svg-sprite || true \ + && yarn add sass; \ + fi \ + && yarn + + +ARG APP_NAME +COPY . apps/${APP_NAME} + +# Install production node modules +RUN yarn --cwd apps/${APP_NAME} --prod + + + +FROM prod_node_modules as assets + +ARG APP_NAME + +# Install development node modules +RUN yarn --cwd apps/${APP_NAME} + +# Build assets +RUN echo "frappe\n${APP_NAME}" >sites/apps.txt \ + && yarn --cwd apps/frappe production --app ${APP_NAME} \ + && rm sites/apps.txt + + + +FROM frappe/frappe-nginx:${FRAPPE_VERSION} + +ARG APP_NAME + +# Copy all not built assets +COPY --from=prod_node_modules /root/frappe-bench/apps/${APP_NAME}/${APP_NAME}/public /usr/share/nginx/html/assets/${APP_NAME} +# Copy production node modules +COPY --from=prod_node_modules /root/frappe-bench/apps/${APP_NAME}/node_modules /usr/share/nginx/html/assets/${APP_NAME}/node_modules +# Copy built assets +COPY --from=assets /root/frappe-bench/sites /usr/share/nginx/html