1

I'm trying to read a file from the host to the container which is python script. That script reads a file from the root folder.

For eg /a/b/c/log.txt(This is a dynamic file so I cant add this into Dockerfile)

I need to access this in the docker container. However, from this platform I got a hint that we need to volume.
docker run -v /path/from/host:/path/to/container sntalarmdocker_snt *sntalarmdocker_snt is the name of image

The main thing is being confused about this path/to/container

  1. Is it path were DockerFile exist
  2. Is it /var/snap/docker/common/var-lib-docker/containers/f901e49b67375d4b1105309569c92afae415309ac1787afa2a565a9c08708b18#

This question is related to Python file writing is not working inside docker

May I know how can I resolve this issue. In short I need to read a file from host and write a file into host in which I cant add that in the docker file. Thanks in Advance for time & help

Aaditya R Krishnan
  • 495
  • 1
  • 10
  • 31

2 Answers2

6

/path/to/container is a poor choice of words. It is actually the path within the container where you would like to mount /path/from/host.

For example, if you had a directory on the host, /home/edward/data, and you wanted the contents of that directory to be available in the container at /data, you would use -v /home/edward/data:/data.

In the container's process, you can then read and/or write files in the /data directory and they will be read from/written to /home/edward/data on the host.

Bind mounts are explained in detail in the documentation.

chash
  • 3,975
  • 13
  • 29
  • What should be the probable reason for not generating the files in docker when I run the image. But the same thing is generating when I run a python script – Aaditya R Krishnan Jun 16 '20 at 23:54
  • Who knows? There are way too many possibilities. If you want help, you'll need to create a minimal, reproducible example so that your problem is easy to spot. Create a python script that just writes a file, put it in a similar Dockerfile and run it. If you still have a problem, add all of that to your question. – chash Jun 16 '20 at 23:58
  • Which propagation type(shared,slave,private,rshared,rslave,rprivate) should I use if I need to write a file into the host directory – Aaditya R Krishnan Jun 17 '20 at 00:48
  • $ docker run -d \ -it \ --name devtest \ -v "$(pwd)"/target:/read_app \ -v "$(pwd)"/target:/write_app \ nginx:latest – Aaditya R Krishnan Jun 17 '20 at 00:49
  • The default bind propagation works for most use cases. My _guess_ is that your program is a) not writing files for some reason or b) not writing them in the correct location. – chash Jun 17 '20 at 14:24
1

./path/to/container is the directory path inside the container where you want your host's data to be bind mount with container.

It can be any directory path like /data ,/root/data etc. (/data does not need to already exist in the container) Whatever updates are taking place in the container's directory by read/write operation will be updated there in your host's path as well.

The only thing need to check is your host machine path to the specified folder/file After bind mounting ,you can use exec -it command to enter into container interactive shell and then use ls to see the list of files/folders.

The path provided at time of bind mount can be seen there.

Atul kumar
  • 11
  • 2