我有一个运行以下Docker容器的VM实例:
我的项目的结构如下:
project_root
|
|--- production.yml
|--- .envs
| |
| |---.production
| |
| |---.postgres
|
|---... more Django apps
|
|--- compose
|
|---production
|
|---- django
| |---- Dockerfile
| |---- entrypoint
| |---- start
|
|---- postgres
|---- Dockerfile
/compose/production/django/Dockerfile
如下:
FROM python:3.6-alpine
ENV PYTHONUNBUFFERED 1
RUN apk update \
# psycopg2 dependencies
&& apk add --virtual build-deps gcc python3-dev 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 \
# git
&& apk add --no-cache git
RUN addgroup -S django \
&& adduser -S -G django django
# Requirements are installed here to ensure they will be cached.
COPY ./requirements /requirements
RUN pip install --no-cache-dir -r /requirements/project.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 . /app
RUN chown -R django /app
USER django
WORKDIR /app
ENTRYPOINT ["/entrypoint"]
/compose/production/django/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 "$@"
/compose/production/django/start
如下:
#!/bin/sh
set -o errexit
set -o pipefail
set -o nounset
python /app/manage.py collectstatic --noinput
/usr/local/bin/gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app
/.envs/.production/.postgres
如下:
# PostgreSQL
# ------------------------------------------------------------------------------
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=some_db
POSTGRES_USER=super_user_123
POSTGRES_PASSWORD=not_gonna_tell_you
\production.yml
version: '3'
volumes:
production_postgres_data: {}
production_postgres_data_backups: {}
production_caddy: {}
services:
django:
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
image: project_production_django
depends_on:
- postgres
- redis
env_file:
- ./.envs/.production/.django
- ./.envs/.production/.postgres
command: /start
postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
image: project_production_postgres
volumes:
- production_postgres_data:/var/lib/postgresql/data
- production_postgres_data_backups:/backups
env_file:
- ./.envs/.production/.postgres
caddy:
build:
context: .
dockerfile: ./compose/production/caddy/Dockerfile
image: project_caddy
depends_on:
- django
volumes:
- production_caddy:/root/.caddy
env_file:
- ./.envs/.production/.caddy
ports:
- "0.0.0.0:80:80"
- "0.0.0.0:443:443"
redis:
image: redis:3.2
当我在云上的VM实例中运行Docker容器时,一切正常。
发生的事情是,我需要使Django应用访问远程数据库,而不是docker网络上的postgres
容器。该远程数据库位于这些Docker容器所在的VM实例之外。
首先,我所做的是将其设置为使VM实例(即docker主机)可以通过localhost:15432
只能通过反向SSH访问此远程数据库,因此,我只需要在VM实例中仅使用localhost:15432
这是100%有效的。我可以使用该地址和端口从VM实例又称为Docker主机访问远程数据库
然后我需要让django容器访问相同的容器。这就是我遇到的问题。
我在.postgres
下的.envs
文件中尝试了以下更改。他们都失败了
.postgres
,以使我使用的是我收集的docker主机的IP,通常为172.17.0.1
。localhost
。 我有必要使django
容器能够同时访问远程数据库和postgres
容器。
但是,到目前为止,我很高兴能够直接让django
容器访问远程数据库而无需完全删除postgres容器。
我该怎么做?