10

I have a simple Python script, inside of a Docker container, that:

-Makes a call to an external API

-A license file is returned from the API

I want to be able to save the returned license file, to my directory inside of the docker container.

This is what I have done:

r = requests.post("http://api/endpoint", headers=headers, data=data, auth=("", "")
f = open('test.lic', 'w+')
f.write(r.content)
f.close()

My understanding is that this would create a test.lic file if one did not already exist, or open the existing test.lic file, and write the content of the request object to the test.lic. However this is not working. No file is being saved to my directory inside of the Docker container. If I run these lines from a python shell it works so I'm guessing it has something to do with being inside of a Docker container.

2
  • 1
    How are you testing whether the file is being saved? Commented Jul 17, 2015 at 21:54
  • How to invoke docker run command ? Which user the python script run with ? This information can be in your Dockerfile. Commented Jul 17, 2015 at 22:03

2 Answers 2

6

It could be that the file is getting saved, but it is in the working directory in the container.

The docker docs say:

The default working directory for running binaries within a container is the root directory (/), but the developer can set a different default with the Dockerfile WORKDIR command.

So the file may be ending up in the root directory / of your container.

You could add a print of os.getcwd() to your script (if you are able to see the output... which you might need to use the docker logs command) to verify this. Either way, the file will likely be in the location returned by os.getcwd().

Sign up to request clarification or add additional context in comments.

3 Comments

Or just run docker diff to see if anything has been written to the filesystem (won't work on a volume though).
@samstav your answer pointed me in the right direction. So the file was getting saved but as you pointed out it was being saved in the root directory of my container. I added WORKDIR and Volume to my Dockerfile and now it is working as I had intended.
How did you add volume to your Dockerfile? I'm also trying this, and I made a volume (docker volume create checkpoint_file), and started my container with a folder bind to this volume. My python code is writing to a checkpoint file in this location, but this is not persisting once the container is stopped.
0

In my case the file was saved to a different container. Check it if you have multiple containers in the docker-compose.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.