Save file with Python script in Docker Container

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

- makes an external API call

-A license file is returned from the API

I want to save the returned license file to my directory inside the docker container.

This is what I did:

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

I understand that this will create the test.lic file if it does not already exist, or open the existing test.lic file and write the contents of the request object in test.lic . However, this does not work. Not a single file is saved in my directory inside the Docker container. If I run these lines from the python shell, this works, so I assume this has something to do with being inside the Docker container.

+5
source share
1 answer

The file may be saved, but it is located in the working directory in the container.

docker docs say:

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

Thus, the file may end in the root directory / your container.

You can add print from os.getcwd() to your script (if you can see the result ... which you might need to use the docker logs ) to check this. In any case, the file is likely to be in the location returned by os.getcwd() .

+2
source

All Articles