问题
I made a Dockerfile for a Python Script, which needs some persistent data in a volume. Problem is the files for the volume won't be in place in the container.
Dockerfile:
FROM python:3
COPY grab_immo /grab_immo
RUN mkdir /data
ADD ./data/contacts /data
ADD ./data/run.log /data
ADD ./data/template /data
VOLUME /data
WORKDIR /grab_immo
RUN pip install -r requirements.txt
ENV MY_ADDRESS=value \
PASSWORD=value \
SMTP_HOST=value \
SMTP_PORT=value
CMD [ "python", "./main.py" ]
docker-compose.yml
---
version: "2"
services:
grab_immo:
build: .
image: yyy/grab_immo
container_name: grab_immo
environment:
- MY_ADDRESS = ''
- PASSWORD =
- SMTP_HOST =
- SMTP_PORT = 587
volumes:
- /home/yyy/grab_immo_config:/data
restart: unless-stopped
so I included this in the python file for troubleshooting:
print(os.path.isfile('/data/contacts') )
print(os.path.isdir('/data'))
docker-compose up gives me this:
grab_immo | False //print(os.path.isfile('/data/contacts') )
grab_immo | True //print(os.path.isdir('/data'))
grab_immo | /grab_immo/main.py
grab_immo | Traceback (most recent call last):
grab_immo | File "./main.py", line 248, in <module>
grab_immo | main()
grab_immo | File "./main.py", line 206, in main
grab_immo | sendmail('asdf', 'asdf', 'asdfk', 'deskldf')
grab_immo | File "./main.py", line 84, in sendmail
grab_immo | names, emails = get_contacts(newPath + 'contacts') # read contacts
grab_immo | File "./main.py", line 122, in get_contacts
grab_immo | with open(filename, mode='r', encoding='utf-8') as contacts_file:
grab_immo | FileNotFoundError: [Errno 2] No such file or directory: '/data/contacts'
grab_immo exited with code 1
docker inspect:
"Mounts": [
{
"Type": "bind",
"Source": "/home/yyy/grab_immo_config",
"Destination": "/data",
"Mode": "rw",
"RW": true,
"Propagation": ""
},
I created the directory on the hostmachine, but there was never a file in it.
The image was built on a Win10 maschine, and I try to run it on a ubuntu server-vm.
also I replaced the ADD commands with COPY, tried to copy the whole directory et cetera.
Since the os.path.isdir is true, I'm suspecting the mounting of the directory works. But somehow the expected files are not there...
Thanks for any suggestions
回答1:
Volumes are mounted after the container startup. You need to add the data inside the volume after it was created or add in an existent folder and link it to the container.
来源:https://stackoverflow.com/questions/58367480/dockerfile-added-files-wont-be-copied-to-volume