0

I want to capture the content of file which is located in the host os (linus) into a variable from the Docker container using a python script

Can I do this

FILE="/home/test/file.txt"
#open the file for read-only
fd = os.open(FILE,os.O_RDONLY)
content = os.read(fd,12)
print content

ended up with this error

OSError: [Errno 2] No such file or directory: '/home/test/file.txt'

Please suggest a way if I am doing it wrong

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
CuriousTechie
  • 413
  • 2
  • 8
  • 21
  • I do not want to map the volume but just want to access the file content @Anthony Kong – CuriousTechie Aug 09 '17 at 23:29
  • 1
    Then you can use `ADD` or `COPY` command https://stackoverflow.com/questions/24958140/what-is-the-difference-between-the-copy-and-add-commands-in-a-dockerfile – Anthony Kong Aug 09 '17 at 23:31
  • Thanks for the idea but as per my requirement, Is there a possible to use python script to do this after the container is launched – CuriousTechie Aug 09 '17 at 23:45
  • So what you are asking is that you want to access the file but you don't want to use any file access functionality offered by Docker? – Anthony Kong Aug 09 '17 at 23:51
  • 3
    You cannot access files on the host from inside a Docker container unless you explicitly expose them using a Docker volume mount. The whole idea behind Docker containers is to isolate the host as much as possible. – larsks Aug 09 '17 at 23:56

1 Answers1

4

mount the volume where the file is located into a directory in your docker container

you do this by using the -v flag in docker

for example:

docker run -v <HOST_FOLDER>:/data <IMAGE>

that will mount to the folder data on the root of your container (/data)

MrE
  • 19,584
  • 12
  • 87
  • 105