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 write dockerfile and start node.js Application with Docker

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "Docker how to write dockerfile to start node.js application", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Docker how to write dockerfile to start node.js application"!

Write dockerfile

Take the directory automatically created by express as an example, the directory structure is as follows:

├── / bin │ └── www ├── / node_modules ├── / public ├── / routes ├── / views ├── package-lock.json ├── package.json ├── ecosystem.config.js ├── app.js └── dockerfile

Create a new dockerfile file in the project directory

From node:10.15maintainer sunhengzhe@foxmail.comcopy. / app/workdir / apprun npm install pm2- gexpose 8003cmd ["pm2-runtime", "ecosystem.config.js"]

From specifies that the base image is version 10.15 of node (official version of node is available)

Maintainer indicates the maintainer of the image

The copy command copies the files of the host to the image in the format of copy [--chown=:]. Here, all the files under the project directory are copied to the / app directory in the image. If the destination path does not exist, the docker is created automatically.

Workdir is used to specify the working directory, that is, the directory where the cmd execution is located.

The run command is used to execute the shell command, which is used to install pm2

The expose command is used to declare that the runtime container provides a service port, but note that the runtime does not open the service for this port. This command is mainly to help consumers understand the daemon port of the mirror service to facilitate the configuration of mapping; in addition, when using random port mapping, the port of expose will be automatically randomly mapped

Cmd is the default startup command for the container main process

Build an image

Execute under the project directory

Docker build-t express-app:v1.

If the build is successful, view the list of images

Docker images

The mirror express-app:v1 should be output

Run the container

Docker run-d-p 8003 express-app 3000-- name= "express-app" 3000

Docker run is an abbreviation for the commands docker create and docker start.

-d means-- detach, which means to let the container run in the background.

-p specifies the port mapping between the host and the container. The port of the host on the left and the port of the container on the right, that is, the port 8003 of the host will be mapped to the port 3000 in the container.

-- name sets the container alias. If it is not specified, docker will randomly generate a name, such as tender_swirles.

Execution

Docker ps

The normal display is as follows

Add the-a parameter to view all started containers.

Enter the container

If you want to enter the container for operation, execute the following command

Docker exec-it express-app bash

-I is generally used in conjunction with-t,-I starts the interaction mode,-t specifies that the terminal needs to be assigned, and you can try not to send one of the effects on your own.

Similar to exec is the attach command, which is docker attach express-app, but exit from this stdin will cause the container to stop, so the exec command is recommended.

Close operation

Stop the container

Docker stop express-app

Delete Container

Docker rm express-app

If the container is still running when it is deleted, you need to add the-f parameter

Delete Mirror

Docker rmi express-app:v1 thank you for your reading, the above is the content of "how Docker compiles dockerfile to start node.js applications". After the study of this article, I believe you have a deeper understanding of how Docker compiles dockerfile to start node.js applications, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report