简体   繁体   English

Locust base Docker 镜像如何添加自定义诗歌环境?

[英]How to add custom poetry environment to Locust base Docker image?

How do you run Locust (load testing tool) in a stable Docker container with extra poetry dependencies installed?如何在安装了额外诗歌依赖项的稳定 Docker 容器中运行 Locust(负载测试工具)? From the docs it's known that running Locust in Docker is easily possible through their base image.从文档中得知,可以通过其基本映像轻松地在 Docker 中运行 Locust

docker run -p 8089:8089 -v $PWD:/mnt/locust locustio/locust -f /mnt/locust/locustfile.py

But if a load testing Python project requires extra libraries which are managed through poetry, the locust command must be run through poetry run locust .但是,如果负载测试 Python 项目需要通过诗歌管理的额外库,则必须通过poetry run locust运行 locust 命令。 The locust docs only give the following example, but with pip :蝗虫文档仅给出以下示例,但带有pip

FROM locustio/locust
RUN pip3 install some-python-package

It get's more tricky if you want to bind mount a directory to the container, as Poetry environments are linked to the working directory they're created in.如果你想将一个目录绑定到容器,它会变得更加棘手,因为 Poetry 环境链接到它们创建的工作目录。

Assuming a python project like this that uses poetry:假设一个 python 这样的项目使用了诗歌:

.
├── Dockerfile
├── README.md
├── data
├── poetry.lock
├── pyproject.toml
├── reports (mounting this directory to Docker to save reports)
└── src (contains the locust source code)
    ├── auth.py
    ├── config.py
    ├── locustfile.py
    ├── shapes
    ├── tasks
    └── users.py

SETUP设置

The contents of my Dockerfile are the following:我的Dockerfile的内容如下:

FROM locustio/locust

USER $USERNAME

# Use non-root user as a security measure
RUN groupadd -g 61000 docker
RUN useradd -g 61000 -l -m -s /bin/false -u 61000 docker

# Setup and install poetry
RUN apt update && \
    apt install -y git curl && \
    pip install poetry

# cacheing project requirements
WORKDIR /code
COPY poetry.lock pyproject.toml /code/

# installing project requirements
RUN poetry install --no-interaction --no-ansi --no-root

COPY . /code

ENTRYPOINT [ "poetry", "run", "locust" ]

Notes:笔记:

  • USER $USERNAME is important to get the correct permissions USER $USERNAME对于获得正确的权限很重要
  • The poetry environment is linked to the code/ directory and it's also where the entire project's code is moved to poetry 环境链接到code/目录,它也是整个项目代码移动到的地方
  • The entry point ENTRYPOINT [ "poetry", "run", "locust" ] is a good pattern as extra arguments can be passed in through the docker run command.入口点ENTRYPOINT [ "poetry", "run", "locust" ]是一个很好的模式,因为额外的 arguments 可以通过docker run命令传入。

Running the Container运行容器

Build the image with:构建图像:

docker build -t locust .

Example command to run the load test:运行负载测试的示例命令:

docker run -p 8089:8089 --mount type=bind,src=$PWD/reports,dst=/code/reports locust -f src/locustfile.py --headless --host <HOST> --csv=reports/<PREFIX>

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM