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

Docker uses writing dockerfile to start node.js application

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

Share

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

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 basic image is version 10.15 of node (the official version of node can be viewed here) MAINTAINER indicates that the maintainer of the image COPY command copies the host files to the image in the format of COPY [--chown=:]. Here, all 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 the pm2EXPOSE command to declare that the runtime container provides a service port, but note that the service on this port is not opened at run time. 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 CMD that automatically randomly maps EXPOSE 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

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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