Locust: Docker run Could not find any locustfile! Ensure file ends in '.py'

Created on 24 Oct 2020  路  11Comments  路  Source: locustio/locust

Describe the bug


I followed the instructions on https://docs.locust.io/en/stable/running-locust-docker.html to try to run the docker image

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

but received this error " Could not find any locustfile! Ensure file ends in '.py'".

Here is my locustfile.py in $PWD:

import time
from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(1, 2)
    @task
    def index_page(self):
        self.client.get("/")

This file works fine with locust running on my local machine (Ubuntu WSL of Windows 10), i.e. not using the container.
I confirm the environment variable $PWD is present and pointing to my present working directory where my locustfile.py resides.

Expected behavior


The Docker run above should start Locust server and make its UI accessible on http://localhost:8089.

Actual behavior


$ docker run -p 8089:8089 -v $PWD:/mnt/locust locustio/locust -f /mnt/locust/locusttest1.py Could not find any locustfile! Ensure file ends in '.py' and see --help for available options.
Surprisingly the above (default to latest tag) shows version 1.1. So I also tried v 1.3.1 but got the same error.
$ docker run -p 8089:8089 -v $PWD:/mnt/locust locustio/locust:1.3.1 -f /mnt/locust/locusttest1.py Could not find any locustfile! Ensure file ends in '.py' and see --help for available options.

Steps to reproduce

  • copy my locustfile.py contents into a new file named locustfile.py in your working directory

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

Environment

  • OS: Ubuntu 16.04 WSL of Windows 10
  • Python version: irrelevant because I am running the docker image
  • Locust version: 1.1 as shown by docker run -p 8089:8089 -v $PWD:/mnt/locust locustio/locust --version
  • Locust command line that you ran: see above docker run ...
  • Locust file contents (anonymized if necessary): see above
bug invalid

All 11 comments

I鈥檒l fix the documentation...

Hello Lars, thank you for your reply. The link you posted would use locustio/locust as a base image and build another image with a custom locustfile in it. This is a bit heavy handed for running simple tests. What I want is to run the base image with any custom locustfile as you recommended in the document.

I have done a bit more investigation. The issue seems to be docker volume mounting is not working as expected. I tried this simple example:

jhu120@DESKTOP-******:~/locust_test_tool$ docker  run  -v $PWD:/mnt/locust -w /mnt/locust -i -t  ubuntu bash
root@9325e6685be1:/mnt/locust# ls -l
total 0
root@9325e6685be1:/mnt/locust# pwd
/mnt/locust

The files from the host volume are not visible in the mounted volume inside the containers. That is the problem. That is why I see the error "Could not find any locustfile!" when running docker run -p 8089:8089 -v $PWD:/mnt/locust locustio/locust -f /mnt/locust/locustfile.py.

I have .py files in the host folder:

jhu120@DESKTOP-******:~/locust_test_tool$ echo $PWD
/home/jhu120/locust_test_tool
jhu120@DESKTOP-******:~/locust_test_tool$ ls
docker_locust  issue_1604_github  locustfile.py  locusttest1.py

I checked the official docker documentation for sharing files between the host and a container https://docs.docker.com/engine/reference/commandline/run/ but the sharing method has not worked for me.

Oh, sorry misremembered what the issue was about. That should work. Perhaps docker daemon doesnt have access to your current directory?

lafp@mac:~/git/locust docker  run  -v $PWD:/mnt/locust -w /mnt/locust -i -t  ubuntu bash
root@4cdf23cbfe93:/mnt/locust# ls
CHANGELOG.md  Dockerfile  LICENSE  MANIFEST.in  Makefile  README  README.md  Vagrantfile  codecov.yml  docs  examples  generate_changelog.py  locust  locust.egg-info  pyproject.toml  setup.cfg  setup.py  tox.ini
root@4cdf23cbfe93:/mnt/locust# pwd
/mnt/locust

This seems to be a pure docker issue, not a locust issue...

Fixed!

It is not a Locust issue, neither a Docker issue, at least not a complete Docker issue.

It is an issue rooted in WSL of Windows. The normal folders in Ubuntu WSL cannot be mounted to a Docker container. The fix is to copy the required file(s) a Windows path in the WSL and mount the volume from there to the container:

$ mkdir /c/Users/JACKHu/Documents/JHU/Locust
$ cp ~/locust_test_tool/locusttest1.py  /c/Users/JACKHu/Documents/JHU/Locust
$ docker run -p 8089:8089 -v /c/Users/JACKHu/Documents/JHU/Locust:/mnt/locust locustio/locust:1.3.1  -f /mnt/locust/locusttest1.py

Obviously, anyone want to use this fix would need to replace JACKHu by your own Windows username, and replace JHU/Locust by whatever folder path you want to create.

Lars, suggest to add this tip to your documentation for Windows users who use WSL.

Maybe you can add it to the wiki? https://github.com/locustio/locust/wiki/FAQ

will do

Done. Added the How to tip in https://github.com/locustio/locust/wiki/FAQ.

Hi Lars, I have been experimenting with creating a new Docker image using locustio/locust:1.3.1 as a base image. Docker build completed but Docker run generates an Unknown user error. I would appreciate it if you could take a look to see what I have done wrong.

Created this Dockerfile:

FROM locustio/locust:1.3.1
ADD locustfile.py /home/locust/
#RUN pip3 install ...
#USER locust
#WORKDIR /home/locust
ENTRYPOINT ["locust", "-f ./locustfile.py"] 

Built a Docker image locally:
$ docker build -t jackhu008/locust:1.3.1 .

Tried to run it:

$ docker run -p 8089:8089  jackhu008/locust:1.3.1
[2020-10-24 17:41:07,138] 55242a586daf/ERROR/locust.main: Unknown User(s):  -f ./locustfile.py

If I override the entrypoint and run the container interactive. I can start "locust -f ./locustfile.py" from inside the container successfully.

Fixed! The last line in Dockerfile is wrong. It needs to changed to
ENTRYPOINT ["locust", "-f", "./locustfile.py"]

Was this page helpful?
0 / 5 - 0 ratings