Docker as we know, is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud. In this post, we are going to take look at how to build a Docker image for Angular application (typically the steps are the same for any type of application).
Quick Snapshot
docker run hello-world
: docker --version
to check the version of the docker you’re running. OK, now we have got the docker setup, the next step is to define the docker container.
# Dockerfile FROM httpd:2.4 MAINTAINER Author Name author@email.com
ng build --prod
) #copy angular dist folder to container COPY dist/ /usr/local/apache2/htdocs/
#copy htaccess and httpd.conf to container COPY .htaccess /usr/local/apache2/htdocs/ COPY httpd.conf /usr/local/apache2/conf/httpd.conf
RewriteEngine On # If an existing asset or directory is requested go to it as it is RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d RewriteRule ^ - [L] # If the requested resource doesn't exist, use index.html RewriteRule ^ /index.html
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so LoadModule mpm_event_module modules/mod_mpm_event.so DocumentRoot "/usr/local/apache2/htdocs" <Directory "/usr/local/apache2/htdocs"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>
#change permissions RUN chmod -R 755 /usr/local/apache2/htdocs/
#expose port EXPOSE 4200
FROM httpd:2.4 #copy angular dist folder to container COPY dist/ /usr/local/apache2/htdocs/ #copy htaccess and httpd.conf to container COPY .htaccess /usr/local/apache2/htdocs/ COPY httpd.conf /usr/local/apache2/conf/httpd.conf #change permissions RUN chmod -R 755 /usr/local/apache2/htdocs/ #expose port EXPOSE 4200
Now that we have completed Dockerfile, next step is to build Docker image by docker build command
docker build -f UIDockerfile -t angularapp .
Here -t
specifies the name of the image and -f
specifies the name of the Dockerfile. Image tag I have left it empty.
Congrats! You’ve successfully built a container for your Angular application.
To run the Docker image, execute the following commands
docker run -p 4200:4200 angularapp
Here -p
specifies the port container:host mapping.
Launch your browser and hit http://localhost:4200
to access the application running on your container.
If you want to use Ngnix instead of Apache then here is sample Dockerfile
FROM nginx COPY nginx.conf /etc/nginx/nginx.conf COPY dist/prod /usr/share/nginx/html
As you can see we are copying all the files from our dist
folder and we are also replacing the Nginx configuration with our own custom configuration (like the one below)
Ngnix sample configuration
server { listen 80; location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html =404; } }
Congrats! You’ve successfully built and run container for your Angular application.
If you’re looking for running multi-container applications using Docker Compose tool then the configuration is as easy, there would be YAML file to configure your application’s services/networks/volumes, etc., Then, with a single command, you can create and start all the services from the compose configuration.
Here are the key steps :
Dockerfile
for your container(s).docker-compose.yml
for the services that make up your application services.docker-compose up
and Compose starts and runs your entire app.Sample docker-compose.yml
for Angular UI application would look like this:
version: '3' services: angularui: build: context: . dockerfile: UIDockerfile ports: - '4200:4200' networks: - samplenet networks: samplenet: null
To scale services using Docker compose refer here. There is much more to the Docker platform than what was covered here, but now you would have got a good idea of the basics of building containers for an application.
Like this post? Don’t forget to share it!
In today's digital-first world, businesses must adopt effective strategies to stay competitive. Social media marketing…
62% of UX designers now use AI to enhance their workflows. Artificial intelligence (AI) rapidly…
The integration of artificial intelligence into graphic design through tools like Adobe Photoshop can save…
The cryptocurrency trading world has grown significantly in recent years, with automation playing a key…
The non-fungible token (NFT) market has witnessed explosive growth over the past few years, transforming…
There are few things as valuable to a business as well-designed software. Organizations today rely…
This website uses cookies.