0
0
Vueframework~10 mins

Docker deployment for Vue apps - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Docker deployment for Vue apps
Write Vue app code
Create Dockerfile
Build Docker image
Run Docker container
Access Vue app in browser
App serves static files inside container
This flow shows how you write your Vue app, create a Dockerfile to package it, build the image, run it as a container, and then access the app served inside Docker.
Execution Sample
Vue
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:stable-alpine
COPY --from=0 /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
This Dockerfile builds the Vue app using Node, then serves the built files with Nginx inside a container.
Execution Table
StepActionDetailsResult
1FROM node:18-alpineStart with Node image for buildBase image ready for building Vue app
2WORKDIR /appSet working directory inside containerCommands run inside /app folder
3COPY package*.json ./Copy package files for dependenciesFiles ready for npm install
4RUN npm installInstall dependenciesnode_modules folder created
5COPY . .Copy all source codeVue app source inside container
6RUN npm run buildBuild Vue app to static filesdist folder with production files created
7FROM nginx:stable-alpineSwitch to Nginx image to serve filesNew base image for serving
8COPY --from=0 /app/dist /usr/share/nginx/htmlCopy built files from build stageStatic files ready for Nginx
9EXPOSE 80Open port 80 for HTTPContainer listens on port 80
10CMD ["nginx", "-g", "daemon off;"]Start Nginx in foregroundNginx serves Vue app
11Run containerdocker run -p 8080:80 imageVue app accessible at localhost:8080
12Access app in browserOpen http://localhost:8080Vue app loads and works
13ExitStop containerApp no longer accessible
💡 Container stops or user closes app, ending deployment session
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 8Final
node_modulesnoneinstalledinstalledinstalledinstalled
dist foldernonenonecreatedcopiedserved
container portnonenonenone80 exposed80 exposed
Nginx runningnonononoyes
Key Moments - 3 Insights
Why do we use two FROM statements in the Dockerfile?
The first FROM uses Node to build the Vue app, which needs Node and npm. The second FROM uses Nginx to serve the built static files. This multi-stage build keeps the final image small and efficient, as shown in execution_table steps 1 and 7.
What happens if we forget to copy the built files to Nginx?
If the built files are not copied (step 8), Nginx will serve an empty folder and the app won't load. The execution_table shows the importance of copying /app/dist to /usr/share/nginx/html.
Why do we expose port 80 and map it to 8080 when running the container?
Nginx listens on port 80 inside the container (step 9). We map it to 8080 on the host so we can access the app at localhost:8080 in the browser (step 11). This port mapping connects the container to your computer.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6. What is created after running 'npm run build'?
Adist folder with production files
BNginx server started
Cnode_modules folder
Dpackage.json file
💡 Hint
Check the 'Result' column at step 6 in the execution_table.
At which step does the container start serving the Vue app with Nginx?
AStep 4
BStep 7
CStep 10
DStep 12
💡 Hint
Look for the step where Nginx is started in the execution_table.
If we change the EXPOSE port from 80 to 3000 in the Dockerfile, what else must we change to access the app correctly?
ANothing, it works automatically
BChange the port mapping in 'docker run' to map 3000
CChange the CMD to start Nginx on port 80
DRebuild the Vue app
💡 Hint
Refer to the port mapping explained in key_moments about EXPOSE and docker run.
Concept Snapshot
Docker deployment for Vue apps:
- Use multi-stage Dockerfile: build with Node, serve with Nginx
- Build Vue app to static files (npm run build)
- Copy built files to Nginx folder
- Expose port 80 in container
- Map container port to host for browser access
- Run container to serve app
Full Transcript
This visual execution shows how to deploy a Vue app using Docker. First, the Vue app code is written. Then a Dockerfile is created with two stages: the first uses a Node image to install dependencies and build the app into static files. The second stage uses an Nginx image to serve those static files. The Dockerfile copies the built files from the first stage into the Nginx folder. Port 80 is exposed in the container, and when running the container, port 80 is mapped to a host port like 8080. Finally, accessing localhost:8080 in a browser loads the Vue app served by Nginx inside Docker. Key moments include understanding why multi-stage builds are used, the importance of copying built files, and how port mapping works. The execution table traces each Dockerfile step and container run action, showing the state changes of files, ports, and services. This step-by-step trace helps beginners see how Docker packages and serves a Vue app.