Docker:不读文件

时间:2017-09-15 13:58:05

标签: python docker cmd dockerfile

我使用Dockerfileapp.pyrequirements.txt构建一个简单的应用。当Dockerfile构建时,我得到错误:"没有这样的文件或目录"。但是,当我在Dockerfile中将ADD更改为COPY时,它可以工作。你知道为什么会这样吗?

我正在使用教程:https://docs.docker.com/get-started/part2/#define-a-container-with-a-dockerfile

error error fixed

App.py

    from flask import Flask
    from redis import Redis, RedisError
    import os
    import socket

    # Connect to Redis
    redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

    app = Flask(__name__)

    @app.route("/")
    def hello():
        try:
            visits = redis.incr("counter")
        except RedisError:
            visits = "<i>cannot connect to Redis, counter disabled</i>"

        html = "<h3>Hello {name}!</h3>" \
               "<b>Hostname:</b> {hostname}<br/>" \
               "<b>Visits:</b> {visits}"
        return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

requirements.txt

Flask
Redis

Dockerfile

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

1 个答案:

答案 0 :(得分:1)

在第一次运行中,您的工作目录在容器内是/app,并将内容复制到/tmp。要纠正此行为,您应该将内容复制到/app,它将正常工作。

第二个,您使用add的地方是正确的,因为您要将内容添加到/app。而不是/tmp