This week assignment from PT. Revolusi Cita Edukasi is "running a simple Node.js Project in Docker Container". These are 3 main steps to do:
- Docker Installations
- Create Files in Visual Studio Code
- Create Docker Image, Run Image(Container), and Test Localhost
First thing first, Here are some general steps to do that :
- Download docker installer and then install it. Do not forget to install or update Windows Subsystem Linux (WSL) especially WSL2 for Windows user, unless the docker cannot be run after installation is done.
- After docker and WSL2 installations are done, consider to install windows terminal for windows user to assist in creating node.js project image and creating container in docker.
- Download node.js installer as a Back up and install it if it is needed. Because, somehow author cannot get package-lock.json file generated if author does not do an npm install first which needs Node.js. Personally author try to not use npm install outside a docker container. Nevertheless, it is not succeded because there is no package-lock.json file in the docker container and then localhost and "Hello World" greetings does not appear in the browser.
- After all installations are done. The next step is to create files in VS code. Files needed to be created are Javascript file, Dockerfile and JSON file. After those 3 files are created, please run npm install on the same folder directory with those three files and then package-lock.json will be created. Lastly, create .dockerignore file to prevent node_modules folder to be copied into docker container.
- last step is create docker build image and run docker container then check localhost port ot test whether a simple node.js project can be run in the browser or not.
That's the general steps for run a simple node.js project in a docker container. Readme.md or this dok=cument will be updated if there is time left. Thank you.
contact author at [email protected]
-
Download and update Windows Subsystem Linux kernel version for windows user
-
Get windows terminal from Microsoft Store for windows user if needed and run the terminal.
-
Optional : WSL Extention, Docker Autostart Settings and Quit Docker
Author used nodejs org reference guide to create files in Visual Studio Code and then on Docker container later on.
nodejs docker web app guide
- Create package.json file
{
"name": "my_node_greet",
"version": "1.0.0",
"description": "Node.js image on Docker container",
"author": "Aldi Andalan <[email protected]>",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit1"
},
"dependencies": {
"express": "^4.18.2"
},
"License": "ISC"
}
- Create package-lock.json file via npm install in the same directory with package.json file especially if localhost cannot be run or cannot be shown in browser after containterization in docker.
- Create javascript file
const http = require('http');
const hostname = '0.0.0.0';
const port = 3001;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello RT3 RW1');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
- Create Dockerfile and .dockerignore before build image in Docker and containerize it.
# download node image
FROM node:20-alpine
# put node image and create folder as its place
WORKDIR /usr/src/greet
# Please copy files in those folder
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json?? are copied
# where available (npm@5+)
COPY package*.json ./
# run the npm
RUN npm install
# Bundle greet source
COPY . .
# expose to this port
EXPOSE 3001
CMD ["node", "index.js"]
node_modules
npm-debug.log
-
Open Windows Terminal, Change directory into a folder that has file needed to build docker image and container. Docker application should be opened too.
-
See list of directory to recheck the files in it. If it has the correct file then proceed.
-
Run docker image, specify port to open its image isolation into pc and browser. Container id shall be produced
-
Check container id using "docker ps" command and localhost link via "docker logs" command and then execute it
-
If command bin/bash cannot be run in previous command then use bin/sh
-
Ctrl + click localhost link that previously produced by "docker logs" command and change it to localhost:3002
Voila ! the node.js greetings is opned in localhost:3002