refactor(docker): use templating to handle env variables (#8878)

Refs #8877
This commit is contained in:
Vladimír Gorej
2023-06-02 16:05:08 +02:00
committed by GitHub
parent b017858fe1
commit 1f7bb89217
4 changed files with 34 additions and 44 deletions

View File

@@ -8,19 +8,21 @@ RUN apk update && apk add --no-cache "nodejs>=18.14.1-r0 "
LABEL maintainer="fehguy" LABEL maintainer="fehguy"
ENV API_KEY "**None**" ENV API_KEY="**None**" \
ENV SWAGGER_JSON "/app/swagger.json" SWAGGER_JSON="/app/swagger.json" \
ENV PORT 8080 PORT="8080" \
ENV BASE_URL "" PORT_IPV6="" \
ENV SWAGGER_JSON_URL "" BASE_URL="/" \
SWAGGER_JSON_URL=""
COPY --chown=nginx:nginx --chmod=0666 ./docker/nginx.conf ./docker/cors.conf /etc/nginx/ COPY --chown=nginx:nginx --chmod=0666 ./docker/default.conf.template ./docker/cors.conf /etc/nginx/templates/
# copy swagger files to the `/js` folder
COPY --chmod=0666 ./dist/* /usr/share/nginx/html/ COPY --chmod=0666 ./dist/* /usr/share/nginx/html/
COPY --chmod=0555 ./docker/docker-entrypoint.d/ /docker-entrypoint.d/ COPY --chmod=0555 ./docker/docker-entrypoint.d/ /docker-entrypoint.d/
COPY --chmod=0666 ./docker/configurator /usr/share/nginx/configurator COPY --chmod=0666 ./docker/configurator /usr/share/nginx/configurator
RUN chmod 777 /usr/share/nginx/html/ /etc/nginx/ /var/cache/nginx/ /var/run/ # Simulates running NGINX as a non root; in future we want to use nginxinc/nginx-unprivileged.
# In future we will have separate unpriviledged images tagged as v5.1.2-unprivileged.
RUN chmod 777 /usr/share/nginx/html/ /etc/nginx/conf.d/ /etc/nginx/conf.d/default.conf /var/cache/nginx/ /var/run/
EXPOSE 8080 EXPOSE 8080

View File

@@ -1,21 +1,8 @@
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
types { types {
text/plain yaml; text/plain yaml;
text/plain yml; text/plain yml;
} }
sendfile on;
keepalive_timeout 65;
gzip on; gzip on;
gzip_static on; gzip_static on;
gzip_disable "msie6"; gzip_disable "msie6";
@@ -29,28 +16,27 @@ http {
server_tokens off; # Hide Nginx version server_tokens off; # Hide Nginx version
server { server {
listen 8080; listen $PORT;
server_name localhost; server_name localhost;
index index.html index.htm; index index.html index.htm;
location / { location $BASE_URL {
absolute_redirect off; absolute_redirect off;
alias /usr/share/nginx/html/; alias /usr/share/nginx/html/;
expires 1d; expires 1d;
location ~ swagger-initializer.js { location ~ swagger-initializer.js {
expires -1; expires -1;
include cors.conf; include templates/cors.conf;
} }
location ~* \.(?:json|yml|yaml)$ { location ~* \.(?:json|yml|yaml)$ {
#SWAGGER_ROOT #SWAGGER_ROOT
expires -1; expires -1;
include cors.conf; include templates/cors.conf;
} }
include cors.conf; include templates/cors.conf;
}
} }
} }

View File

@@ -1,18 +1,13 @@
#! /bin/sh #! /bin/sh
set -e set -e
BASE_URL=${BASE_URL:-/}
NGINX_ROOT=/usr/share/nginx/html NGINX_ROOT=/usr/share/nginx/html
INITIALIZER_SCRIPT=$NGINX_ROOT/swagger-initializer.js INITIALIZER_SCRIPT=$NGINX_ROOT/swagger-initializer.js
NGINX_CONF=/etc/nginx/nginx.conf NGINX_CONF=/etc/nginx/conf.d/default.conf
node /usr/share/nginx/configurator $INITIALIZER_SCRIPT node /usr/share/nginx/configurator $INITIALIZER_SCRIPT
if [[ "${BASE_URL}" != "/" ]]; then
sed -i "s|location / {|location $BASE_URL {|g" $NGINX_CONF
fi
if [ "$SWAGGER_JSON_URL" ]; then if [ "$SWAGGER_JSON_URL" ]; then
sed -i "s|https://petstore.swagger.io/v2/swagger.json|$SWAGGER_JSON_URL|g" $INITIALIZER_SCRIPT sed -i "s|https://petstore.swagger.io/v2/swagger.json|$SWAGGER_JSON_URL|g" $INITIALIZER_SCRIPT
sed -i "s|http://example.com/api|$SWAGGER_JSON_URL|g" $INITIALIZER_SCRIPT sed -i "s|http://example.com/api|$SWAGGER_JSON_URL|g" $INITIALIZER_SCRIPT
@@ -41,12 +36,7 @@ fi
# enable/disable the address and port for IPv6 addresses that nginx listens on # enable/disable the address and port for IPv6 addresses that nginx listens on
if [[ -n "${PORT_IPV6}" ]]; then if [[ -n "${PORT_IPV6}" ]]; then
sed -i "s|8080;|8080;\n listen [::]:${PORT_IPV6};|g" $NGINX_CONF sed -i "s|${PORT};|${PORT};\n listen [::]:${PORT_IPV6};|g" $NGINX_CONF
fi
# replace the PORT that nginx listens on if PORT is supplied
if [[ -n "${PORT}" ]]; then
sed -i "s|8080|${PORT}|g" $NGINX_CONF
fi fi
find $NGINX_ROOT -type f -regex ".*\.\(html\|js\|css\)" -exec sh -c "gzip < {} > {}.gz" \; find $NGINX_ROOT -type f -regex ".*\.\(html\|js\|css\)" -exec sh -c "gzip < {} > {}.gz" \;

View File

@@ -64,7 +64,7 @@ const ui = SwaggerUIBundle({
You can pull a pre-built docker image of the swagger-ui directly from Docker Hub: You can pull a pre-built docker image of the swagger-ui directly from Docker Hub:
``` ```sh
docker pull swaggerapi/swagger-ui docker pull swaggerapi/swagger-ui
docker run -p 80:8080 swaggerapi/swagger-ui docker run -p 80:8080 swaggerapi/swagger-ui
``` ```
@@ -73,24 +73,36 @@ Will start nginx with Swagger UI on port 80.
Or you can provide your own swagger.json on your host Or you can provide your own swagger.json on your host
``` ```sh
docker run -p 80:8080 -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui docker run -p 80:8080 -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui
``` ```
You can also provide a URL to a swagger.json on an external host: You can also provide a URL to a swagger.json on an external host:
``` ```sh
docker run -p 80:8080 -e SWAGGER_JSON_URL=https://petstore3.swagger.io/api/v3/openapi.json swaggerapi/swagger-ui docker run -p 80:8080 -e SWAGGER_JSON_URL=https://petstore3.swagger.io/api/v3/openapi.json swaggerapi/swagger-ui
``` ```
The base URL of the web application can be changed by specifying the `BASE_URL` environment variable: The base URL of the web application can be changed by specifying the `BASE_URL` environment variable:
``` ```sh
docker run -p 80:8080 -e BASE_URL=/swagger -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui docker run -p 80:8080 -e BASE_URL=/swagger -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui
``` ```
This will serve Swagger UI at `/swagger` instead of `/`. This will serve Swagger UI at `/swagger` instead of `/`.
You can specify a different port via `PORT` variable for accessing the application, default is `8080`.
```sh
docker run -p 80:80 -e PORT=80 swaggerapi/swagger-ui
```
You can specify an IPv6 port via `PORT_IPV6` variable. By default, IPv6 port is not set.
```sh
docker run -p 80:80 -e PORT_IPV6=8080 swaggerapi/swagger-ui
```
For more information on controlling Swagger UI through the Docker image, see the Docker section of the [Configuration documentation](configuration.md#docker). For more information on controlling Swagger UI through the Docker image, see the Docker section of the [Configuration documentation](configuration.md#docker).
### unpkg ### unpkg