【问题标题】:Docker: not reading fileDocker:不读取文件
【发布时间】:2017-09-15 13:58:04
【问题描述】:

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

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

应用程序.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"]

【问题讨论】:

  • 你能显示两个构建的日志吗?
  • 复制了你的 Dockerfile,并尝试构建,一切正常,你的 requirements.txtDockerfile

标签: python docker cmd dockerfile


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2019-07-27
    • 2018-12-23
    • 1970-01-01
    • 2013-10-10
    • 2012-08-24
    相关资源
    最近更新 更多