首页 > 编程语言 >[Docker] Docker Node.js project tips

[Docker] Docker Node.js project tips

时间:2023-08-01 14:57:14浏览次数:37  
标签:Node node index -- app js project Docker COPY

Basic node image

FROM node:12-stretch

COPY index.js index.js

CMD ["node", "index.js"]

Build docker image: docker build -t my-node-app .

--init

docker run my-node-app, after docker is running, you press CTRL + C, it doesn't exit node process, to pass down the CTRL + C to node process as well, we can do:

docker run --init my-node-app

If you want to remove the container after exit:

docker run --init --rm my-node-app

If you want to expose node.js app port (4040) to outside world (3030)

docker run --init --rm --publish 3030:4040 my-node-app

USER node

Node image ship your a USER called node and a group called node which you can use to run without your root user.

# Wrong
FROM node:12-stretch

COPY index.js index.js

USER node

CMD ["node", "index.js"]

If we do like that, it's still wrong. Because COPY index.js index.js happens before USER node, so it still use root user.

# Correct
FROM node:12-stretch

USER node

COPY --chown=node:node index.js index.js # --chown=<user>:<group>

CMD ["node", "index.js"]

Rebuild: docker build -t my-node-app .
Rerun: docker run --init --rm --publish 3030:4040 my-node-app whoami
Output: node

COPY vs ADD

COPY --chown=node:node index.js index.js and ADD --chown=node:node index.js index.js, on your local mahcine, it pretty much doing the same thing.

COPY: do less work than ADD
ADD: if the file is not in your local machine, it will download from network. If it's a zip file, ADD will download it and unzip it.

WORKDIR

Currently, it copy the file to the root directory of the container.

FROM node:12-stretch

USER node

WORKDIR /home/node/code # home dir, node user, code folder

COPY --chown=node:node index.js index.js 

CMD ["node", "index.js"]

Node.js with deps

  1. First we want to install dependencies inside container
FROM node:12-stretch

USER node

# create a directory for the app code as node user
# to resolve permission issue
RUN mkdir home/node/code 

WORKDIR /home/node/code

COPY --chown=node:node . .

# install dependencies
RUN npm ci 

CMD ["node", "index.js"]

Buiid: docker build -t my-node-app .
Run: docker run --init --rm --publish 3000:3000 my-node-app ls -lsah
Output:

8.0K drwxr-xr-x 1 node node 4.0K Aug  1 06:27 .
8.0K drwxr-xr-x 1 node node 4.0K Aug  1 06:28 ..
4.0K -rw-r--r-- 1 node node  148 Aug  1 06:27 Dockerfile
   0 -rw-r--r-- 1 node node    0 Aug  1 06:21 README.md
4.0K -rw-r--r-- 1 node node  554 Aug  1 06:17 index.js
8.0K drwxr-xr-x 1 node node 4.0K Aug  1 06:28 node_modules
 56K -rw-r--r-- 1 node node  55K Aug  1 06:19 package-lock.json
4.0K -rw-r--r-- 1 node node  309 Aug  1 06:19 package.json

As you can see, all the files and folders belongs to node group and user.

One thing to notice that, in the node.js app code:

  const server = hapi.server({
    host: "0.0.0.0",
    port: process.env.PORT || 3000,
  });

It uses 0.0.0.0 as host, you cannot change it to localhost. This will not allow it to escape the container when you run it on localhost. It's a hard loop back that it cannot escape itself. Bind it to 0.0.0.0, allow you to escape outside the container.

EXPOSE

You can add EXPOSE 3000 as documentation.

FROM node:12-stretch

USER node

# create a directory for the app code as node user
# to resolve permission issue
RUN mkdir home/node/code 

WORKDIR /home/node/code

COPY --chown=node:node . .

# install dependencies
RUN npm ci

EXPOSE 3000

CMD ["node", "index.js"]

Layers

Each line in Docker file has its own layer. For example COPY --chown=node:node . . has it's own layer, Docker will check whether the layer output different from cache or not, if it is, then Docker will re-calcaulte.

The problem here for the current docker file is that, if we change our source code, then COPY --chown=node:node . . is changed, we need to re-install packages again and again.

So the improved version:

FROM node:12-stretch

USER node

# create a directory for the app code as node user
# to resolve permission issue
RUN mkdir home/node/code 

WORKDIR /home/node/code

COPY --chown=node:node package.json package-lock.json ./

RUN npm ci

COPY --chown=node:node . .

EXPOSE 3000

CMD ["node", "index.js"]

.dockerignore

Every project should have .dockerignore file

node_modules/
.git/

标签:Node,node,index,--,app,js,project,Docker,COPY
From: https://www.cnblogs.com/Answer1215/p/17596487.html

相关文章

  • Docker - Nginx
    Nginx运行命令dockerrun--namecontainerName-p80:80-dnginx命令解读:dockerrun:创建并运行一个容器--name:给容器起一个名字,比如叫做mn-p:将宿主机端口与容器端口映射,冒号左侧是宿主机端口,右侧是容器端口-d:后台运行容器nginx:镜像名称,例如nginx 进入Nginx容器......
  • YAPI部署服务器(Docker)
    【一】YAPI介绍YApi是高效、易用、功能强大的api管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务。可以帮助开发者轻松创建、发布、维护API,YApi还为用户提供了优秀的交互体验,开发人员只需利用平台提供的接口数据写入工具以及简单的点击操作就可以实现接口的......
  • Node.js安装
    2.2管理nodejs版本下载安装nvm-windowshttps://github.com/coreybutler/nvm-windows/releases安装后在powershell输入nvm-h查看支持的命令 安装nodejsv16.12.0nvminstall16.12.0 将16.12.0设置当前使用版本nvmuse16.12.0 2.3全局安装yarnnpmiyarn......
  • 【宝塔面板部署nodeJs项目】网易云nodeJs部署在云服务器上,保姆级教程,写网易云接口用自
    看了很多部署的,要么少步骤,要么就是写的太简洁,对新手不友好前言参考链接服务器如何上线node.js项目【项目放置在github中】宝塔部署nodejs项目参考多篇文章,主要为上2篇,才总结本篇提示:这里可以添加本文要记录的大概内容:github传送门,可一键下载它的代码到本地运行网易云n......
  • Docker部署
    docker安装1、安装docker的依赖组件    在安装docker之前,我们需要先通过yum来安装docker的必要的依赖组件。同时为了编译后面的配置信息格式化输出,我们同时需要安装jq工具yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2yuminstal......
  • docker 不适合MySQL
    近几年Docker非常的火热,各位开发者恨不得把所有的应用、软件都部署在Docker容器中,但是您确定也要把数据库也部署的容器中吗?这个问题不是子虚乌有,因为在网上能够找到很多各种操作手册和视频教程,这里整理了一些数据库不适合容器化的原因供大家参考,同时也希望大家在使用时能够谨慎一......
  • docker 数据卷
    Docker镜像是由多个文件系统(只读层)叠加而成。当我们启动一个容器的时候,Docker会加载只读镜像层并在其上(镜像栈顶部)添加一个读写层。如果运行中的容器修改了现有的一个已经存在的文件,那该文件将会从读写层下面的只读层复制到读写层,该文件的只读版本仍然存在,只是已经被读写层中该......
  • Node.js 与前端开发实战
    0x1Node.js的应用场景前端工程化打包工具:webpack、vite、esbuild、parce代码压缩:uglifyjs语法转换:babeljs,typescript难以替代Web服务端应用学习曲线平缓,开发效率较高运行效率接近常见的编程语言社区生态丰富及工具链成熟(npm)与前端结合的场景会有优势(SSR)竞......
  • docker-ubuntu
    第一步拉取镜像dockerpullubuntu第二步运行容器dockerrun-itd--nameu1ubuntudockerrun-itd--nameu2ubuntu第三步进入容器dockerexec-itu1bash 第四步在u1容器内运行ipaddr命令结果如下: 和在虚拟机上运行ipaddr:apt-getinstall-yiprout......
  • centos 安装docker
    服务器的系统是CentOS7.4的[root@VM_82_178_centos~]#cat/etc/RedHat-releaseCentOSLinuxrelease7.4.1708(Core)#安装依赖包yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2#添加Docker软件包源yum-config-manager\--add-repo\https://download......