Create one Docker image of Angular + Nginx with dynamic API url

In this tutorial, I explain one workaround that I did to create one Docker image of one Angular application which has one dynamic API URL.

The article How To Build An Angular App Once And Deploy It To Multiple Environments – Solution 4: Override envir…


This content originally appeared on DEV Community and was authored by Felipe Henrique Gross Windmoller

In this tutorial, I explain one workaround that I did to create one Docker image of one Angular application which has one dynamic API URL.

The article How To Build An Angular App Once And Deploy It To Multiple Environments - Solution 4: Override environment file values explains how you can do this.

But, as I'm far from a Docker expert, I spent some time to create the Dockerfile to do this :)

Just quoting the article:

The idea is to use Angular’s environment.ts (for local development) and environment.prod.ts (for all other stages) with placeholder values which are overwritten per deployment:

export const environment = {
  apiUrl: 'MY_APP_API_URL',
  stage: 'MY_APP_STAGE',
};

If our pod is started we can then run the following script, for example in a Dockerfile, that overrides these placeholder values:

#!/bin/sh
# replace placeholder value in JS bundle with environment specific values
sed -i "s#MY_APP_API_URL#$API_URL#g" /usr/share/nginx/html/main.*.js

My contribution

Here is the Dockerfile I created to run this workaround:

FROM nginx:latest

COPY dist/your-app-name /usr/share/nginx/html

CMD ["/bin/bash", "-c", \
"echo API_URL=[$API_URL], && \
sed -i s#MY_APP_API_URL#$API_URL#g /usr/share/nginx/html/main.*.js && \
nginx -g 'daemon off;'"]

Build your project:

$ ng build

Place this Dockerfile into your Angular root folder and run this to build your image:

$ docker build -t angular/your-app-name .

After this, just run your container passing the API_URL variable:

$ docker run -p 80:80 --name your-app-name -e API_URL=http://localhost:8080 angular/your-app-name


This content originally appeared on DEV Community and was authored by Felipe Henrique Gross Windmoller


Print Share Comment Cite Upload Translate Updates
APA

Felipe Henrique Gross Windmoller | Sciencx (2021-07-09T19:37:52+00:00) Create one Docker image of Angular + Nginx with dynamic API url. Retrieved from https://www.scien.cx/2021/07/09/create-one-docker-image-of-angular-nginx-with-dynamic-api-url/

MLA
" » Create one Docker image of Angular + Nginx with dynamic API url." Felipe Henrique Gross Windmoller | Sciencx - Friday July 9, 2021, https://www.scien.cx/2021/07/09/create-one-docker-image-of-angular-nginx-with-dynamic-api-url/
HARVARD
Felipe Henrique Gross Windmoller | Sciencx Friday July 9, 2021 » Create one Docker image of Angular + Nginx with dynamic API url., viewed ,<https://www.scien.cx/2021/07/09/create-one-docker-image-of-angular-nginx-with-dynamic-api-url/>
VANCOUVER
Felipe Henrique Gross Windmoller | Sciencx - » Create one Docker image of Angular + Nginx with dynamic API url. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/09/create-one-docker-image-of-angular-nginx-with-dynamic-api-url/
CHICAGO
" » Create one Docker image of Angular + Nginx with dynamic API url." Felipe Henrique Gross Windmoller | Sciencx - Accessed . https://www.scien.cx/2021/07/09/create-one-docker-image-of-angular-nginx-with-dynamic-api-url/
IEEE
" » Create one Docker image of Angular + Nginx with dynamic API url." Felipe Henrique Gross Windmoller | Sciencx [Online]. Available: https://www.scien.cx/2021/07/09/create-one-docker-image-of-angular-nginx-with-dynamic-api-url/. [Accessed: ]
rf:citation
» Create one Docker image of Angular + Nginx with dynamic API url | Felipe Henrique Gross Windmoller | Sciencx | https://www.scien.cx/2021/07/09/create-one-docker-image-of-angular-nginx-with-dynamic-api-url/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.