From aea3b9ddaf0595875316303018caa0672bed1a7c Mon Sep 17 00:00:00 2001
From: Juan Picado
Date: Tue, 14 Jun 2022 18:33:27 +0200
Subject: [PATCH] add docker examples to use plugins with docker files (#3217)
* add docker examples to use plugins at docker
* Update Dockerfile
* chore: add v6 examples
---
.eslintignore | 1 +
docker-examples/v5/README.md | 9 +
.../docker-build-install-plugin/Dockerfile | 25 ++
.../docker-build-install-plugin/README.md | 46 ++++
.../docker-build-install-plugin/docker.yaml | 197 ++++++++++++++++
.../v5/plugins/docker-local-plugin/Dockerfile | 26 +++
.../v5/plugins/docker-local-plugin/README.md | 42 ++++
.../plugins/docker-local-plugin/docker.yaml | 199 ++++++++++++++++
.../docker-local-plugin/plugins/.eslintrc | 8 +
.../verdaccio-docker-memory/lib/index.js | 17 ++
.../lib/local-memory.js | 96 ++++++++
.../lib/memory-handler.js | 182 +++++++++++++++
.../verdaccio-docker-memory/package.json | 19 ++
docker-examples/v6/README.md | 22 +-
.../docker-build-install-plugin/Dockerfile | 25 ++
.../docker-build-install-plugin/README.md | 46 ++++
.../docker-build-install-plugin/docker.yaml | 197 ++++++++++++++++
.../v6/plugins/docker-local-plugin/Dockerfile | 26 +++
.../v6/plugins/docker-local-plugin/README.md | 42 ++++
.../plugins/docker-local-plugin/docker.yaml | 199 ++++++++++++++++
.../docker-local-plugin/plugins/.eslintrc | 8 +
.../verdaccio-docker-memory/lib/index.js | 22 ++
.../lib/local-memory.js | 141 ++++++++++++
.../lib/memory-handler.js | 214 ++++++++++++++++++
.../verdaccio-docker-memory/lib/utils.js | 16 ++
.../verdaccio-docker-memory/package.json | 56 +++++
.../v6/{ => proxy}/apache-verdaccio/README.md | 0
.../apache-verdaccio/apache_proxy/Dockerfile | 0
.../apache_proxy/conf/000-default.conf | 0
.../apache_proxy/conf/env.load | 0
.../apache-verdaccio/docker-compose.yaml | 0
.../https-portal-example/README.md | 0
.../https-portal-example/conf/config.yaml | 0
.../https-portal-example/conf/htpasswd | 0
.../https-portal-example/docker-compose.yml | 0
.../storage/@scope/example/example-1.0.0.tgz | Bin
.../storage/@scope/example/package.json | 0
.../storage/jquery/package.json | 0
website/docs/programmatically.md | 2 +-
39 files changed, 1878 insertions(+), 5 deletions(-)
create mode 100644 docker-examples/v5/plugins/docker-build-install-plugin/Dockerfile
create mode 100644 docker-examples/v5/plugins/docker-build-install-plugin/README.md
create mode 100644 docker-examples/v5/plugins/docker-build-install-plugin/docker.yaml
create mode 100644 docker-examples/v5/plugins/docker-local-plugin/Dockerfile
create mode 100644 docker-examples/v5/plugins/docker-local-plugin/README.md
create mode 100644 docker-examples/v5/plugins/docker-local-plugin/docker.yaml
create mode 100644 docker-examples/v5/plugins/docker-local-plugin/plugins/.eslintrc
create mode 100644 docker-examples/v5/plugins/docker-local-plugin/plugins/verdaccio-docker-memory/lib/index.js
create mode 100644 docker-examples/v5/plugins/docker-local-plugin/plugins/verdaccio-docker-memory/lib/local-memory.js
create mode 100644 docker-examples/v5/plugins/docker-local-plugin/plugins/verdaccio-docker-memory/lib/memory-handler.js
create mode 100644 docker-examples/v5/plugins/docker-local-plugin/plugins/verdaccio-docker-memory/package.json
create mode 100644 docker-examples/v6/plugins/docker-build-install-plugin/Dockerfile
create mode 100644 docker-examples/v6/plugins/docker-build-install-plugin/README.md
create mode 100644 docker-examples/v6/plugins/docker-build-install-plugin/docker.yaml
create mode 100644 docker-examples/v6/plugins/docker-local-plugin/Dockerfile
create mode 100644 docker-examples/v6/plugins/docker-local-plugin/README.md
create mode 100644 docker-examples/v6/plugins/docker-local-plugin/docker.yaml
create mode 100644 docker-examples/v6/plugins/docker-local-plugin/plugins/.eslintrc
create mode 100644 docker-examples/v6/plugins/docker-local-plugin/plugins/verdaccio-docker-memory/lib/index.js
create mode 100644 docker-examples/v6/plugins/docker-local-plugin/plugins/verdaccio-docker-memory/lib/local-memory.js
create mode 100644 docker-examples/v6/plugins/docker-local-plugin/plugins/verdaccio-docker-memory/lib/memory-handler.js
create mode 100644 docker-examples/v6/plugins/docker-local-plugin/plugins/verdaccio-docker-memory/lib/utils.js
create mode 100644 docker-examples/v6/plugins/docker-local-plugin/plugins/verdaccio-docker-memory/package.json
rename docker-examples/v6/{ => proxy}/apache-verdaccio/README.md (100%)
rename docker-examples/v6/{ => proxy}/apache-verdaccio/apache_proxy/Dockerfile (100%)
rename docker-examples/v6/{ => proxy}/apache-verdaccio/apache_proxy/conf/000-default.conf (100%)
rename docker-examples/v6/{ => proxy}/apache-verdaccio/apache_proxy/conf/env.load (100%)
rename docker-examples/v6/{ => proxy}/apache-verdaccio/docker-compose.yaml (100%)
rename docker-examples/v6/{ => proxy}/https-portal-example/README.md (100%)
rename docker-examples/v6/{ => proxy}/https-portal-example/conf/config.yaml (100%)
rename docker-examples/v6/{ => proxy}/https-portal-example/conf/htpasswd (100%)
rename docker-examples/v6/{ => proxy}/https-portal-example/docker-compose.yml (100%)
rename docker-examples/v6/{ => proxy}/https-portal-example/storage/@scope/example/example-1.0.0.tgz (100%)
rename docker-examples/v6/{ => proxy}/https-portal-example/storage/@scope/example/package.json (100%)
rename docker-examples/v6/{ => proxy}/https-portal-example/storage/jquery/package.json (100%)
diff --git a/.eslintignore b/.eslintignore
index cd0aa034e..af1589dea 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -11,3 +11,4 @@ wiki/
dist/
docs/
test/functional/store/*
+docker-examples/**/lib/**/*.js
diff --git a/docker-examples/v5/README.md b/docker-examples/v5/README.md
index 6938440b7..3bdd54cd7 100644
--- a/docker-examples/v5/README.md
+++ b/docker-examples/v5/README.md
@@ -3,3 +3,12 @@
> Before run examples, build the local image by running `pnpm docker`.
- [Docker + Nginx + Verdaccio](reverse_proxy/nginx/README.md)
+
+## Using Plugins with Docker
+
+List of different approaches
+
+> Note these options could be improved, feel free to submit upgrades
+
+- [Docker + Install plugins from a registry](plugins/docker-build-install-plugin/README.md)
+- [Docker + Install local plugin](plugins/docker-local-plugin/README.md)
diff --git a/docker-examples/v5/plugins/docker-build-install-plugin/Dockerfile b/docker-examples/v5/plugins/docker-build-install-plugin/Dockerfile
new file mode 100644
index 000000000..38c2eb238
--- /dev/null
+++ b/docker-examples/v5/plugins/docker-build-install-plugin/Dockerfile
@@ -0,0 +1,25 @@
+# Docs based on https://github.com/xlts-dev/verdaccio-prometheus-middleware#installation
+
+# Docker multi-stage build - https://docs.docker.com/develop/develop-images/multistage-build/
+# Use an alpine node image to install the plugin
+FROM node:lts-alpine as builder
+
+# Install the metrics middleware plugin
+# npm docs
+# --global-style https://docs.npmjs.com/cli/v7/commands/npm-install#global-style
+# --no-bin-links https://docs.npmjs.com/cli/v7/commands/npm-install#bin-links
+# --omit=optional
+RUN mkdir -p /verdaccio/plugins \
+ && cd /verdaccio/plugins \
+ && npm install --global-style --no-bin-links --omit=optional verdaccio-auth-memory@latest
+
+# The final built image will be based on the standard Verdaccio docker image.
+FROM verdaccio/verdaccio:5
+
+# Copy the plugin files over from the 'builder' node image.
+# The `$VERDACCIO_USER_UID` env variable is defined in the base `verdaccio/verdaccio` image.
+# Refer to: https://github.com/verdaccio/verdaccio/blob/v5.2.0/Dockerfile#L32
+ADD docker.yaml /verdaccio/conf/config.yaml
+COPY --chown=$VERDACCIO_USER_UID:root --from=builder \
+ /verdaccio/plugins/node_modules/verdaccio-auth-memory \
+ /verdaccio/plugins/verdaccio-auth-memory
diff --git a/docker-examples/v5/plugins/docker-build-install-plugin/README.md b/docker-examples/v5/plugins/docker-build-install-plugin/README.md
new file mode 100644
index 000000000..2f440cff4
--- /dev/null
+++ b/docker-examples/v5/plugins/docker-build-install-plugin/README.md
@@ -0,0 +1,46 @@
+# Installing a plugin with Docker build
+
+On this small tutorial (based on [`verdaccio-prometheus-middleware`](https://github.com/xlts-dev/verdaccio-prometheus-middleware) example) you will be able to use a published package in any random registry (npmjs by default) and use it withing a docker image without mapping need it.
+
+> Since verdaccio:5 uses `yarn@2` to run the application, this tutorial is a workaround but future prove since verdaccio 6 uses `pnpm` to build the docker image.
+
+There are two main steps to highlight:
+
+- `docker.yaml`: This is a copy of the original configuration file for docker and with small modifications to use the plugin [`verdaccio-auth-memory`](https://www.npmjs.com/package/verdaccio-auth-memory) and custom web title for demonstration.
+- The `Dockerfile` take advance of the docker multi-stage build to install the plugin into the `verdaccio/plugins` folder withing the image, then we apply the right permissions `--chown=$VERDACCIO_USER_UID:root` so the plugin is recognized.
+
+## Run it
+
+Build this image.
+
+```bash
+docker build -t verdaccio/verdaccio:local .
+```
+
+and to run it
+
+```bash
+docker run -it --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio:local
+```
+
+## Usage
+
+```dockerfile
+# Docker multi-stage build - https://docs.docker.com/develop/develop-images/multistage-build/
+# Use an alpine node image to install the plugin
+FROM node:lts-alpine as builder
+
+RUN mkdir -p /verdaccio/plugins \
+ && cd /verdaccio/plugins \
+ && npm install --global-style --no-bin-links --omit=optional verdaccio-auth-memory@latest
+
+FROM verdaccio/verdaccio:5
+
+# copy your modified config.yaml into the image
+ADD docker.yaml /verdaccio/conf/config.yaml
+
+COPY --chown=$VERDACCIO_USER_UID:root --from=builder \
+ /verdaccio/plugins/node_modules/verdaccio-auth-memory \
+ /verdaccio/plugins/verdaccio-auth-memory
+
+```
diff --git a/docker-examples/v5/plugins/docker-build-install-plugin/docker.yaml b/docker-examples/v5/plugins/docker-build-install-plugin/docker.yaml
new file mode 100644
index 000000000..3bc70a146
--- /dev/null
+++ b/docker-examples/v5/plugins/docker-build-install-plugin/docker.yaml
@@ -0,0 +1,197 @@
+#
+# This is the default configuration file. It allows all users to do anything,
+# please read carefully the documentation and best practices to
+# improve security.
+#
+# Do not configure host and port under `listen` in this file
+# as it will be ignored when using docker.
+# see https://verdaccio.org/docs/en/docker#docker-and-custom-port-configuration
+#
+# Look here for more config file examples:
+# https://github.com/verdaccio/verdaccio/tree/5.x/conf
+#
+# Read about the best practices
+# https://verdaccio.org/docs/best
+
+# path to a directory with all packages
+storage: /verdaccio/storage/data
+# path to a directory with plugins to include
+plugins: /verdaccio/plugins
+
+# https://verdaccio.org/docs/webui
+web:
+ title: Verdaccio Publish Config Test
+ # comment out to disable gravatar support
+ # gravatar: false
+ # by default packages are ordercer ascendant (asc|desc)
+ # sort_packages: asc
+ # convert your UI to the dark side
+ # darkMode: true
+ # html_cache: true
+ # by default all features are displayed
+ # login: true
+ # showInfo: true
+ # showSettings: true
+ # In combination with darkMode you can force specific theme
+ # showThemeSwitch: true
+ # showFooter: true
+ # showSearch: true
+ # showRaw: true
+ # showDownloadTarball: true
+ # HTML tags injected after manifest
+ # scriptsBodyAfter:
+ # - ''
+ # HTML tags injected before ends
+ # metaScripts:
+ # - ''
+ # - ''
+ # - ''
+ # HTML tags injected first child at
+ # bodyBefore:
+ # - '
html before webpack scripts
'
+ # Public path for template manifest scripts (only manifest)
+ # publicPath: http://somedomain.org/
+
+# https://verdaccio.org/docs/configuration#authentication
+auth:
+ auth-memory:
+ users:
+ foo:
+ name: foo
+ password: s3cret
+ bar:
+ name: bar
+ password: s3cret
+
+# https://verdaccio.org/docs/configuration#uplinks
+# a list of other known repositories we can talk to
+uplinks:
+ npmjs:
+ url: https://registry.npmjs.org/
+
+# Learn how to protect your packages
+# https://verdaccio.org/docs/protect-your-dependencies/
+# https://verdaccio.org/docs/configuration#packages
+packages:
+ '@*/*':
+ # scoped packages
+ access: $all
+ publish: $authenticated
+ unpublish: $authenticated
+ proxy: npmjs
+
+ '**':
+ # allow all users (including non-authenticated users) to read and
+ # publish all packages
+ #
+ # you can specify usernames/groupnames (depending on your auth plugin)
+ # and three keywords: "$all", "$anonymous", "$authenticated"
+ access: $all
+
+ # allow all known users to publish/publish packages
+ # (anyone can register by default, remember?)
+ publish: $authenticated
+ unpublish: $authenticated
+
+ # if package is not available locally, proxy requests to 'npmjs' registry
+ proxy: npmjs
+
+# To improve your security configuration and avoid dependency confusion
+# consider removing the proxy property for private packages
+# https://verdaccio.org/docs/best#remove-proxy-to-increase-security-at-private-packages
+
+# https://verdaccio.org/docs/configuration#server
+# You can specify HTTP/1.1 server keep alive timeout in seconds for incoming connections.
+# A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.
+# WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enough.
+server:
+ keepAliveTimeout: 60
+
+# https://verdaccio.org/docs/configuration#offline-publish
+# publish:
+# allow_offline: false
+
+# https://verdaccio.org/docs/configuration#url-prefix
+# url_prefix: /verdaccio/
+# VERDACCIO_PUBLIC_URL='https://somedomain.org';
+# url_prefix: '/my_prefix'
+# // url -> https://somedomain.org/my_prefix/
+# VERDACCIO_PUBLIC_URL='https://somedomain.org';
+# url_prefix: '/'
+# // url -> https://somedomain.org/
+# VERDACCIO_PUBLIC_URL='https://somedomain.org/first_prefix';
+# url_prefix: '/second_prefix'
+# // url -> https://somedomain.org/second_prefix/'
+
+# https://verdaccio.org/docs/configuration#security
+# security:
+# api:
+# legacy: true
+# jwt:
+# sign:
+# expiresIn: 29d
+# verify:
+# someProp: [value]
+# web:
+# sign:
+# expiresIn: 1h # 1 hour by default
+# verify:
+# someProp: [value]
+
+# https://verdaccio.org/docs/configuration#user-rate-limit
+# userRateLimit:
+# windowMs: 50000
+# max: 1000
+
+# https://verdaccio.org/docs/configuration#max-body-size
+# max_body_size: 10mb
+
+# https://verdaccio.org/docs/configuration#listen-port
+# listen:
+# - localhost:4873 # default value
+# - http://localhost:4873 # same thing
+# - 0.0.0.0:4873 # listen on all addresses (INADDR_ANY)
+# - https://example.org:4873 # if you want to use https
+# - "[::1]:4873" # ipv6
+# - unix:/tmp/verdaccio.sock # unix socket
+
+# The HTTPS configuration is useful if you do not consider use a HTTP Proxy
+# https://verdaccio.org/docs/configuration#https
+# https:
+# key: ./path/verdaccio-key.pem
+# cert: ./path/verdaccio-cert.pem
+# ca: ./path/verdaccio-csr.pem
+
+# https://verdaccio.org/docs/configuration#proxy
+# http_proxy: http://something.local/
+# https_proxy: https://something.local/
+
+# https://verdaccio.org/docs/configuration#notifications
+# notify:
+# method: POST
+# headers: [{ "Content-Type": "application/json" }]
+# endpoint: https://usagge.hipchat.com/v2/room/3729485/notification?auth_token=mySecretToken
+# content: '{"color":"green","message":"New package published: * {{ name }}*","notify":true,"message_format":"text"}'
+
+middlewares:
+ audit:
+ enabled: true
+
+# https://verdaccio.org/docs/logger
+# log settings
+logs: { type: stdout, format: pretty, level: http }
+#experiments:
+# # support for npm token command
+# token: false
+# # enable tarball URL redirect for hosting tarball with a different server, the tarball_url_redirect can be a template string
+# tarball_url_redirect: 'https://mycdn.com/verdaccio/${packageName}/${filename}'
+# # the tarball_url_redirect can be a function, takes packageName and filename and returns the url, when working with a js configuration file
+# tarball_url_redirect(packageName, filename) {
+# const signedUrl = // generate a signed url
+# return signedUrl;
+# }
+
+# translate your registry, api i18n not available yet
+# i18n:
+# list of the available translations https://github.com/verdaccio/verdaccio/blob/master/packages/plugins/ui-theme/src/i18n/ABOUT_TRANSLATIONS.md
+# web: en-US
diff --git a/docker-examples/v5/plugins/docker-local-plugin/Dockerfile b/docker-examples/v5/plugins/docker-local-plugin/Dockerfile
new file mode 100644
index 000000000..1c9d67723
--- /dev/null
+++ b/docker-examples/v5/plugins/docker-local-plugin/Dockerfile
@@ -0,0 +1,26 @@
+# Docs based on https://github.com/xlts-dev/verdaccio-prometheus-middleware#installation
+# Docker multi-stage build - https://docs.docker.com/develop/develop-images/multistage-build/
+# Use an alpine node image to install the plugin
+FROM node:lts-alpine as builder
+
+RUN mkdir -p /verdaccio/plugins
+
+# Copy the local plugin into the docker image
+ADD plugins/verdaccio-docker-memory /verdaccio/plugins/verdaccio-docker-memory
+# Install the production dependencies (be careful install devDependencies here)
+RUN cd /verdaccio/plugins/verdaccio-docker-memory \
+ && npm install --production
+
+# The final built image will be based on the standard Verdaccio docker image.
+FROM verdaccio/verdaccio:5
+
+# Copy the plugin files over from the 'builder' node image.
+# The `$VERDACCIO_USER_UID` env variable is defined in the base `verdaccio/verdaccio` image.
+# Refer to: https://github.com/verdaccio/verdaccio/blob/v5.2.0/Dockerfile#L32
+# The local verdaccio-docker-memory is setup as storage
+ADD docker.yaml /verdaccio/conf/config.yaml
+
+# Copy the plugin into the /verdaccio/plugins
+COPY --chown=$VERDACCIO_USER_UID:root --from=builder \
+ /verdaccio/plugins/verdaccio-docker-memory \
+ /verdaccio/plugins/verdaccio-docker-memory
diff --git a/docker-examples/v5/plugins/docker-local-plugin/README.md b/docker-examples/v5/plugins/docker-local-plugin/README.md
new file mode 100644
index 000000000..afc12e9a1
--- /dev/null
+++ b/docker-examples/v5/plugins/docker-local-plugin/README.md
@@ -0,0 +1,42 @@
+# Installing a local plugin with Docker build
+
+On this small tutorial (based on [`verdaccio-prometheus-middleware`](https://github.com/xlts-dev/verdaccio-prometheus-middleware) example) you will be able to use a published package in any random registry (npmjs by default) and use it withing a docker image without mapping need it.
+
+> Since verdaccio:5 uses `yarn@2` to run the application, this tutorial is a workaround but future prove since verdaccio 6 uses `pnpm` to build the docker image.
+
+There are three main steps to highlight:
+
+- Note the custom plugin at `plugins/verdaccio-docker-memory` under the name `verdaccio-docker-memory`.
+- Install the _production_ dependencies for the plugin `verdaccio-docker-memory`
+- `docker.yaml`: This is a copy of the original configuration file for docker and with small modifications to use the local plugin `verdaccio-docker-memory`.
+- The `Dockerfile` take advance of the docker multi-stage build to copy the local plugin into the `verdaccio/plugins` folder withing the image, then we apply the right permissions `--chown=$VERDACCIO_USER_UID:root` so the plugin is recognized.
+
+## Run it
+
+Build this image.
+
+```bash
+docker build -t verdaccio/verdaccio:local .
+```
+
+and to run it
+
+```bash
+docker run -it --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio:local
+```
+
+## Usage
+
+```dockerfile
+FROM node:lts-alpine as builder
+RUN mkdir -p /verdaccio/plugins
+ADD plugins/verdaccio-docker-memory /verdaccio/plugins/verdaccio-docker-memory
+RUN cd /verdaccio/plugins/verdaccio-docker-memory \
+ && ls -ls \
+ && npm install --production
+FROM verdaccio/verdaccio:5
+ADD docker.yaml /verdaccio/conf/config.yaml
+COPY --chown=$VERDACCIO_USER_UID:root --from=builder \
+ /verdaccio/plugins/verdaccio-docker-memory \
+ /verdaccio/plugins/verdaccio-docker-memory
+```
diff --git a/docker-examples/v5/plugins/docker-local-plugin/docker.yaml b/docker-examples/v5/plugins/docker-local-plugin/docker.yaml
new file mode 100644
index 000000000..aa779b5d9
--- /dev/null
+++ b/docker-examples/v5/plugins/docker-local-plugin/docker.yaml
@@ -0,0 +1,199 @@
+#
+# This is the default configuration file. It allows all users to do anything,
+# please read carefully the documentation and best practices to
+# improve security.
+#
+# Do not configure host and port under `listen` in this file
+# as it will be ignored when using docker.
+# see https://verdaccio.org/docs/en/docker#docker-and-custom-port-configuration
+#
+# Look here for more config file examples:
+# https://github.com/verdaccio/verdaccio/tree/5.x/conf
+#
+# Read about the best practices
+# https://verdaccio.org/docs/best
+
+# path to a directory with all packages
+storage: /verdaccio/storage/data
+
+store:
+ # dummy copy of https://www.npmjs.com/package/verdaccio-memory
+ docker-memory:
+ limit: 1000
+
+# path to a directory with plugins to include
+plugins: /verdaccio/plugins
+
+# https://verdaccio.org/docs/webui
+web:
+ title: Verdaccio This is a Test
+ # comment out to disable gravatar support
+ # gravatar: false
+ # by default packages are ordercer ascendant (asc|desc)
+ # sort_packages: asc
+ # convert your UI to the dark side
+ # darkMode: true
+ # html_cache: true
+ # by default all features are displayed
+ # login: true
+ # showInfo: true
+ # showSettings: true
+ # In combination with darkMode you can force specific theme
+ # showThemeSwitch: true
+ # showFooter: true
+ # showSearch: true
+ # showRaw: true
+ # showDownloadTarball: true
+ # HTML tags injected after manifest
+ # scriptsBodyAfter:
+ # - ''
+ # HTML tags injected before ends
+ # metaScripts:
+ # - ''
+ # - ''
+ # - ''
+ # HTML tags injected first child at