How to Serve a Vue App with nginx in Docker
Tags: vue, docker, webdev, devops
Published: 2020-12-28 11:43:20

---
title: How to Serve a Vue App with nginx in Docker
published: true
description: Let’s create a website using the Vue framework and learn how to serve it in a Docker container using nginx.
tags: vue, docker, webdev, devops
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/v8imgtix2h0luq2it7ch.png
canonical_url: https://typeofnan.dev/how-to-serve-a-vue-app-with-nginx-in-docker/
---

Let's create a website using the Vue framework and learn how to serve it in a Docker container using nginx. 

<hr />

### If you enjoy this post, please give it a 💓, 🦄, or 🔖 and consider signing up for 📬 [my free weekly dev newsletter](https://buttondown.email/devtuts)

<hr />

To start out, we are going to use the Vue CLI to generate a starter Vue app.

We will name this app `vue-nginx-docker`.

```bash
npx @vue/cli create vue-nginx-docker
```

For this tutorial I'll be using yarn, so if you want to follow along exactly be sure to select "yarn" when generating the application. It's totally fine to select npm as well; you will just need to adjust some of the Docker commands accordingly.

Once the app is set up, navigate to the new app folder:

```bash
cd vue-nginx-docker
```

Now we know we'll need a couple files to use with Docker: a `Dockerfile` and a `.dockerignore` file. Let's make them now.

```bash
touch Dockerfile
```

For our `.dockerignore` file, let's make sure to ignore our dependency files in `node_modules` like we would with git.

```bash
echo "node_modules" > .dockerignore
```

# Building Out the Dockerfile

Time to build out the Dockerfile! There are a lot of ways we could do this, but today we'll use a _multi-stage_ process:

- Stage 1: Node image for building frontend assets
- Stage 2: nginx stage to serve frontend assets

## Stage 1: Building the front-end assets

Our first stage will:

- Use a node image
- Copy all our Vue files into a working directory
- Install the project dependencies with yarn
- Build the app with yarn

Here's how this looks in Docker!

```dockerfile
# Name the node stage "builder"
FROM node:10 AS builder
# Set working directory
WORKDIR /app
# Copy all files from current directory to working dir in image
COPY . .
# install node modules and build assets
RUN yarn install && yarn build
```

Next up, we actually serve the content we just built!

## Stage 2: Our nginx server

Our second stage will:

- Use an nginx image
- Remove any default static assets from the nginx image
- Copy our static assets from the `builder` image we created in the first stage
- Specify the entrypoint for our container to run nginx

Here's what this stage looks like in the Dockerfile:

```dockerfile
# nginx state for serving content
FROM nginx:alpine
# Set working directory to nginx asset directory
WORKDIR /usr/share/nginx/html
# Remove default nginx static assets
RUN rm -rf ./*
# Copy static assets from builder stage
COPY --from=builder /app/dist .
# Containers run nginx with global directives and daemon off
ENTRYPOINT ["nginx", "-g", "daemon off;"]
```

Importantly, Vue builds the static assets into the `dist` folder by default, so we copy our files from there. If your app builds the files into another folder for some reason, adjust accordingly.

## Putting it All Together

Okay! Let's put this all together and run our app.

Our multi-stage Dockerfile should now look like this:

```dockerfile
# Multi-stage
# 1) Node image for building frontend assets
# 2) nginx stage to serve frontend assets

# Name the node stage "builder"
FROM node:10 AS builder
# Set working directory
WORKDIR /app
# Copy all files from current directory to working dir in image
COPY . .
# install node modules and build assets
RUN yarn install && yarn build

# nginx state for serving content
FROM nginx:alpine
# Set working directory to nginx asset directory
WORKDIR /usr/share/nginx/html
# Remove default nginx static assets
RUN rm -rf ./*
# Copy static assets from builder stage
COPY --from=builder /app/dist .
# Containers run nginx with global directives and daemon off
ENTRYPOINT ["nginx", "-g", "daemon off;"]
```

Now that we have assembled our Dockerfile, let's build an image called `vue-nginx`:

```bash
docker build -t vue-nginx .
```

Now that our image is built, we can start a container with the following command, which will serve our app on port 8080.

```bash
docker run --rm -it -p 8080:80 vue-nginx
```

Navigate to http://localhost:8080, and you should now see our default Vue app!

<hr />

### If you enjoy this post, please give it a 💓, 🦄, or 🔖 and consider signing up for 📬 [my free weekly dev newsletter](https://buttondown.email/devtuts)