简体   繁体   English

如何对我在Amazon Linux AMI上托管的nodejs express应用程序进行docker化?

[英]how to dockerize my nodejs express application hosted on amazon linux ami?

1.my technology stack for above application is expressjs, nodejs, mongoDB, redisDB, s3(storage). 1.我用于上述应用程序的技术堆栈是expressjs,nodejs,mongoDB,redisDB,s3(storage)。 2.API is hosted on Linux AMI 3.I need to create docker container image for my application. 2.API托管在Linux AMI 3.我需要为我的应用程序创建docker容器映像。

First of all you will need to decide to either keep everything inside a single container (monolithic, cannot really recommend it) or separate the concern and run a separate express/nodejs container, a mongodb container, and a redisDB container, s3 is a service you cannot run for yourself, 首先,您需要决定将所有内容保存在单个容器中(整体式,不能真正推荐它),或者将问题分离并运行单独的express / nodejs容器,mongodb容器和redisDB容器,s3是一项服务你不能自己跑

If you chose the later approach, there are already officially supported images on the docker hub for redis, and mongo, now for the actual app server (node) you need to set express as a dependency on node and start the official node image with an npm install command (which would get express on it) and then npm start (or whatever command you use for it), dont forget to include your code as a volume for this to work, 如果选择后一种方法,则在docker hub上已经有正式支持的Redis和mongo映像,现在对于实际的应用程序服务器(节点),您需要将express设置为对节点的依赖关系,并以npm install命令(会在上面显示出来),然后再npm start(或您使用的任何命令),不要忘记将您的代码作为一个卷包含在内,以使其正常工作,

Now, bear in mind that if your app uses any reference data inside mongodb, you should make sure to insert it when the mongodb container starts or create an image based on the official mongodb that already has said data on it! 现在,请记住,如果您的应用程序在mongodb中使用了任何参考数据,则应确保在mongodb容器启动时将其插入,或基于已在其上声明数据的官方mongodb创建映像!

Another valuable note is that you should pass all connections inside your expressjs app as env vars, that way you can change them when deploying your app container (useful for when you distribute your system accross several hosts), 另一个值得注意的注意事项是,您应该将expressjs应用程序内的所有连接都作为env vars传递,这样您就可以在部署应用程序容器时更改它们(对于在多个主机之间分布系统时很有用),

At the end of the day you would then start the containers in this order: mongodb, redis, and node/express. 最后,您将按照以下顺序启动容器:mongodb,redis和node / express。 Now, the connection to s3 should already be handled inside your node app, so it is irrelevant in this context, just make sure the node app can reach the bucket! 现在,到s3的连接应该已经在您的节点应用程序内部处理了,因此在这种情况下无关紧要,只需确保节点应用程序可以到达存储桶即可!

If you want just to build a monolithic container, just start with a debian jessie image, get a shell inside the container, install everything as you would on a server, get your code running and commit the image to your repo, then use it to run your app, Still i cannot recommend this approach at all! 如果您只想构建一个整体的容器,则只需从一个debian jessie映像开始,在该容器中获取一个shell,像在服务器上一样安装所有内容,运行代码并将该映像提交到您的仓库中,然后将其用于运行您的应用程序,但我完全不推荐这种方法!

BR, BR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM