Skip to content

Instantly share code, notes, and snippets.

@diyfr
Last active June 3, 2021 13:40

Revisions

  1. diyfr revised this gist Jun 3, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Dockerfile
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ FROM node:slim AS build

    COPY . /app
    WORKDIR /app
    RUN npm ci && npx ng build --prod --base-href='\${BASE_URL}'
    RUN npm run ng build -- --prod --base-href='\${BASE_URL}'

    # RUN
    FROM nginx:alpine AS run
  2. diyfr revised this gist Jun 3, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Dockerfile
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ FROM node:slim AS build

    COPY . /app
    WORKDIR /app
    RUN npm ci && npx ng build --prod --base-href '\${BASE_URL}'
    RUN npm ci && npx ng build --prod --base-href='\${BASE_URL}'

    # RUN
    FROM nginx:alpine AS run
  3. diyfr revised this gist Jun 3, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions readme.MD
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    ### Multistage Docker sur un projet Angular
    Fichiers utilisés : `Dockerfile` et `docker-entrypoint.sh`
    Le build docker va injecter ${BASE_URL} en base href dans le index.html

    ### Récupérer valeurs d'environnement dans l'application. Ajout d'un service angular.
  4. diyfr created this gist Jun 3, 2021.
    22 changes: 22 additions & 0 deletions Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # BUILD
    FROM node:slim AS build

    COPY . /app
    WORKDIR /app
    RUN npm ci && npx ng build --prod --base-href '\${BASE_URL}'

    # RUN
    FROM nginx:alpine AS run

    COPY ./docker/docker-entrypoint.sh /usr/bin/
    COPY ./docker/nginx.front.conf /etc/nginx/conf.d/default.conf
    COPY --from=build /app/dist/my-app /usr/share/nginx/html

    EXPOSE 80
    ENV PORT=80
    ENV BASE_URL=/
    ENV VAL1=maVal1
    ENV VAL2=maVal2

    ENTRYPOINT ["/usr/bin/docker-entrypoint.sh"]
    CMD ["nginx", "-g", "daemon off;"]
    8 changes: 8 additions & 0 deletions config.js.tpl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    (function (window) {
    const config = {
    baseUrl: '${BASE_URL}',
    val1: '${VAL1}',
    val2: '${VAL2}'
    }
    window.config = window.config || config
    })(this)
    16 changes: 16 additions & 0 deletions config.service.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    @Injectable()
    export class ConfigService implements Configuration {
    baseUrl: string
    val1: string
    val2: string

    constructor () {
    const browserWindow = window || {}
    const browserWindowConfig = browserWindow['config'] || {}
    for (const key in browserWindowConfig) {
    if (browserWindowConfig.hasOwnProperty(key)) {
    this[key] = browserWindowConfig[key]
    }
    }
    }
    }
    20 changes: 20 additions & 0 deletions docker-entrypoint.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/bin/sh
    set -e

    INDEX_PATH=/usr/share/nginx/html/index.html
    TEMP_INDEX=$(mktemp)
    # Remplace les valeurs d'environnements existantes dans index.html et place le résultat dans un fichier temporaire
    envsubst < ${INDEX_PATH} > ${TEMP_INDEX}
    # Remplace le fichier index.html par le fichier temporaire
    mv ${TEMP_INDEX} ${INDEX_PATH}
    chmod a+r ${INDEX_PATH}

    # Option 2 récuperer les valeurs environnement dans l'appli
    CONFIG_PATH=/usr/share/nginx/html/assets/config.js
    CONFIG_TEMPLATE_PATH=/usr/share/nginx/html/assets/config.js.tpl
    if test -f ${CONFIG_TEMPLATE_PATH}; then
    envsubst < ${CONFIG_TEMPLATE_PATH} > ${CONFIG_PATH}
    fi
    # -> Fin option 2

    exec "$@"
    15 changes: 15 additions & 0 deletions readme.MD
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    ### Multistage Docker sur un projet Angular
    Le build docker va injecter ${BASE_URL} en base href dans le index.html

    ### Récupérer valeurs d'environnement dans l'application. Ajout d'un service angular.
    Dans le Header du fichier `index.html` ajouter
    ```html
    <script type="text/javascript" src="assets/config.js"></script>
    ```
    `config.js.tpl` à placer dans assets/config
    `config.js` injecte les variables

    `config.service` Récupère les variables injectées