问题描述
我正在创建一个项目,该项目需要建立从在 docker 容器中运行的 Python 到在另一个容器中运行的 MysqL 数据库的连接。目前,我的 docker-compose
文件如下所示:
version: "3"
services:
login:
build:
context: ./services/login
dockerfile: docker/Dockerfile
ports:
- "80:80"
# Need to remove this volume - this is only for dev work
volumes:
- ./services/login/app:/app
# Need to remove this command - this is only for dev work
command: /start-reload.sh
db_users:
image: MysqL
volumes:
- ./data/MysqL/users_data:/var/lib/MysqL
- ./databases/users:/docker-entrypoint-initdb.d/:ro
restart: always
ports:
- 3306:3306
# Remove 'expose' below for prod
expose:
- 3306
environment:
MysqL_ROOT_PASSWORD: password
MysqL_DATABASE: users
MysqL_USER: user
MysqL_PASSWORD: password
我的 Dockerfile
服务的 login
如下所示:
# Note: this needs to be run from parent service directory
FROM tianGolo/uvicorn-gunicorn-fastapi:python3.8
# Install Poetry
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=/opt/poetry python && \
cd /usr/local/bin && \
ln -s /opt/poetry/bin/poetry && \
poetry config virtualenvs.create false
# copy using poetry.lock* in case it doesn't exist yet
copY ./app/pyproject.toml ./app/poetry.lock* /app/
RUN poetry install --no-root --no-dev
copY ./app /app
我正在尝试将我的 login
服务连接到 db_users
,并且想要使用 mysqlclient,但是当我运行 poetry add MysqLclient
时,我收到一个错误,其中包括以下几行:
/bin/sh: MysqL_config: command not found
/bin/sh: mariadb_config: command not found
/bin/sh: MysqL_config: command not found
Traceback (most recent call last):
File "<string>",line 1,in <module>
File "/private/var/folders/33/5yy7bny964bb0f3zggd1b4440000gn/T/pip-req-build-lak6lqu7/setup.py",line 15,in <module>
Metadata,options = get_config()
File "/private/var/folders/33/5yy7bny964bb0f3zggd1b4440000gn/T/pip-req-build-lak6lqu7/setup_posix.py",line 70,in get_config
libs = MysqL_config("libs")
File "/private/var/folders/33/5yy7bny964bb0f3zggd1b4440000gn/T/pip-req-build-lak6lqu7/setup_posix.py",line 31,in MysqL_config
raise OSError("{} not found".format(_MysqL_config_path))
OSError: MysqL_config not found
MysqL_config --version
mariadb_config --version
MysqL_config --libs
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
我假设这与我需要 mysql-connector-c 库才能工作这一事实有关,但我不确定如何在诗歌中实现这一点。
我正在查看以下 this 教程,但由于我不是在本地运行 MysqL,而是在 docker 中,我不确定如何将这些步骤转换为在 docker 中工作。
所以基本上,我的问题有两个:
解决方法
我忘记了我的开发环境也在 Docker 中,所以我真的不需要关心诗歌环境。
话虽如此,我将 Dockerfile 编辑为如下所示:
# Note: this needs to be run from parent service directory
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
RUN apt-get install default-libmysqlclient-dev
# Install Poetry
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=/opt/poetry python && \
cd /usr/local/bin && \
ln -s /opt/poetry/bin/poetry && \
poetry config virtualenvs.create false
# Copy using poetry.lock* in case it doesn't exist yet
COPY ./app/pyproject.toml ./app/poetry.lock* /app/
RUN poetry install --no-root --no-dev
COPY ./app /app
现在一切都按预期工作了。