运行 依赖于 mysql 容器的 python 容器中的 alembic
Running alembic in a python container that depends upon a mysql container
我有一个项目,我在其中使用以下 docker-compose.yml
文件:
version: '3'
services:
python:
restart: always
build: ./budget/dockerfiles/python/
ports:
- "5000:5000"
links:
- db
depends_on:
- db
volumes:
- ./budget/:/app:z
entrypoint:
- python
- -u
- /app/run.py
db:
build: ./budget/dockerfiles/mysql/
environment:
MYSQL_ROOT_PASSWORD: rainmakers
MYSQL_DATABASE: forecast
MYSQL_USER: rain
MYSQL_PASSWORD: rainmakers
volumes:
- ./Dump.sql:/db/Dump.sql:z
- ./Dump_Test.sql:/db/Dump_Test.sql:z
- ./big_fc.sql:/db/big_fc.sql:z
ports:
- "3306:3306"
我还有以下 alembi.ini
文件:
# A generic, single database configuration.
[alembic]
sqlalchemy.url = mysql://username:password@localhost/database_name
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
我需要 运行 我的 Python 容器中的命令 alembic upgrade head
,但是当我 运行 它时,我收到以下错误:
Traceback (most recent call last):
File "/usr/local/bin/alembic", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 479, in main
CommandLine(prog=prog).main(argv=argv)
File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 473, in main
self.run_cmd(cfg, options)
File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 456, in run_cmd
**dict((k, getattr(options, k, None)) for k in kwarg)
File "/usr/local/lib/python2.7/dist-packages/alembic/command.py", line 254, in upgrade
script.run_env()
File "/usr/local/lib/python2.7/dist-packages/alembic/script/base.py", line 421, in run_env
util.load_python_file(self.dir, 'env.py')
File "/usr/local/lib/python2.7/dist-packages/alembic/util/pyfiles.py", line 93, in load_python_file
module = load_module_py(module_id, path)
File "/usr/local/lib/python2.7/dist-packages/alembic/util/compat.py", line 75, in load_module_py
mod = imp.load_source(module_id, path, fp)
File "alembic/env.py", line 70, in <module>
run_migrations_online()
File "alembic/env.py", line 58, in run_migrations_online
with connectable.connect() as connection:
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2085, in connect
return self._connection_cls(self, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 90, in __init__
if connection is not None else engine.raw_connection()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2171, in raw_connection
self.pool.unique_connection, _connection)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2145, in _wrap_pool_connect
e, dialect, self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1456, in _handle_dbapi_exception_noconnection
exc_info
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2141, in _wrap_pool_connect
return fn()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 516, in checkout
rec = pool._do_get()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 1229, in _do_get
return self._create_connection()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 385, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
我猜错误是这个命令试图在我的 python 容器中找到 mysql,但这不在这里,所以我想知道我能做些什么来使它 运行 正确。
从 python
容器的角度来看,mysql IP 被解析为 mysql 服务的名称:db
。解决问题如下:
[alembic]
sqlalchemy.url = mysql://username:password@db/database_name
我有一个项目,我在其中使用以下 docker-compose.yml
文件:
version: '3'
services:
python:
restart: always
build: ./budget/dockerfiles/python/
ports:
- "5000:5000"
links:
- db
depends_on:
- db
volumes:
- ./budget/:/app:z
entrypoint:
- python
- -u
- /app/run.py
db:
build: ./budget/dockerfiles/mysql/
environment:
MYSQL_ROOT_PASSWORD: rainmakers
MYSQL_DATABASE: forecast
MYSQL_USER: rain
MYSQL_PASSWORD: rainmakers
volumes:
- ./Dump.sql:/db/Dump.sql:z
- ./Dump_Test.sql:/db/Dump_Test.sql:z
- ./big_fc.sql:/db/big_fc.sql:z
ports:
- "3306:3306"
我还有以下 alembi.ini
文件:
# A generic, single database configuration.
[alembic]
sqlalchemy.url = mysql://username:password@localhost/database_name
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
我需要 运行 我的 Python 容器中的命令 alembic upgrade head
,但是当我 运行 它时,我收到以下错误:
Traceback (most recent call last):
File "/usr/local/bin/alembic", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 479, in main
CommandLine(prog=prog).main(argv=argv)
File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 473, in main
self.run_cmd(cfg, options)
File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 456, in run_cmd
**dict((k, getattr(options, k, None)) for k in kwarg)
File "/usr/local/lib/python2.7/dist-packages/alembic/command.py", line 254, in upgrade
script.run_env()
File "/usr/local/lib/python2.7/dist-packages/alembic/script/base.py", line 421, in run_env
util.load_python_file(self.dir, 'env.py')
File "/usr/local/lib/python2.7/dist-packages/alembic/util/pyfiles.py", line 93, in load_python_file
module = load_module_py(module_id, path)
File "/usr/local/lib/python2.7/dist-packages/alembic/util/compat.py", line 75, in load_module_py
mod = imp.load_source(module_id, path, fp)
File "alembic/env.py", line 70, in <module>
run_migrations_online()
File "alembic/env.py", line 58, in run_migrations_online
with connectable.connect() as connection:
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2085, in connect
return self._connection_cls(self, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 90, in __init__
if connection is not None else engine.raw_connection()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2171, in raw_connection
self.pool.unique_connection, _connection)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2145, in _wrap_pool_connect
e, dialect, self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1456, in _handle_dbapi_exception_noconnection
exc_info
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2141, in _wrap_pool_connect
return fn()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 516, in checkout
rec = pool._do_get()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 1229, in _do_get
return self._create_connection()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 385, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
我猜错误是这个命令试图在我的 python 容器中找到 mysql,但这不在这里,所以我想知道我能做些什么来使它 运行 正确。
从 python
容器的角度来看,mysql IP 被解析为 mysql 服务的名称:db
。解决问题如下:
[alembic]
sqlalchemy.url = mysql://username:password@db/database_name