Reputation: 514
I run Django in docker and I want to read a file from host. I mount a path in host to container using following command in my docker-compose file:
volumes:
- /path/on/host/sharedV:/var/www/project/src/shV
Then I execute mongodump and export a collection into sharedV on host. After that, I inspect web container and go to the shV directory in container and I can see the backup file. However, when I run os.listdir(path)
in django, the result is empty list. In the other word, I can access to sharedV directory in Django but I can not see its contents!
Here is the Mount part of container inspect:
"Mounts": [
{
"Type": "bind",
"Source": "/path/on/host/sharedV",
"Destination": "/var/www/project/src/shV",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
]
Is there any idea that how can I access to host from a running container?
thanks
Upvotes: 1
Views: 1109
Reputation: 46
This works for me, Tying to give you a perception Project Tree
.
├── app
│ ├── dir
│ ├── file.txt
│ └── main.py
├── dir
│ └── demo.txt
├── docker-compose.yml
└── Dockerfile
Dockerfile
# Dockerfile
FROM python:3.7-buster
RUN mkdir -p /app
WORKDIR /app
RUN useradd appuser && chown -R appuser /app
USER appuser
CMD [ "python", "./main.py" ]
docker-compose
version: '2'
services:
api:
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- ./app:/app/
- ./dir:/app/dir
main.py
if __name__ == '__main__':
path = './dir'
dir = os.listdir(path)
print(f'Hello', dir)
Prints
Hello ['demo.txt']
Upvotes: 1