Dockerfile:添加的文件不会被复制到卷

时间:2019-10-13 20:17:11

标签: docker docker-compose dockerfile

我为Python脚本制作了一个Dockerfile,该文件需要卷中的一些持久数据。问题是该卷的文件不在容器中。

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

因此我将其包含在python文件中以进行故障排除:

print(os.path.isfile('/data/contacts') )    
print(os.path.isdir('/data'))

docker-compose up给了我这个

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

码头工人检查:

"Mounts": [
            {
                "Type": "bind",
                "Source": "/home/yyy/grab_immo_config",
                "Destination": "/data",
                "Mode": "rw",
                "RW": true,
                "Propagation": ""
            },

我在主机上创建了目录,但是其中没有文件。

该映像基于Win10机器构建,我尝试在ubuntu服务器虚拟机上运行它。

我也用COPY替换了ADD命令,试图复制整个目录等等。

由于os.path.isdir为true,所以我怀疑目录的安装有效。但是以某种方式没有预期的文件...

感谢任何建议

1 个答案:

答案 0 :(得分:0)

在容器启动后装入卷。创建卷后,您需要在卷中添加数据,或添加到现有文件夹中并将其链接到容器。