Node v21.6.2 (npm v10.2.4)
Create project
Let’s use the command line to create our project my-vite-vue-ssr
.
Follow other commands then continue.
Save your current Node & NPM version (for NVM).
Initiate git with git init
. And save the Initial state.
git init
Initialized empty Git repository in ~/git/my-vite-vue-ssr/.git/
# Stage all files
git add .
# Commit and Message it
git commit -avm "INIT my-vite-vue-ssr Node `node -v`"
Docker things
Add .dockerignore
Create Docker Node Image
Before we create Dockerfile
we need to edit the package.json
. In scripts
we add more handy command lines.
I’m using Docker Hub User as dockeruser
and project name as my-vite-vue-ssr
. Use your own preferences and change the code accordingly.
See we’re using port 3000 instead of 5173
And move all devDependencies
lines to dependencies
.
During Docker build the script will only check dependencies
.
See below the result.
Create Dockerfile
Since my node version is v21.6.2
. I’m looking which Alpine version from https://hub.docker.com/_/node/ and look for available version number -alpine
.
Build Docker Image
Create Docker Image: dockeruser/my-vite-vue-ssr
.
Since we made special commands in package.json
we can build from it, without knowing long strings of commands. Just peek it in.
Hopefully everything goes well, and we can look up with in command: docker images
.
REPOSITORY TAG IMAGE ID CREATED SIZE
dockeruser/my-vite-vue-ssr latest 323b18f70290 About a minute ago 211MB
Test Docker Image
Let’s use the command and check the web: http://localhost:3000/
npm run docker:run
Kill Running Image
To kill the image, first we need to see what’s running again on port 3000. With this command below we can see what’s going on.
docker ps | grep 3000
Use this info 7d32fd2396d8
to kill the process.
Other useful docker commands
# see docker images
docker images
# remove IMAGE ID ex. baabdd69cf7a
docker rmi -f baabdd69cf7a
# see dangling images
docker images -q --filter "dangling=true"
# remove untagged images
docker rmi -f `docker images -q --filter "dangling=true"` > /dev/null 2>&1 || echo "Nothing to remove"
Conclusion
This is the way to build your own docker image. Every time you have new changes you can run again: npm run docker-build
.
And if you have Docker Hub you can push it with: npm run docker:push
.
Also don’t forget to commit your git.
Let me know what you think of this article?