Skip to content

Commit edbbf1a

Browse files
fabriziocuccisdras
authored andcommitted
[cookbook] Dockerize Vue.js App (#1483)
* First draft with 'Simple Example' section * Fix typo on Docker 'build' command * Fix copy of multiple files * Remove unnecessary whitespace * Fix typo in webpack template link * First draft of 'Real-World Example' * Update 'Real-World Example' with multi-stage build * Add 'Why Dockerize a Vue.js App?' section * Rename 'Why Dockerize a Vue.js App?' as 'Additinal Context' * Add 'Alternative Patterns' section * Minor fix on 'Alternative Patterns' * Fixed typo * Update order to avoid collision with other cookbooks * Clarify why NGINX in real-world example
1 parent 9969aad commit edbbf1a

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: Dockerize Vue.js App
3+
type: cookbook
4+
order: 13
5+
---
6+
7+
## Simple Example
8+
9+
So you built your first Vue.js app using the amazing [Vue.js webpack template](https://github.com/vuejs-templates/webpack) and now you really want to show off with your colleagues by demonstrating that you can also run it in a Docker container.
10+
11+
Let's start by creating a `Dockerfile` in the root folder of our project:
12+
13+
```docker
14+
FROM node:9.11.1-alpine
15+
16+
# install simple http server for serving static content
17+
RUN npm install -g http-server
18+
19+
# make the 'app' folder the current working directory
20+
WORKDIR /app
21+
22+
# copy both 'package.json' and 'package-lock.json' (if available)
23+
COPY package*.json ./
24+
25+
# install project dependencies
26+
RUN npm install
27+
28+
# copy project files and folders to the current working directory (i.e. 'app' folder)
29+
COPY . .
30+
31+
# build app for production with minification
32+
RUN npm run build
33+
34+
EXPOSE 8080
35+
CMD [ "http-server", "dist" ]
36+
```
37+
38+
It may seem reduntant to first copy `package.json` and `package-lock.json` and then all project files and folders in two separate steps but there is actually [a very good reason for that](http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/) (spoiler: it allows us to take advantage of cached Docker layers).
39+
40+
Now let's build the Docker image of our Vue.js app:
41+
42+
```bash
43+
docker build -t vuejs-cookbook/dockerize-vuejs-app .
44+
```
45+
46+
Finally, let's run our Vue.js app in a Docker container:
47+
48+
```bash
49+
docker run -it -p 8080:8080 --rm --name dockerize-vuejs-app-1 vuejs-cookbook/dockerize-vuejs-app
50+
```
51+
52+
We should be able to access our Vue.js app on `localhost:8080`.
53+
54+
## Real-World Example
55+
56+
In the previous example, we used a simple, zero-configuration command-line [http server](https://github.com/indexzero/http-server) to serve our Vue.js app which is perfectly ok for quick prototyping and _may_ even be ok for simple production scenarios. After all, the documentation says:
57+
58+
> It is powerful enough for production usage, but it's simple and hackable enough to be used for testing, local development, and learning.
59+
60+
Nevertheless, for realistically complex production use cases, it may be wiser to stand on the shoulders of some giant like [NGINX](https://www.nginx.com/) or [Apache](https://httpd.apache.org/) and that is exactly what we are going to do next: we are about to leverage NGINX to serve our vue.js app because it is considered to be one of the most performant and battle-tested solutions out there.
61+
62+
Let's refactor our `Dockerfile` to use NGINX:
63+
64+
```docker
65+
# build stage
66+
FROM node:9.11.1-alpine as build-stage
67+
WORKDIR /app
68+
COPY package*.json ./
69+
RUN npm install
70+
COPY . .
71+
RUN npm run build
72+
73+
# production stage
74+
FROM nginx:1.13.12-alpine as production-stage
75+
COPY --from=build-stage /app/dist /usr/share/nginx/html
76+
EXPOSE 80
77+
CMD ["nginx", "-g", "daemon off;"]
78+
```
79+
80+
Ok, let's see what's going on here:
81+
* we have split our original `Dockerfile` in multiple stages by leveraging the Docker [multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/) feature;
82+
* the first stage is responsible for building a production-ready artifact of our Vue.js app;
83+
* the second stage is responsible for serving such artifact using NGINX.
84+
85+
Now let's build the Docker image of our Vue.js app:
86+
87+
```bash
88+
docker build -t vuejs-cookbook/dockerize-vuejs-app .
89+
```
90+
91+
Finally, let's run our Vue.js app in a Docker container:
92+
93+
```bash
94+
docker run -it -p 8080:80 --rm --name dockerize-vuejs-app-1 vuejs-cookbook/dockerize-vuejs-app
95+
```
96+
97+
We should be able to access our Vue.js app on `localhost:8080`.
98+
99+
## Additional Context
100+
101+
If you are reading this cookbook, chances are you already know why you decided to dockerize your Vue.js app. But if you simply landed on this page after hitting the Google's `I'm feeling lucky` button, let me share with you a couple of good reasons for doing that.
102+
103+
Today's modern trend is to build applications using the [Cloud-Native](https://pivotal.io/cloud-native) approach which revolves mainly around the following buzzwords:
104+
* Microservices
105+
* DevOps
106+
* Continuous Delivery
107+
108+
Let's see how these concepts actually affect our decision of dockerizing our Vue.js app.
109+
110+
### Effects of Microservices
111+
112+
By adopting the [microservices architectural style](https://martinfowler.com/microservices/), we end up building a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms. These services are built around business capabilities and independently deployable by fully automated deployment machinery.
113+
114+
So, committing to this architectural approach most of the time implies developing and delivering our front-end as an independent service.
115+
116+
### Effects of DevOps
117+
118+
The adoption of [DevOps](https://martinfowler.com/bliki/DevOpsCulture.html) culture, tools and agile engineering practices has, among other things, the nice effect of increasing the collaboration between the roles of development and operations. One of the main problem of the past (but also today in some realities) is that the dev team tended to be uninterested in the operation and maintenance of a system once it was handed over to the ops team, while the latter tended to be not really aware of the system's business goals and, therefore, reluctant in satisfying the operational needs of the system (also referred to as "whims of developers").
119+
120+
So, delivering our Vue.js app as a Docker image helps reducing, if not removing entirely, the difference between running the service on a deveveloper's laptop, the production environment or any environment we may think of.
121+
122+
### Effects of Continuous Delivery
123+
124+
By leveraging the [Continuous Delivery](https://martinfowler.com/bliki/ContinuousDelivery.html) discipline we build our software in a way that it can potentially be released to production at any time. Such engineering practice is enabled by means of what is normally called [continuous delivery pipeline](https://martinfowler.com/bliki/DeploymentPipeline.html). The purpose of a continuous delivery pipeline is to split our build into stages (e.g. compilation, unit tests, integration tests, performance tests, etc.) and let each stage verify our build artifact whenever our software changes. Ultimately, each stage increases our confidence in the production readiness of our build artifact and, therefore, reduces the risk of breaking things in production (or any other environment for that matters).
125+
126+
So, creating a Docker image for our Vue.js app is a good choice here because that would represent our final build artifact, the same artifact that would be verified against our continuous delivery pipeline and that could potentially be released to production with confidence.
127+
128+
## Alternative Patterns
129+
130+
If your company is not into Docker and Kubernetes just yet or you simply want to get your MVP out the door, maybe dockerizing your Vue.js app is not what you need.
131+
132+
Common alternatives are:
133+
* leveraging an all-in-one platform like [netlify](https://www.netlify.com/);
134+
* hosting your SPA on [Amazon S3](https://aws.amazon.com/s3/) and serving it with [Amazon CloudFront](https://aws.amazon.com/cloudfront/) (see [this](https://serverless-stack.com/chapters/deploy-the-frontend.html) link for a detailed guide).

0 commit comments

Comments
 (0)