docker 如何将mysqlclient添加到诗歌环境中

nnsrf1az  于 2023-04-29  发布在  Docker
关注(0)|答案(2)|浏览(123)

我正在创建一个项目,它需要从运行在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

我的login服务的Dockerfile看起来像这样:

# 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中工作。
所以,我的问题有两个方面:
1.如何将mysqlclient添加到pyproject.toml文件
1.我如何在我的docker env中使用它?

xqkwcwgp

xqkwcwgp1#

我忘了我的开发环境也在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

现在一切都按预期工作了。

z0qdvdin

z0qdvdin2#

对于所有其他因为无法在诗歌中添加mysqlclient而被google带到这里的人:
问题是mysqlclient不是你要找的python包的名字。您需要运行以下命令:

poetry add mysql-connector-python

相关问题