Python Flask Docker migration

Python Flask Docker migration … here is a solution to the problem.

Python Flask Docker migration

I’m trying to run a database migration for my Flask application. The manual steps do work:

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

… But not automation of Docker and Docker-Compose files. How to solve the problem of the cloud?

Thanks

Docker file

# 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-Entrypoint (currently not used).

#!/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” error

    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 doesn’t seem to know the name “db”, probably because it’s only defined in Docker-Compose files, but how should “db” be changed?

Solution

I’ve solved the problem 😉

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

Related Problems and Solutions