Dockerfile:添加的文件不会复制到卷 [英] Dockerfile: added files won't be copied to volume

查看:55
本文介绍了Dockerfile:添加的文件不会复制到卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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文件中以进行故障排除:

so I included this in the python file for troubleshooting:

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

docker-compose up给了我这个

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

码头工人检查:

"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.

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

The image was built on a Win10 maschine, and I try to run it on a ubuntu server-vm.

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

also I replaced the ADD commands with COPY, tried to copy the whole directory et cetera.

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

Since the os.path.isdir is true, I'm suspecting the mounting of the directory works. But somehow the expected files are not there...

谢谢您的建议

推荐答案

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

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.

这篇关于Dockerfile:添加的文件不会复制到卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆