'Unable to write file to external volume using Docker

I have a simple Python program as an example for a Docker image which writes to a file externally. It doesn't throw any error but it also doesn't write the file on the mounted volume.

Dockerfile:

FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

ENTRYPOINT [ "python3", "/app/test.py"]

Here is the Python code:

print("test")
f = open("./demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()

Command I used to build the image:

sudo docker build --tag=test .

Command used for running the image:

sudo docker run -ti test -v /home/user:/app

Running this setup on Ubuntu 20.04 with the latest Docker version.

The output has no errors. The file seems to be created but I don't know where because the external path doesn't show it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source