Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy a nodejs service in Dockerfile

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to deploy a nodejs service in Dockerfile? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Initialize Dockerfile

Suppose our project is named express and create and edit the Dockerfile file in the express project:

$vim DockerfileFROM node:latestRUN mkdir-p / home/www/expressWORKDIR / home/www/expressCOPY. / home/www/expressRUN npm installEXPOSE 3000ENTRYPOINT ["npm", "run"] CMD ["start"]

This file contains the following commands:

FROM node:latest-specifies to use the latest version of the node base image

RUN mkdir-p / home/www/express-create a / home/www/express directory within the container

WORKDIR / home/www/express-set the working directory in the container to / home/www/express

COPY. / home/www/express-copy the contents of the current directory of the host to the mirror / home/www/express directory

RUN npm install-npm install the NPM package required to install the application

EXPOSE 3000-Port 3000 of the open container

ENTRYPOINT ["npm", "run"]-the command executed after the container starts. Cannot be overridden by parameters provided by docker run

CMD ["start"]-commands executed when the container is started, which can be overridden by parameters provided by docker run

Build an image

After writing the Dockerfile file, you can use the docker build command to build the image:

$sudo docker build-t test/express.

We name the mirror test/express with the-t parameter. The build process is similar to the following:

Sending build context to Docker daemon 29.7 kBStep 1 home/www/express-app 8: FROM registry.src.followme.com:5000/node:v1-> c99c549e8227Step 2 ax 8: RUN mkdir-p / home/www/express-app-- > Running in 8be9a90629b0-- > b9f584851225Removing intermediate container 8be9a90629b0Step 3 home/www/express-app 8: WORKDIR / home/www/express-app-- > 5072c31f9dd9Removing intermediate container e9dbf4ce3d8bStep 4 max 8: COPY. / home/www/express-app-> a4d1725f15edRemoving intermediate container 30aa49765015Step 5amp 8: RUN yarn-> Running in f181c243deaayarn install v1.3.2 [1apt 4] Resolving packages... [2max 4] Fetching packages... [3max 4] Linking dependencies... [4max 4] Building fresh packages...Done in 9.46s. -> d390931d73e6Removing intermediate container f181c243deaaStep 6Compact 8: EXPOSE 3000-- > Running in 94101ab38864-- > 43199a8a5a90Removing intermediate container 94101ab38864Step 7Accord 8: ENTRYPOINT npm run-> Running in 80b1318962cf-- > 6b203c50e855Removing intermediate container 80b1318962cfStep 8Compare 8: CMD start-> Running in a9909e537f59-- > d56eae48377cRemoving intermediate container a9909e537f59Successfully built d56eae48377c

Run the container

After the construction of the image is completed, you can create / run the container through the constructed image, thus realizing the Docker of the express application.

Use the tets/express image to run a container:

$sudo docker run-d-name experss-app-p 3000UR 3000 test/express

In the above operation, we run the container through the test/express image and name the container experss-app. To run the container, we also specify the-d parameter, which makes the container run in the background. The-p parameter maps port 3000 of the host to port 3000 of the container. After running the container, you can see the running container through the docker ps command. The service can be accessed through localhost:3000 at this time.

This is the answer to the question about how to deploy a nodejs service in Dockerfile. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report