Docker Swarm-如何为各种节点上的任务设置环境变量

时间:2019-02-11 18:39:30

标签: docker docker-compose environment-variables dockerfile swarm

我是docker swarm的新手,可以在各种节点上部署我的服务,但是,从dockerfile ENTRYPOINT脚本导出的环境变量未为docker swarm中部署的任务设置集群。

设置

  • docker版本18.09.1,内部版本4c52b90
  • docker-compose版本1.23.2,内部版本1110ad01
  • Django 2.1.5
  • PosgresSQL 10

尝试在django任务中执行一次性命令,使用docker exec -t CONTAINER_ID sh进入容器,然后执行python manage.py migrate,出现以下错误:

错误

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 38, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 211, in fetch_command
    settings.INSTALLED_APPS
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 107, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/app/config/settings/production.py", line 15, in <module>
    DATABASES['default'] = env.db('DATABASE_URL')  # noqa F405
  File "/usr/local/lib/python3.6/site-packages/environ/environ.py", line 202, in db_url
    return self.db_url_config(self.get_value(var, default=default), engine=engine)
  File "/usr/local/lib/python3.6/site-packages/environ/environ.py", line 275, in get_value
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable

因此,DATABASE_URL未设置为我的Docker容器内的环境变量。如前所述,这是从ENTRYPOINT脚本导出的,该脚本在dockerfile中调用。

Dockerfile

FROM python:3.6-alpine

ENV PYTHONUNBUFFERED 1

RUN apk update \
  # psycopg2 dependencies
  && apk add --virtual build-deps gcc python3-dev g++ musl-dev \
  && apk add postgresql-dev \
  # Pillow dependencies
  && apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
  # CFFI dependencies
  && apk add libffi-dev py-cffi \
  # Translations dependencies
  && apk add gettext \
  # https://docs.djangoproject.com/en/dev/ref/django-admin/#dbshell

# Requirements are installed here to ensure they will be cached.
COPY ./requirements /requirements
RUN pip install -r /requirements/production.txt \
    && rm -rf /requirements

COPY ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r//' /entrypoint
RUN chmod +x /entrypoint
RUN chown django /entrypoint

COPY ./compose/production/django/start /start
RUN sed -i 's/\r//' /start
RUN chmod +x /start
RUN chown django /start

COPY ./compose/production/django/celery/worker/start /start-celeryworker
RUN sed -i 's/\r//' /start-celeryworker
RUN chmod +x /start-celeryworker
RUN chown django /start-celeryworker

COPY ./compose/production/django/celery/beat/start /start-celerybeat
RUN sed -i 's/\r//' /start-celerybeat
RUN chmod +x /start-celerybeat
RUN chown django /start-celerybeat

COPY ./compose/production/django/celery/flower/start /start-flower
RUN sed -i 's/\r//' /start-flower
RUN chmod +x /start-flower

COPY . /app

RUN chown -R django /app

USER django

WORKDIR /app

ENTRYPOINT ["/entrypoint"]

ENTRYPOINT脚本

#!/bin/sh

set -o errexit
set -o pipefail
set -o nounset


# N.B. If only .env files supported variable expansion...
export CELERY_BROKER_URL="${REDIS_URL}"

if [ -z "${POSTGRES_USER}" ]; then
    base_postgres_image_default_user='postgres'
    export POSTGRES_USER="${base_postgres_image_default_user}"
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"

postgres_ready() {
python << END
import sys

import psycopg2

try:
    psycopg2.connect(
        dbname="${POSTGRES_DB}",
        user="${POSTGRES_USER}",
        password="${POSTGRES_PASSWORD}",
        host="${POSTGRES_HOST}",
        port="${POSTGRES_PORT}",
    )
except psycopg2.OperationalError:
    sys.exit(-1)
sys.exit(0)

END
}
until postgres_ready; do
  >&2 echo 'Waiting for PostgreSQL to become available...'
  sleep 1
done
>&2 echo 'PostgreSQL is available'

exec "$@"

这取自pydanny的django-cookie-cutter项目。一切都使用正常的非群设置进行:docker-compose -f production.yml builddocker-compose -f production.yml up用于单实例生产部署。

最后,这是我的docker-compose文件在群中的样子:

Docker-compose.yml

version: '3.6'

volumes:
  production_postgres_data: {}
  production_postgres_data_backups: {}
  production_caddy: {}
  node-modules:

networks:
  webnet:
    driver: overlay
    attachable: true

services:
  django: &django
    image: registry:image
    depends_on:
      - postgres
      - redis
    env_file:
      - PATH to .env

    command: /start
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
        delay: 5s
    networks:
      -  webnet

  postgres:
    image: registry:image
    volumes:
      - production_postgres_data:/var/lib/postgresql/data
      - production_postgres_data_backups:/backups
    env_file:
      - PATH to .env
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
      placement:
        constraints:
          - node.role == manager
    networks:
      - webnet

  frontend:
    image: registry:image
    command: /start
    volumes:
      - node-modules:/app/node_modules
    ports:
      - "3000:3000"
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
        delay: 5s
    networks:
      -  webnet

  caddy:
    image: registry:image
    depends_on:
      - django
      - frontend
    volumes:
      - production_caddy:/root/.caddy
    env_file:
       - PATH to .env
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"
    deploy:
      placement:    
        constraints:
          - node.role == manager
    networks:
      -  webnet

  redis:
    image: redis:3.2
    deploy:
      mode: replicated
      replicas: 2
    networks:
      -  webnet

我不确定在使用docker stack deploy --with-registry-auth -c production.yml my_swarm将任务部署到节点时,为什么没有设置从入口点脚本导出环境变量的问题。

使用此方法或设置环境变量的替代解决方案将为您提供任何帮助。我找不到将dockerfile入口点脚本链接到docker swarm任务/服务的文档。

编辑:

我假设我必须以某种方式利用https://docs.docker.com/engine/swarm/secrets/,但希望能够保留入口点脚本。

编辑2: 找到资源,需要适应我的过程。 https://docs.docker.com/engine/swarm/secrets/#build-support-for-docker-secrets-into-your-images

编辑3: 经过更多检查后,除入口点脚本中的那些环境变量外,所有其他环境变量都将转移到每个任务中。我可以使用docker exec进入django容器,并运行相同的命令来创建DATABASE_URLCELERY_BROKER_URL,如脚本所示。但是,仍然不知道为什么不能使用入口点脚本来创建环境变量。

1 个答案:

答案 0 :(得分:0)

由于bmitch,此问题已解决,请参阅注释。对于遇到此问题的任何人。当任务创建角色/子进程时,入口点脚本可以正常工作。因此,其中设置的任何变量将可用于容器/子进程。

没有问题的是,当我使用docker exec在特定容器/子进程内执行一次性命令时,它创建了一个不调用入口点脚本的新外壳,因此无法访问在入口点设置的变量。但是,您可以在外壳中再次设置它们,子进程将可以访问它们。例如数据库迁移等。