ng new
with the desired project name. In the following examples, we’ll be using
the example project name of angular-project
.
Do you want to enable Server-Side Rendering (SSR) and Static Site Generation?
, proceed with N
(no), since this guide
focuses only on single-page applications. We’ll cover SSR in another guide.
If you have more questions, check out the Angular installation guide to learn more
about scaffolding the app.
Once the application is ready, navigate to the directory and install all the dependencies:
node_modules
folder in your project), so you can start your project by running the command:
http://localhost:4200
) to see your application.
dist/${PROJECT_NAME}
by default).
nginx.conf.template
in the root of your project. We won’t be using this template now; we’ll use it to
run the Nginx server inside the Docker container.
Note two important blocks in this configuration:
index.html
from the files we built using Ember, stored in the /usr/share/nginx/html
folder. We
will copy the assets to this folder while building the Docker application.
And then the API block:
example.com
. Whenever an API call is made to
example.com/api
, the nginx server acts as a reverse proxy server and forwards the request to the backend server.
You can use reverse proxying to navigate the traffic that is inbound to nginx. The reverse proxy server also acts as a
mask, hiding the real backend from the outside world.
.dockerignore
file and add the contents that should not be copied over
to the Docker file system.
.dockerignore
file and adding folders like node_modules
is a must, since dependencies will be installed
while building the image based on the platform preferences used. Copying those from the file system will overwrite the
installed dependencies and might cause errors during deployment.angular-app
image for platform linux/amd64
and tags it as latest
.
If you are locally testing your application, you can skip the platform
key to build the images:
-it
: Enables interactivity with TTY.--rm
: Tells the Docker Daemon to clean up the container and remove the file system after the container exits.--name angular-app
: Names the container angular-app
.-e PORT=4200
: Sets the environment variable PORT in Docker to 4200
.-e BACKEND_PORT=4500
: Sets the environment variable BACKEND_PORT in Docker to 4500
.-e BACKEND_HOST=host.docker.internal
: Sets the environment variable BACKEND_HOST in Docker to
host.docker.internal
.-d
: Runs the container in detached (background) mode. You can skip this flag to see the logs directly in your
terminal window.-p 4200:4200
: Maps port 4200 on your host to port 4200 in the container.angular-app
: The name of the image.http://localhost:4200
to see the Angular application running inside the Docker
container.
Hurray 🎉. Now we have created and packaged an Angular app for production use.