[英]How to run node js docker container using docker-composer to manage php app assets
Lets say we have three services - php+ apache - mysql - nodejs假设我们有三个服务 - php+ apache - mysql - nodejs
I know how to use docker-compose to setup application to link mysql with php apache service.我知道如何使用 docker-compose 来设置应用程序以将 mysql 与 php apache 服务链接起来。 I was wondering how we can add node.js service just to manage js/css assets.我想知道我们如何添加 node.js 服务来管理 js/css 资产。 The purpose of node.js service is to just manage javascript/css resources. node.js 服务的目的只是管理 javascript/css 资源。 Since docker provides this flexibility I was wondering to use docker service instead of setting up node.js on my host computer.由于 docker 提供了这种灵活性,我想使用 docker 服务而不是在我的主机上设置 node.js。
version: '3.2'
services:
web:
build: .
image: lap
volumes:
- ./webroot:/var/www/app
- ./configs/php.ini:/usr/local/etc/php/php.ini
- ./configs/vhost.conf:/etc/apache2/sites-available/000-default.conf
links:
- dbs:mysql
dbs:
image: mysql
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_PASSWORD=rest
- MYSQL_DATABASE=symfony_rest
- MYSQL_USER=restman
volumes:
- /var/mysql:/var/lib/mysql
- ./configs/mysql.cnf:/etc/mysql/conf.d/mysql.cnf
node:
image: node
volumes:
- ./webroot:/var/app
working_dir: /var/app
I am not sure this is correct strategy , I am sharing ./webroot
with both web and node service.我不确定这是正确的策略,我正在与 Web 和节点服务共享./webroot
。 docker-compose up -d
only starts mysql and web and fails to start node container , probably there is not valid entrypoint set. docker-compose up -d
仅启动 mysql 和 web 并且无法启动节点容器,可能没有设置有效的入口点。
You can also add nginx service to docker-compose, and nginx can take care of forwarding requests to php container or node.js container.也可以在docker-compose中添加nginx服务,nginx可以负责将请求转发到php容器或node.js容器。 You need some server that binds to 80 port and redirect requests to designated container.您需要一些绑定到 80 端口并将请求重定向到指定容器的服务器。
if you want to use node js separate from PHP service you must set two more options to make node stay up, one is stdin_open
and the other one is tty
like bellow如果要使用与 PHP 服务分开的节点 js,则必须再设置两个选项以使节点保持运行状态,一个是stdin_open
,另一个是tty
,如下所示
stdin_open: true
tty: true
this is equivalent to CLI command -it
like bellow这相当于 CLI 命令-it
像波纹管一样
docker container run --name nodeapp -it node:latest
if you have a separate port to run your node app (eg your frontend is completely separate from your backend and you must run it independently from your backend like you must run npm run start
command in order to run the frontend app) you must publish your port like bellow如果您有一个单独的端口来运行您的节点应用程序(例如,您的前端与后端完全分开,并且您必须独立于后端运行它,就像您必须运行npm run start
命令以运行前端应用程序一样),您必须发布您的像波纹管一样的端口
ports:
- 3000:3000
ports structure is systemPort:containerInnerPort
.端口结构是systemPort:containerInnerPort
。
this means publish port 3000 from inside node container to port 3000 on the system, in another way your make port 3000 inside your container accessible on your system and you can access this port like localhost:3000
.这意味着将端口 3000 从节点容器内部发布到系统上的端口 3000,以另一种方式使容器内的端口 3000 在您的系统上可访问,您可以像localhost:3000
一样访问此端口。
in the end, your node service would be like bellow最后,您的节点服务将如下所示
node:
image: node
stdin_open: true
tty: true
volumes:
- ./webroot:/var/app
working_dir: /var/app
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.