This project shows How to run angular app inside a container:
- How to build a Angular app.
- How to run angular app along side nginx server.
- How to mount a volume inside a container that point to local dev environment.
- How to build an image.
- How to run a containerize angular app.
-
Install the latest LTS version of Node.js from https://nodejs.org. IMPORTANT: The server uses ES2015 features AND the Angular CLI so you need a current version of Node.js.
-
Install the Angular CLI
npm install -g @angular/cli
-
Run
npm install
to install app dependencies -
Run
ng build --watch
to build and bundle the code -
Run
npm start
in a separate terminal window to build the TypeScript, watch for changes and launch the web server -
Go to http://localhost:8080 in your browser
-
Install the Angular CLI
npm install -g @angular/cli
-
Run
npm install
at the root of this project -
Build the project
ng build
-
Ensure that you have volumes (file sharing) enabled in the Docker Desktop settings.
-
Note that this build puts the build files directly in the
dist
folder. If yourangular.json
file in your own custom project puts them in a subfolder such asdist/your-project-folder
then you'll need to update thedocker-compose.yml
file. In that case you'd change:volumes: - ./dist:/usr/share/nginx/html
To:
volumes: - ./dist/your-project-folder:/usr/share/nginx/html
-
Run
docker-compose build
-
Run
docker-compose up
-
Visit
http://localhost
-
Run
docker-compose -f docker-compose.prod.yml [build | up]
. This uses a multi-stage Docker build process to create the nginx image for the Angular app.Note: This project build puts the Angular build files directly in the
dist
folder. If yourangular.json
file in your own custom project puts them in a subfolder such asdist/your-project-folder
then you'll need to updatenginx.prod.dockerfile
with the appropriate path. You'd need to update this instruction:COPY --from=node /app/dist /usr/share/nginx/html
To:
COPY --from=node /app/dist/your-project-folder /usr/share/nginx/html
- Install Docker Desktop from https://www.docker.com/get-started
- Start Docker and enable Kubernetes in the Docker Desktop preferences/settings
- Run
docker-compose build
to create the images - Run
kubectl apply -f .k8s
to start Kubernetes - Visit
http://localhost
- Stop Kubernetes using
kubectl delete -f .k8s
```
docker-compose build
docker-compose up
docker-compose down
```
```
docker stack deploy -c docker-compose.yml angular-jumpstart
docker stack ls
docker stack rm angular-jumpstart
```
From Docker-Compose + Kubernetes = Kompose
https://kompose.io/
```
kompose convert -h
kompose convert -f docker-compose.yml -o ./[your-folder-goes-here]
```
Tweak the generated YAML. Then once ready run:
```
kubectl apply -f [your-folder-name]
```
If you'd like to use the Skaffold tool to run the project in Kubernetes, install it, and run the following command:
skaffold dev
To generate the skaffold.yaml
file that's included in the project the following command was run and the image context paths it defines were modified:
skaffold init -k '.k8s/*.yml' \
-a '{"builder":"Docker","payload":{"path":".docker/nginx.dev.dockerfile"},"image":"nginx-angular-jumpstart"}' \
-a '{"builder":"Docker","payload":{"path":".docker/node.dockerfile"},"image":"node-service-jumpstart"}'
If you wanted to generate the initial Kubernetes manifest files from an existing docker-compose.yml file you can use the following command. It uses the Kompose tool behind the scenes to create the YAML files
skaffold init --compose-file docker-compose.yml \
-a '{"builder":"Docker","payload":{"path":".docker/nginx.dev.dockerfile"},"image":"nginx-angular-jumpstart"}' \
-a '{"builder":"Docker","payload":{"path":".docker/node.dockerfile"},"image":"node-service-jumpstart"}'