docker 如何在运行MLflow的服务器上存储工件

q5iwbnjs  于 2023-05-28  发布在  Docker
关注(0)|答案(3)|浏览(237)

我定义了以下docker镜像:

FROM python:3.6

RUN pip install --upgrade pip
RUN pip install --upgrade mlflow

ENTRYPOINT mlflow server --host 0.0.0.0 --file-store /mnt/mlruns/

并创建一个名为mlflow-server的映像。接下来,我从本地机器启动这个服务器:

docker run --rm -it -p 5000:5000 -v ${PWD}/mlruns/:/mnt/mlruns mlflow-server

接下来,我定义以下函数:

def foo(x, with_af=False):
    mlflow.start_run()
    mlflow.log_param("x", x)
    print(x)
    if with_af:
        with open(str(x), 'wb') as fout:
            fout.write(os.urandom(1024))
        mlflow.log_artifact(str(x))
        mlflow.log_artifact('./foo.data')
    mlflow.end_run()

在同一目录中,我运行foo(10),参数被正确记录。但是,foo(10, True)会产生以下错误:PermissionError: [Errno 13] Permission denied: '/mnt'。似乎log_artifact试图直接将文件保存在本地文件系统上。
知道我哪里做错了吗

q3qa4bjr

q3qa4bjr1#

问得好只是为了确保,听起来你已经配置MLflow在运行脚本时与跟踪服务器对话,例如。通过MLFLOW_TRACKING_URI=http://localhost:5000 python my-script.py

MLflow中的Artifact存储

工件与其他运行数据(指标、参数、标记)的细微区别在于,客户端而不是服务器负责持久化它们。当前流量(从MLflow 0.6.0开始)为:

  • 用户代码调用mlflow.start_run
  • MLflow客户端向跟踪服务器发出API请求以创建运行
  • 跟踪服务器为运行确定适当的根项目URI(当前为:运行的工件根是其父实验的工件根目录的子目录)
  • 跟踪服务器保留运行元数据(包括其工件根)并向客户端返回Run对象
  • 用户代码调用log_artifact
  • 客户端将工件记录在活动运行的工件根目录下

问题

当您通过mlflow server --host 0.0.0.0 --file-store /mnt/mlruns/启动MLflow服务器时,服务器会在docker容器中记录/mnt/mlruns下的指标和参数,并将/mnt/mlruns下的工件路径返回给客户端。然后,客户端尝试在 * 本地文件系统 * 上的/mnt/mlruns下记录工件,但遇到的PermissionError失败了。

修复

使用远程跟踪服务器存储工件的最佳实践是将服务器配置为使用客户端和服务器都可以访问的工件根目录(例如S3存储桶或Azure Blob存储URI)。你可以通过mlflow server --default-artifact-root [artifact-root]来实现。
请注意,服务器仅在将工件根分配给新创建的实验时使用此工件根-在现有实验下创建的运行将使用现有实验的工件根下的工件根目录。有关配置跟踪服务器的详细信息,请参阅MLflow跟踪指南。

uidvcgyl

uidvcgyl2#

我遇到了同样的问题,试试看:

sudo chmod 755 -R /mnt/mlruns
docker run --rm -it -p 5000:5000 -v /mnt/mlruns:/mnt/mlruns mlflow-server

我不得不创建一个文件夹与docker的确切路径,并更改权限。
我在Docker内部也做了同样的事情。

FROM python:3.6

RUN pip install --upgrade pip
RUN pip install --upgrade mlflow
RUN mkdir /mnt/mlruns/
RUN chmod 777 -R /mnt/mlruns/

ENTRYPOINT mlflow server --host 0.0.0.0 --file-store /mnt/mlruns/
xam8gpfp

xam8gpfp3#

当我们将记录MLFlow实体的REST API请求发送到跟踪服务器时,服务器将根据容器中设置的内容使用存储位置进行响应。如果值不同,则客户端将最终假设容器相对路径在主机上可用,这将导致权限错误。
下面是一个docker-compose文件,它将默认存储位置设置为${HOME}/mnt/mlruns

services:
  web:
    restart: always
    build:
      context: ./mlflow
      args:
        - "MLFLOW_TRACKING_DIRECTORY=${HOME}/mnt/mlruns"
    image: mlflow_server
    container_name: mlflow_server
    ports:
      - "${MLFLOW_PORT:-5000}:5000"
    volumes:
      - "${HOME}/mnt/mlruns:${HOME}/mnt/mlruns"

./mlflow的内容:
Dockerfile

FROM python:3.10-slim-buster

ARG MLFLOW_TRACKING_DIRECTORY
ENV MLFLOW_TRACKING_DIRECTORY=${MLFLOW_TRACKING_DIRECTORY}

# Install python packages
COPY requirements.txt /tmp
RUN pip install -r /tmp/requirements.txt
RUN echo ${MLFLOW_TRACKING_DIRECTORY} > test.txt

CMD mlflow server \
    --backend-store-uri ${MLFLOW_TRACKING_DIRECTORY}/tracking \
    --default-artifact-root ${MLFLOW_TRACKING_DIRECTORY}/artifacts \
    --host 0.0.0.0

requirements.txt

mlflow==2.3.1

请确保正确设置${HOME}/mnt/mlruns$的权限,因为客户端将直接访问本地存储。

相关问题