如何在Docker中运行NodeJS命令行工具?
上下文
我正在为我们的开发环境创build一个大的Docker组合。 我们用:
- nginx的
- PHP
- MySQL的
- MongoDB的
为我们开发的networking应用提供服务。
我们也使用一些使用NodeJS的命令行工具:
- Grunt(编译SCSS)
- JSHint
所以我有一个docker-compose.yml
和五个Dockerfile
(每个服务一个,一个包含NodeJS)。 build筑工程。
Dockerconfiguration
docker-compose.yml
version: '2' services: web: build: ../app-web/ ports: - "80:80" tty: true # Add a volume to link php code on the host and inside the container volumes: - /var/www/my_app:/usr/share/nginx/html/my_app - /var/www/my_app/docker_files/docker-assistant:/usr/share/nginx/html/assistant # Add hostnames to allow devs to call special url to open sites extra_hosts: - "localhost:127.0.0.1" - "assistant.docker:127.0.0.1" - "my_app.my_company.dev:127.0.0.1" depends_on: - custom-php - custom-mysql - custom-mongo - custom-node links: - custom-php:custom-php - custom-mysql:custom-mysql - custom-mongo:custom-mongo custom-php: build: ../app-php/ ports: - "50:50" volumes: - /var/www/my_app:/usr/share/nginx/html/my_app - /var/www/my_app/docker_files/docker-assistant:/usr/share/nginx/html/assistant custom-mysql: build: ../app-mysql/ #tty: true volumes: - /var/lib/mysql:/var/lib/mysql - /var/www/my_app/sql:/usr/share/sql environment: - MYSQL_ROOT_PASSWORD=rootpwd - MYSQL_DATABASE=appdb - MYSQL_USER=app - MYSQL_PASSWORD=apppwd custom-mongo: build: ../app-mongo/ volumes: - /var/lib/mongodb:/data/db custom-node: build: ../app-npm/ tty: true
节点Dockerfile
FROM node:7.8.0-alpine MAINTAINER DarckCrystale "xxx@xxx.xxx" CMD [ "node" ]
题
我如何从Docker容器(eG Grunt)运行NodeJS命令行工具?
例如,当我需要编译SCSS时,没有Docker,我去我的应用程序文件夹:
cd /var/www/my_app_folder
我运行咕噜声:
grunt
要么
grunt watch
但我不知道如何使用我的节点的Docker容器做到这一点。
你可以在你的Dockerfile
添加CMD
和WORKDIR
。 对于你的具体例子,尝试添加到你的Dockerfile
WORKDIR /var/www/my_app_folder CMD grunt // or CMD grunt watch
有关更多详细信息,请参阅Digital Ocean的链接
————–编辑—————
如果你想要根据你的开发场景执行它,请尝试以下操作:
@darkckcrystale I see well I was giving you production level solution, well in dev case try running following // This will list all the running containers $ docker ps // output CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4c01db0b339c ubuntu:12.04 bash 17 seconds ago Up 16 seconds 3300-3310/tcp webapp d7886598dbe2 crosbymichael/redis:latest /redis-server --dir 33 minutes ago Up 33 minutes 6379/tcp redis,webapp/db $ docker exec -it 4c01db0b339c grunt // docker exec [options] [container id/name] <command>
有关更多详情,请查看此链接
用docker exec你可以在容器中运行命令,例如:
docker exec my-container-name grunt
更多信息: https : //docs.docker.com/engine/reference/commandline/exec/
- Docker:来自守护进程的错误响应:OCI运行时创build失败:container_linux.go:296:
- 无法通过Docker中的node.js连接到MongoDB
- nodeJS dockerfile构build失败与NPM
- 将docker-compose文件转换为构build和运行命令
- 将node.js移动到一个docker容器,收到一个can not file模块错误
- Flask web应用程序,Node.js(队列)worker。 不能使docker工作
- 尽pipe项目位于泊坞窗容器上,但在我的IDE上仍可使用Lintingfunction
- Webpack不会生成build.js
- Docker端口转发为nodejs应用程序