【发布时间】:2018-03-13 18:26:35
【问题描述】:
我有一个使用 docker-compose 在单个服务器上运行的 React/Node.js 应用程序。我正在尝试为我的反应应用程序实现 0 停机时间部署。现在的过程是进行 webpack 构建(替换我的 dist 文件夹中的文件),然后 docker down 和 docker up。整个过程大约需要 2-3 分钟。
我意识到使用 docker-compose 我可以向上/向下扩展我的容器,但我不确定如何只将我的代码推送到其中 1 个,重建 webpack 并 npm 重新启动它然后杀死其他容器。我真的不想使用 Kubernetes/Swarm 或 Openshift,因为它有点矫枉过正。我想知道是否还有其他人取得了类似的成就。
我的 docker-compose 看起来像这样:
node:
build:
context: ./env/docker/node
args:
- PROJECT_ROOT=/var/www/app
image: react_app:rapp_node
command: "npm run prod"
expose:
- "3333"
networks:
- react-net
volumes_from:
- volumes_source
tty: false
nginx:
env_file:
- ".env"
build:
context: ./env/docker/nginx
volumes_from:
- volumes_source
volumes:
- ./env/data/logs/nginx/:/var/log/nginx
- ./env/docker/nginx/sites/node.template:/etc/nginx/node.template
networks:
- react-net
- nginx-proxy
environment:
NGINX_HOST: ${NGINX_HOST}
VIRTUAL_HOST: ${NGINX_VIRTUAL_HOST}
LETSENCRYPT_HOST: ${NGINX_VIRTUAL_HOST}
ESC: $$
links:
- node:node
command: /bin/sh -c "envsubst < /etc/nginx/node.template > /etc/nginx/sites-available/node.conf && nginx -g 'daemon off;'"
volumes_source:
image: tianon/true
volumes:
- ./app:/var/www/app
而我的 nginx 是这样的:
server {
server_name www.${NGINX_HOST};
return 301 ${ESC}scheme://${NGINX_HOST}${ESC}request_uri;
}
server {
listen 80;
server_name ${NGINX_HOST};
root /var/www/app;
location / {
proxy_pass http://node:3333;
proxy_http_version 1.1;
proxy_set_header Upgrade ${ESC}http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host ${ESC}host;
proxy_cache_bypass ${ESC}http_upgrade;
}
}
【问题讨论】:
-
你为什么要做
down你可以只用build图像然后docker-compose up -d它只会创建那些新的contianers -
@MazelTov 因为我需要基本上杀死正在运行的节点服务器并重新启动 npm。如果您查看我的节点容器,它正在运行一个处理该问题的脚本。我没有对我的 Node.js 映像进行任何更改,因此不需要重新构建它。我们只是做出反应变化
-
那么你可以只做
docker-compose restart node,但如果你做down,它将删除网络,停止所有容器等等 -
我的意见是你应该在里面构建你的代码,而不是把代码文件夹挂载到同一个容器。
-
@MazelTov 我将尝试重启节点并让您知道。用内部代码构建图像是什么意思?是否意味着我不应该使用音量?
标签: docker kubernetes docker-compose docker-swarm