0

I have a simple Docker container from python:3.8.3.

FROM python:3.8.3-alpine

RUN pip install --no-cache-dir fastapi python-multipart httpx pytest requests pytest-cov

COPY app ./app
COPY tests ./tests
COPY pytest.ini ./pytest.ini

WORKDIR /

I then run the container to execute pytest and generate a coverage file.

docker run -it testimage python -m pytest tests -v --cov=app --cov-report xml:/coverage.xml

Doing this I get the tests to run fine and that the xml file has been generated.

platform linux -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python
cachedir: .pytest_cache
rootdir: /, inifile: pytest.ini
plugins: cov-2.10.0
collected 11 items
 ...
tests/test_main.py::test_invalid_missing_params PASSED                                                                                                                                                                           [100%]
----------- coverage: platform linux, python 3.8.3-final-0 -----------
Coverage XML written to file /coverage.xml

So all appears fine. I can run this command inside the container and see that the coverage.xml is generated too.

But when I try to get that file copied out of the container to artifact the coverage.xml, or even just view the file to make sure it's there by appending && cat coverage.xml to my run statement I always get: cat: coverage.xml: No such file or directory.

So I'm not sure what I've done wrong here

I did also try using docker cp and appending that to my run command.

&& docker cp testimage:/coverage.xml ~/Development/coverage.xml

but that too results in Error: No such container:path: testimage:/coverage.xml

Jonnny
  • 4,939
  • 11
  • 63
  • 93
  • have you tried the `docker cp` command? https://stackoverflow.com/questions/22049212/copying-files-from-docker-container-to-host – Tin Nguyen Jul 07 '20 at 12:51
  • @TinNguyen Good question, yes I also tried this. I should update my Q. But the same problem, because the file doesn't seem to exist, even though the output says it's written – Jonnny Jul 07 '20 at 12:57

1 Answers1

2

When you are doing the && cat coverage.xml, I think that is getting run after the docker command is complete, not in the container. So it's like (docker run ...) && cat

To run it in the container, you could do something like docker run -it testimage sh -c 'python ... && cat /coverage.xml'

But really what you want to do is copy the xml file out of the container. Your docker cp command is using the image name (testimage), rather than the container name. When you run the container, you can supply something like --name mycontainer and then use && docker cp mycontainer:/coverage.xml ~/Development/coverage.xml

Keep in mind you will need to remove the container after each run if the name is the same.

Another option is to use a volume, so something like docker run -it -v ~/Development:/output testimage python -m pytest tests -v --cov=app --cov-report xml:/output/coverage.xml

Andrew Schlei
  • 377
  • 4
  • 5
  • Yeah, that started to dawn on me after I posted that the docker container process had exited by the time I was trying to copy the file to the host machine. - Thanks – Jonnny Jul 07 '20 at 16:02