简体   繁体   English

使用docker run添加数据文件

[英]add a data file with docker run

I want to add a python script to a docker container. 我想将python脚本添加到Docker容器中。 This script processes a data file that I provide when I run the container. 该脚本处理运行容器时提供的数据文件。 I could not figure out how to do that so I would appreciate some help (I do not want to add the data.txt file to the container). 我不知道该怎么做,所以我将不胜感激(我不想将data.txt文件添加到容器中)。 I understand that I could send the file to a REST interface with curl but if possible I'd like a simpler solution. 我知道我可以将文件发送到带有curl的REST接口,但如果可能的话,我需要一个更简单的解决方案。

(I would be great if the solution works on ECS, too. But I do not yet understand the implications of that.) (如果该解决方案也可以在ECS上使用,我将非常高兴。但是我尚不了解其含义。)

Dockerfile: Dockerfile:

FROM centos:6

RUN yum -y update
RUN yum -y install epel-release && yum clean all
RUN yum -y install python-pip && yum clean all

ADD . /myapp
WORKDIR /myapp

# install requirements
RUN pip install -r requirements.txt
# install 
#RUN pip install .

CMD ["python","./app.py", "/data.txt"]

script: 脚本:

import sys


def print_out(datafile):
    with open(datafile, 'r') as dfile:
        for line in dfile:
            print line.strip()


def main():
    datafile = sys.argv[1]
    print_out(datafile)


if __name__ == '__main__':
    main()

data.txt: data.txt:

Bob
Steve
Marvin
Paul

run the container (does not work like that) 运行容器(不能那样工作)

$ docker run myapp data.txt $ docker运行myapp data.txt

Use a host volume (aka bind mount) to map your script (assuming it's called app.py) and data.txt into the container. 使用主机卷(aka绑定安装)将脚本(假设其名为app.py)和data.txt映射到容器中。 Place them both in the same directory and then run: 将它们放在同一目录中,然后运行:

docker run -v `pwd`:/myapp myapp python app.py data.txt

From that directory to map the current directory to /myapp (overlaying whatever is inside the image at that location). 从该目录将当前目录映射到/ myapp(覆盖该位置映像内的内容)。 If you're on Docker for Windows/Mac, make sure you're under the c:/Users or /Users directory. 如果您使用的是适用于Windows / Mac的Docker,请确保您位于c:/ Users或/ Users目录下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM