+62 858 7607 6005
elgibran@iegcode.com
Sleman, Yogyakarta.
blog-img
11/08/2024

Pendidikan

Ibrahim El Gibran | Pendidikan
Development Node JS with Docker, Sample run and watch change Node JS Express with Docker

Build docker images and running container application for nodejs.

View Github

Before running the docker application, first create a nodejs project. Open browser klick "Express" application generator. Make sure you have express installed. If it has been installed, create the project with the command:
$ express --view=pug myapp

If so, you need to continue installing the dependency.

$ cd myapp.

$ npm install.

Next if it is successful. Download the "Docker desktop" application via the official website.

Install docker on your respective OS or operating system. If it has been installed, you can open the Docker desktop application.

next if the installation is successful. Open text editor visual code studio and create a "Dockerfile" in the root folder.

If it has been created then fill in the code in the Dockerfile file.

The next explanation for the code above is: Just setup for development mode, for example in local, when there is a change in the code, you can run it directly via Docker.

The explanation of the code is:

→ FROM : images from where, for example, initiating a project so you can use it from node:16 can be read in docker hub node.

→ WORKDIR : when it reaches the contest it will immediately initiate which is free in this case I entered it in /usr/app.

→ COPY . . : To copy from the current local directory to the container and in this case everything will be copied in this folder. Example: COPY. . (Resource walkway and destination point).

→ RUN npm install : I want to run a sample comment.

→ EXPOSE 3000 : Next, expose the port that is running on the express.

→ CMD [“nam”, “start”] : The last comment you want to make is to run the app.

This is the configuration for generating images for docker.

Step 1

This is so that the Docker container can, in other words, watch local changes, it's called a volume, it can sort of synchronize. Still using the image configuration that was created earlier.

Next build the app:

$ docker build . -t ieg-run

Check the build results:

$ docker images

The images are ready, just run as a container:

You can search with the command: $ pwd
Then hassel : /Users/elgibran17/Documents/Dekstop/
myapp

Then when you want to run the container.

add the volume: $ docker run --volume=/Users/elgibran17/
Documents/Dekstop/myapp:/usr/app -p 3000:3000 -d ieg-run

Note: add the destination:/usr/app

View running containers:

$ docker ps

Additionally, you can also use it via the Docker desktop application.
Example:

Open run images, input volumes or host path and destination or container path.

Check container, make sure it runs smoothly:

Check files, ensure the build results are successful:

more or less.

Step 2

Open the desktop application docker, Make sure it is connected.

Next one, check the container on the application desktop.

Next two, check the browser for access port: "localhost:3000".

Next I tried to edit the code page.

After saving, it will automatically be updated with the composer connected to our OS.

In conclusion, Docker helps run applications in containers without the need for a separate physical or virtual server. For example, commands like npm start can be run in a Docker container, which makes them very useful for running programs and applications quickly and consistently across different environments. Docker allows developers to package an application and all its dependencies into a single standard unit that can be run anywhere.

Thank you for coming, see you soon.

Bagikan Ke:

Kategori

Populer