Git Product home page Git Product logo

week-6-andalanaldi's Introduction

Review Assignment Due Date

A Simple Node.js Project Run in Docker Container

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:

  1. Docker Installations
  2. Create Files in Visual Studio Code
  3. Create Docker Image, Run Image(Container), and Test Localhost

First thing first, Here are some general steps to do that :

Docker Installations

  1. 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.
  2. 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.
  3. 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.

Create Files in Visual Studio Code

  1. 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.

Create Docker Image, Run Image(Container), and Test Localhost

  1. 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]

Documentation Update 1: Docker Installations

  1. Download Docker installer. Download Docker installer

  2. Run Docker installer. Install Docker

  3. Wait for the installation process Wait for the process

  4. Read and accept Docker Subscription Service Agreement Service Agreement

  5. Wait for Docker Engine to start Start Docker Engine

  6. Download and update Windows Subsystem Linux kernel version for windows user Windows Subsystem Linux

  7. Get windows terminal from Microsoft Store for windows user if needed and run the terminal. Windows Terminal

  8. Set WSL2 as Default in the terminal Set Windows Subsystem Linux 2 as default

  9. Optional : WSL Extention, Docker Autostart Settings and Quit Docker WSL Extention, Docker Autostart and Quit Docker

Documentation Update 2: Create Files in Visual Studio Code

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 dockerize nodejs web app

  1. Create package.json file

package.json

{
  "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"
}
  1. 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.

npm install to generate lock file

npm install to generate lock file

lock file generated

lock file generated

  1. Create javascript file

index.js

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}/`);
});
  1. Create Dockerfile and .dockerignore before build image in Docker and containerize it.

Dockerfile

# 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"]

.dockerignore

node_modules
npm-debug.log

Documentation Update 3: Create Docker Image, Run Image(Container), and Test Localhost

  1. Open Windows Terminal, Change directory into a folder that has file needed to build docker image and container. Docker application should be opened too. Change Directory

  2. See list of directory to recheck the files in it. If it has the correct file then proceed. List of folder

  3. Build a node.js image in Docker and give a tag name Build a docker image

  4. Run docker image, specify port to open its image isolation into pc and browser. Container id shall be produced Run docker image

  5. Check container id using "docker ps" command and localhost link via "docker logs" command and then execute it Container id, logs and execution

  6. If command bin/bash cannot be run in previous command then use bin/sh Execution option

  7. Ctrl + click localhost link that previously produced by "docker logs" command and change it to localhost:3002 Localhost display

Voila ! the node.js greetings is opned in localhost:3002

week-6-andalanaldi's People

Contributors

andalanaldi avatar github-classroom[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.