零停机时间部署节点。js/NGINX Docker

Zero Down Time Deployment Node.js/NGINX Docker

我在使用 docker-compose 的单个服务器上有一个 React/Node.js 应用程序 运行。我正在尝试为我的 React 应用实现零停机部署。现在的过程是构建 webpack(替换我的 dist 文件夹中的文件),然后 docker down 和 docker up。整个过程大约需要 2-3 分钟。

我意识到使用 docker-compose 我可以扩展我的容器 up/down 但我不确定如何只将我的代码推送到其中一个,重建 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;
}
}

刷新容器的更快方法是

docker-compose restart node

因为如果你这样做 docker-compose down 它会关闭所有服务,删除配置的网络。

如果您有扩展服务,您可以尝试使用

重新启动它
docker restart foldername_node_2

你可以有 0 个停机时间,但是 nginx 配置不够,它会随机 select node 机器,所以你将不得不实施一些备份服务器