How to reload environment variables in docker-compose container with minimum downtime?

两盒软妹~` 提交于 2019-12-20 10:16:14

问题


docker-compose.yml

version: '2'
services:
  app:
    build:
      context: .
    command: python src/app.py
    restart: on-failure
    depends_on:
      - db
    environment:
      - TJBOT_DB_HOST=db
      - TJBOT_API_KEY
      - TJBOT_AUTO_QUESTION_TIME
    env_file:
      - .env
  db:
    image: mongo:3.0.14
    volumes:
      - mongodbdata:/data/db
volumes:
  mongodbdata:

If I change the .env file, how could I reload the container to use the new environment variables with minimum downtime?


回答1:


If you are running the yml with docker-compose, you can just run docker-compose up -d and it will recreate any containers that have changes and leave all unchanged services untouched.

$ cat docker-compose.env2.yml
version: '2'

services:
  test:
    image: busybox
    # command: env
    command: tail -f /dev/null
    environment:
      - MY_VAR=hello
      - MY_VAR2=world
  test2:
    image: busybox
    command: tail -f /dev/null
    environment:
      - MY_VAR=same ole same ole

$ docker-compose -f docker-compose.env2.yml up -d                                               
Creating network "test_default" with the default driver
Creating test_test_1
Creating test_test2_1

$ vi docker-compose.env2.yml # edit the file to change MY_VAR

$ docker-compose -f docker-compose.env2.yml up -d
Recreating test_test_1
test_test2_1 is up-to-date

If you run the containers as a docker stack deploy -c docker-compose.yml with a version 3 file format, you can do a rolling update of the service which will prevent any downtime if you have multiple instances of your service running. This functionality is still very new, you'll want 1.13.1 to fix some of the issues with updates, and as with anything this new, bugs are still being worked out.



来源:https://stackoverflow.com/questions/42149529/how-to-reload-environment-variables-in-docker-compose-container-with-minimum-dow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!