我正在尝试使用 docker-compose 组织应用程序基础架构。这是源代码:
服务器.js
const fastify = require('fastify');
const app = fastify();
app.get('/', (_req, res) => {
res.send('Hello world!');
});
app.listen({port: 3333, host: '0.0.0.0'}, (error) => {
if (error) {
console.error(error);
process.exit(1);
}
console.info(`fastify server listening...`);
});
码头工人-compose.yml:
version: '3.2'
services:
mysql:
image: mysql
...
ports:
- 3306:3306
webserver:
image: nginx:mainline-alpine
container_name: webserver
ports:
- 80:80
volumes:
- ./nginx-conf:/etc/nginx/conf.d
depends_on:
- nodejs
networks:
- app-network
nodejs:
build: ./node
container_name: nodejs
hostname: nodejs
command: npm run start
volumes:
- ./node:/node_app/
ports:
- 3333:3333
networks:
- app-network
networks:
app-network:
driver: bridge
nginx.conf
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location / {
proxy_pass http://nodejs:3333;
}
}
server {
listen 3333;
listen [::]:3333;
index index.html index.htm index.nginx-debian.html;
location / {
return 404;
}
}
我运行 docker-compose up,
从浏览器访问并http://localhost看到“Hello world!”。
但http://localhost:3333我也看到“你好世界!”。
如何从外界关闭 Node 应用程序?
结果对我来说也是如此,基地也发光了。
一方面,这很好,因为。对于本地开发,我可以使用 MySQL Workbench,另一方面,它必须关闭才能用于生产。
如何正确组织容器在开发和生产中的可见性?
您可以从 docker-compose 中删除端口,此设置只是将端口打开到外部,而在 app-network 网络(对于其他容器)内部,它们无论如何都是可用的。这很容易通过删除端口并将“左”容器连接到网络来检查,服务器将可用。