将Docker与具有node-gyp依赖项的nodejs一起使用 [英] Using Docker with nodejs with node-gyp dependencies

查看:120
本文介绍了将Docker与具有node-gyp依赖项的nodejs一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用Docker部署node.js应用程序.该应用程序具有几个依赖项,需要node-gyp.Node-gyp在交付平台上针对已编译的库构建这些模块(例如canvas,lwip,qrcode),以我的经验,这些构建可能高度依赖于o/s版本和所安装的库,并且它们经常破坏简单的 npm install .

I'm planning to use Docker to deploy a node.js app. The app has several dependencies that require node-gyp. Node-gyp builds these modules (e.g. canvas, lwip, qrcode) against compiled libraries on the delivery platform, and in my experience these builds can be highly dependent on the o/s version and libraries installed, and they often break a simple npm install.

那么构建我的Dockerfile FROM node:version 是正确的方法吗?到目前为止,这似乎是我在每个Docker/Node教程中展示的方法.但是,如果我从节点映像进行构建,那么在部署容器时会发生什么?如何确保目标主机具有编译node-gyp模块所需的库?

So is building my Dockerfile FROM node:version the correct approach? This seems to be the approach shown in every Docker/Node tutorial I've found so far. But if I build from a node image, what will happen when I deploy the container? How can I ensure the target host will have the libraries needed to compile the node-gyp modules?

我正在寻找的另一种方法是构建Dockerfile FROM ubuntu:version .但是我认为这意味着将nodeJS安装到Ubuntu映像中,整个过程将变得更大.

The other way I'm looking at is to build the Dockerfile FROM ubuntu:version. But I think this would mean installing nodeJS into the Ubuntu image and the whole thing would be much larger.

还有其他处理方式吗?

推荐答案

如何确保目标主机具有编译node-gyp模块所需的库?

How can I ensure the target host will have the libraries needed to compile the node-gyp modules?

目标主机也正在运行docker.只要依赖关系在您的映像中,那么服务器也将拥有它们.如果您问我,这就是码头工人的全部要点.如果它在本地运行,那么它也将在服务器上运行.

The target host is running docker as well. As long as the dependencies are in your image then your server has them as well. That's the entire point with docker if you ask me. If it runs locally, then it runs on the server as well.

我会使用node-alpine( FROM node:8-alpine )来获得更小的文件.在缠结之前,我一直在努力处理node-gyp,但是现在我什至看不到我曾经以为这是个问题.只要您添加构建工具 RUN apk add python make gcc g ++ ,您就可以使用(但是会增加100-200mb的大小).

I'd go with node-alpine (FROM node:8-alpine) for even smaller files. I struggled with node-gyp before I wrapped my head around it, but now I don't even see how I ever thought it was a problem. As long as you add build tools RUN apk add python make gcc g++ you are good to go (this adds some 100-200mb to the size however).

如果它也很费时(例如,您发现自己不时使用--no-cache重建映像),那么将其拆分为您自己的基本映像和另一个基本映像是一个好主意.图像 FROM my-base-image:latest ,其中包含您经常更改的内容.

Also if it ever gets time consuming (say you find yourself rebuilding your image with --no-cache every now and then) then it can be a good idea to split it up into a base-image of your own and another image FROM my-base-image:latest which contains things that you change a more often.

肯定有一些学习曲线,但是我没有发现它这么陡峭.如果您以前接触过docker,至少不会.

There is some learning curve for sure, but I didn't find it that steep. At least not if you have touched docker before.

我正在查看的另一种方法是从ubuntu:version构建Dockerfile.

The other way I'm looking at is to build the Dockerfile FROM ubuntu:version.

我在跳到docker之前只使用过CentOS,并且在服务器上运行CentOS.因此,我认为同时运行CentOS映像也是一个好主意,但我发现这很愚蠢.除非您需要特定于操作系统的内容,否则绝对没有零增益.现在我只使用了Alpine大约半年了,到目前为止,我唯一需要学习的特定于Alpine的命令是 apk add/del .

I had only used CentOS before jumping on docker, and I run CentOS on my servers. So I thought it would be a good idea to run CentOS-images as well, but I found that to be just silly. There is absolutely zero gain unless you need something very OS-specific. Now I've only used alpine for maybe half a year, and so far the only alpine-specific command I've needed to learn is apk add/del.

您可能已经知道,但是一开始不要花太多时间优化docker文件的大小.(您可以通过在一行上组合命令来大幅度减少层大小,(添加软件包,运行命令,删除软件包).但是,如果在较大的层中进行任何小更改,那将取消使用docker映像缓存.最好离开直到重要为止.

And you probably know already, but don't spend too much time optimizing docker file size in the beginning. (You can reduce layer size a lot by combining commands on one line, (adding packages, running command, removing packages). But that cancels out the use of the docker image cache if you make any small changes in big layers. Better to leave that out until it matters.

这篇关于将Docker与具有node-gyp依赖项的nodejs一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆