Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save file from Python script to Docker Container

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.

like image 955
Jonathan Avatar asked Jul 17 '15 21:07

Jonathan


People also ask

Can you copy a file into a docker image?

Dockerfile Dockerfiles are used to build Docker images, which are then instantiated into Docker containers. Dockerfiles can contain several different instructions, one of which is COPY. The COPY instruction lets us copy a file (or files) from the host system into the image.


1 Answers

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().

like image 106
samstav Avatar answered Sep 19 '22 15:09

samstav