0

when I build and then run docker the code works fine and prints whatever I want to the command line. However, within my code, I have places where I write to a text file. My local machine doesn't reflect the text file changes.

EXAMPLE

print("Hello World)

this prints to my command line when I build and run Docker.

f = open("/feed.txt", "w")
f.write(str(time))

the feed.txt file is still blank even with this code.

What is happening here?

6
  • Is /feed.txt really the correct path?
    – akop
    Apr 20, 2019 at 15:56
  • /feed.txt is absolute path. Will write to a feed.txt file in your root directory (if the script has the permission to do it). Remove the / and should work.
    – Valentino
    Apr 20, 2019 at 16:00
  • The file will be written when you close the file handle (or use a with statement which handles this for you).
    – tripleee
    Apr 20, 2019 at 16:01
  • How are you building and running the container? If writing to the host's filesystem is your primary task, you'll probably find it easier to not use Docker, which generally prevents this as a design goal.
    – David Maze
    Apr 20, 2019 at 17:14
  • If you expect Docker to write to root filesystem, then you should mount root filesystem inside Docker. Apr 20, 2019 at 18:34

1 Answer 1

0

You should share a volume folder between your local machine and container. You script is running fine but the file feed.txt is into containter.

Take a look at: https://docs.docker.com/storage/volumes/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.