To test if the installation succeeds, run the command docker -version. If you see a version number, everything is good to go to the next step.
4. Create a Multi-Stage Docker Image
If you have a bit of experience with Docker, you know what a Docker image is and what you can do with it. For those who don’t know it, let me explain it in short.
What is a Docker image?
A Docker image is the blueprint for the application you want to run in a Docker container. In the blueprint, you have to decide what kind of steps Docker needs to take to build your application, get dependencies, and copy the needed files.
With the Docker image, you can spin up a Docker container to run it and make it visible in the browser, for example.
What is a Docker container?
A Docker container is a sort of virtual (mini) server that you can start, stop, or remove with a single command. A Docker container makes it easy to spin up multiple instances of that container so you can divide the traffic between them.
Let’s build the first part of the Docker image
Create a file, Dockerfile, notice there is no file extension. (That's correct.)
First, we are going to tell it on which image we want our image to be based on. This image will install all the dependencies for Node.js automatically to build our Angular 9 application on it.
FROM node:13.3.0 AS compile-image
We are using node:13.3.0, if you want to use another version or smaller base image, check the Node.js Docker page. We call that image compile-image, this will become handy later on.
RUN npm install -g yarn
Next step, we install Yarn for the people who are using Yarn. If you want to use npm, you can skip this step.
We set our WORKDIR to /opt/ng so we know the directory where everything will be copied to with the COPY line.
For Yarn, we run the yarn command as you would do on your local computer.
RUN npm install
When using npm, you run the npm install command to install all the dependencies.
COPY . ./ RUN ng build --prod
Copy all the other Angular files to be able to run the ng build --prod. It is recommended to run this command on your computer before creating a Docker image, this will save you from constantly building a Docker image that is failing.
Most Docker images will stop here. But this will make your Docker image bigger than needed.
Build the second part of the Docker image
To run our Angular app, we don’t need to run it with Node.js, we can choose a more lightweight variant to run it.
The second phase of the Docker image starts with the FROM statement where we specify we want to use nginx.
The first COPY statement copies the default.conf file from the nginx base image to the Docker image we are building.
The second COPY statement tells it that we want some files from the compile-image. So, underwater, it has built the first part of the image in a separate Docker image.
We copy the built Angular app from the /dist folder in the compile-image to the html folder of NGINX.
The reason for using NGINX is that it will make it possible to run the Angular app without the need for the big node_modules folder. We only use the files of our application. This will save some bytes on the Docker image.
If you made changes to your Angular app, you need to create a new Docker image.
docker container stop 8ef42
docker container rm 8ef42
Use this command to stop and remove your Docker container. (If you don’t know your Docker container ID, run docker container ls.) You don't need the long ID, just type the first three or four characters, that will be enough.
When the new Docker image is created, you can start a new Docker container with the docker run command above.
7. Add Docker Image to GitHub
Now that we have created a Docker image and used it to run in a Docker container on your local computer, we have tested that this worked.
If you want to use this app on your hosting (it has to be a hosting platform that supports Docker, I recommend DigitalOcean) you have to store this Docker image in a Docker registry.
A Docker registry is a storage provider for Docker images. GitHub is a great way to store private and public Docker images.
Login to GitHub Docker Registry
Before we can push our images, we need to login to the Github Docker Registry on your computer.
Replace the USERNAME with your username and TOKEN with the token you have created via the GitHub website. Give the token a name and select the repo, write:packages, read:packages, and delete:packages.
Copy the token and past it in the command.
Image for post
To push a Docker image, you have to create a GitHub repo. If you host your code on GitHub, you have probably done that already. If you haven’t, do that first.
Publish Docker Image
The Docker image we created from our Angular app is going to be published to the registry.
docker image ls
To publish, we need to know the Docker image ID, you can find this with the command above.
Now that you know your Docker image ID, you can replace IMAGE_ID with your image ID. Next, replace everything in the URL with your information. For VERSION, you can start with 0.0.1 or anything else that makes sense for you.
When everything went great, go to your GitHub repo in the browser. You should see that there is a package next to the number of branches.
Image for post
Click the link and choose your image. This will navigate to your Docker image page. On this page, you will see the link to your image.
Now you can use the Docker image on your web hosting. I’m not going to explain how you install Docker on your hosting (because there are a million possibilities). If you are using Digital Ocean, you can install it manually or use the Docker installer in the marketplace.
With this command, you can pull the image you just published to the GitHub Docker Registry.
docker run --name ng-app-container -d -p 8080:80 angular-9-app
When you want to run it in a container, you can run the same command as before.
Hopefully, this tutorial helped you create a Docker image with Angular 9, run it inside a Docker container, and publish it in the GitHub Docker registry.
For this post I would like to thank Frank Spijkerman who helped me creating this fast Docker image setup. 🙏
If you have any questions about it, please let me know in the comments. If not, happy coding!