Python Flask Docker 迁移

Python Flask Docker Migrations

我正在尝试为我的 Flask 应用程序进行数据库迁移 运行。手动步骤确实有效:

docker-compose exec python /usr/local/bin/python manage.py db init
docker-compose exec python /usr/local/bin/python manage.py db migrate
docker-compose exec python /usr/local/bin/python manage.py db upgrade

... 但不是 Docker 和 Docker-Compose 文件的自动化。云的问题如何解决?

谢谢

Docker-文件

# Use an official Python runtime as a parent image
FROM python:2.7

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Environment
RUN apt-get update
RUN apt-get install -y ruby

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME APP
ENV TERM xterm

# Run db migrations
#COPY docker-entrypoint.sh /docker-entrypoint.sh
#RUN chmod a+rx /docker-entrypoint.sh

RUN chmod +x manage.py
RUN python manage.py db init
RUN python manage.py db migrate
RUN python manage.py db upgrade

COPY init.sql /docker-entrypoint-initdb.d/10-init.sql

# Run the app when the container launches
CMD ["python", "app.py"]

Docker-Compose.yml

version: '2'
services:
  db:
    image: postgres
    environment:
      - POSTGRES_USER=abc
      - POSTGRES_PASSWORD=abc
    ports:
      - "55432:5432"
  python:
    build: ./app
    ports:
      - "80:5000"
    depends_on:
      - db
    links:
      - db
    tty: true

manage.py

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from sdst import app, db

manager = Manager(app)
migrate = Migrate(app, db)

manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()

Docker-入口点 (目前未使用)

#!/bin/sh

python manage.py db init
python manage.py db migrate
python manage.py db upgrade

exec "$@"

app.py

[...]
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://%(user)s:\
    %(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES
[...]

'docker-compose build && docker-compose up'

出错
    self.pool.unique_connection, _connection)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2162, in _wrap_pool_connect
    e, dialect, self)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception_noconnection
    exc_info
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2158, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 345, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 791, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 532, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 1287, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 350, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 477, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 674, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 106, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 411, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "db" to address: Name or service not known
 (Background on this error at: http://sqlalche.me/e/e3q8)
ERROR: Service 'python' failed to build: The command '/bin/sh -c python manage.py db migrate' returned a non-zero code: 1

好像Docker不知道"db"这个名字,可能是因为它只定义在Docker-Compose文件中,但是"db"应该怎么写改变了吗?

我已经解决了问题 ;)

Dockerfile

# Use an official Python runtime as a parent image
FROM python:2.7

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Environment
RUN apt-get update
RUN apt-get install -y ruby

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME APP
ENV TERM xterm

# DB migrations
RUN chmod +x manage.py
COPY init.sql /docker-entrypoint-initdb.d/10-init.sql

# Make Entrypoint executable
RUN chmod +x /app/docker-entrypoint.sh

# Tools
RUN chmod +x ./src/urlcrazy/urlcrazy
RUN chmod +x ./src/dnstwist/dnstwist.py

# Run the app when the container launches
CMD ["/bin/bash", "/app/docker-entrypoint.sh"]

docker-entrypoint.sh

#!/bin/sh

python manage.py db init
python manage.py db migrate
python manage.py db upgrade

psql -U XXX -d XXX -f /docker-entrypoint-initdb.d/10-init.sql -h db

cd /app
python app.py