docker简单的node.js工作stream程
我在Windows上使用Docker进行开发,我正在尝试为node.js项目创build一个简单的工作stream程。
我跟着这个教程https://nodejs.org/en/docs/guides/nodejs-docker-webapp/所以我的Dockerfile看起来像这样
FROM node:boron # Create app directory WORKDIR /usr/src/app # Install app dependencies COPY package.json . # For npm@5 or later, copy package-lock.json as well # COPY package.json package-lock.json ./ RUN npm install # Bundle app source COPY . . EXPOSE 8080 CMD [ "npm", "start" ]
我的每个更改的“工作stream程”将如下所示
第一次build造
docker build -t thomas/myApp DockerProjects/myApp ; docker run --name app -p 49160:8080 -d thomas/myApp
每次更改后
docker build -t thomas/myApp DockerProjects/myApp ; docker stop app ; docker rm app ; docker run --name app -p 49160:8080 -d thomas/myApp
在项目每次更改之后,我不想拥有数百个容器,这就是为什么我要在创build另一个容器之前将其删除。
我看到几个问题:
- 每次发生更改,新图像都将生成,将创build一个新的
<none>:<none>
图像。 这些图像具有与原始图像相同的重量。 我怎样才能避免呢? - 我能以某种方式使用nodemon吗?
- 我能在每次更改代码中的某些内容时自动启动此过程吗?
Docker对我来说是相当新的,我仍然在试验它。 谢谢
您可以在您的项目中使用nodemon自动重启您的应用程序,同时将源代码目录安装在卷上。
例如,使用这个目录结构(使用package.json
Grunt
来运行nodemon):
app/ ├── Dockerfile ├── package.json ├── Gruntfile.js ├── src/ │ └── app.js └── docker-compose.yml
你可以使用docker-compose这个工具来运行多个容器。 如果您想要添加应用程序可以与之交互的数据库容器,或者与您的应用程序进行交互的其他服务,这会非常有用。
以下docker-composeconfiguration将在容器上的/usr/src/app/src
上挂载src
文件夹。 使用nodemon查找src
中的更改,您将能够对计算机进行更改,以便自动重启容器上的应用程序
要使用这个你会做:
cd app docker-compose up
上面的命令从dockerfile构build镜像并启动在docker-compose.yml
中定义的容器。
docker-compose.yml
:
version: '2' services: your-app: build: . ports: - "8080:8080" restart: always container_name: app_container volumes: - ./src:/usr/src/app/src environment: - SERVER_PORT=8080
Dockerfile
:
FROM node:latest RUN mkdir -p /usr/src/app WORKDIR /usr/src/app COPY package.json . COPY Gruntfile.js . RUN npm install CMD ["npm","start"]
Gruntfile.js:
var path = require('path'); module.exports = function (grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), concurrent: { dev: { tasks: ['nodemon'], options: { logConcurrentOutput: true } } }, nodemon: { dev: { script: 'src/app.js', options: { ignore: [ 'node_modules/**' ], ext: 'js' } } }, clean: {} }); grunt.loadNpmTasks('grunt-concurrent'); grunt.loadNpmTasks('grunt-nodemon'); grunt.registerTask('default', ['concurrent']); };
package.json:
{ "name": "your-app", "version": "1.0.0", "description": "service", "scripts": { "start": "grunt" }, "author": "someone", "license": "MIT", "dependencies": { "express": "^4.14.0" }, "devDependencies": { "grunt": "1.xx", "grunt-cli": "1.xx", "grunt-concurrent": "2.xx", "grunt-nodemon": "0.4.x" } }
示例app.js:
'use strict'; const express = require('express'); const port = process.env.SERVER_PORT; var app = express(); app.get('/', function(req, res) { res.send('Hello World'); }); app.listen(port, function() { console.log('listening on port ' + port); });
要重build图像,您将执行docker-compose build
Each time there is a change and a new image is build, a new <none>:<none> image is created. These images have the same weight as the original one. How can I avoid that ?
你不能。 这个:iamge是你以前的形象,被你的新形象所取代。 所以就把它删除吧: docker image prune
Can I use nodemon somehow?
我不熟悉,但它看起来只是重新启动你的服务器,但不会做一个npm install
。
Can I launch this process automatically each time I change something in the code?
我会使用Jenkins,并在每个git commit上自动构build新的Docker镜像。