OS: Windows 11 Version 22H2
Docker Desktop: 4.21.1.114176
NodeJS Docker Image: Version 18.17.0(LTS)
-
Download docker desktop from www.docker.com and run it
6v_1.mp4
-
Update Windows Subsystem for Linux with this command and run docker desktop again
wsl --update
6v_2.mp4
-
Verify docker installation with this command
docker --version
-
Download nodejs version 18.17.0(LTS) image on docker with this command
docker pull node:18.17.0-alpine
6v_3.mp4
-
For other version of nodejs images tag, you can check at this link https://hub.docker.com/_/node/
- Download app.js from https://gist.github.com/berdoezt/e51718982926f0caa3fcd8ed45111430 and move to new folder
- Create file with name Dockerfile, edit file with this code, and save at same folder with app.js
#Tells docker base image to use #nodejs version 18.17.0-alpine(LTS version) FROM node:18.17.0-alpine #The environment application is production ENV NODE_ENV=production #Location of working directory WORKDIR /app #The COPY command takes all the files located in #the current directory and copies them into the image COPY . . #Informs Docker that the container listens #on the port 3001 EXPOSE 3001 #Tell Docker what command to run CMD ["node", "app.js"]
-
Open folder of the app on command prompt, after that run this command
docker build -t nodejs_hello-world .
-
Check images succesful built with this command
docker images
-
Run app with this command(binds port 3001 of container to port 3000 of host)
docker run -p 3000:3001 nodejs_hello-world
-
Finally, open browser to check app running well with this link
localhost:3000
6v_4.mp4