mariusz
mariusz

Reputation: 167

How to create proper Dockerfile

I'm completely new to Docker, but I have to create Docker Compose of this application.

I decided to start from building an image and run it as one container, so I cloned repository and created following Dockerfile:

    FROM python:2.7-slim
    WORKDIR /flask
    COPY requirements.txt requirements.txt
    RUN pip install -r requirements.txt
    CMD ["python", "routes.py"]

The image was created successfully, but when I used run command I received:

python: can't open file 'routes.py': [Errno 2] No such file or directory

My Dockerfile is in the same directory as routes.py, so I have no idea why he doesn't see it.

EDIT: Copy directory to the container solved the problem with opening file. But the web content it's not display. docker run tagName command returns:

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 262-019-209

However when I type docker container ls my container is not on the list. So maybe he's still not created.

routes.py file:

from flask import Flask, render_template
app = Flask(__name__)

# two decorators, same function
@app.route('/')
@app.route('/index.html')
def index():
    return render_template('index.html', the_title='Tiger Home Page')

@app.route('/symbol.html')
def symbol():
    return render_template('symbol.html', the_title='Tiger As Symbol')

@app.route('/myth.html')
def myth():
    return render_template('myth.html', the_title='Tiger in Myth and Legend')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

Upvotes: 0

Views: 89

Answers (1)

vivekyad4v
vivekyad4v

Reputation: 14903

You need to copy your current directory/file to the container -

FROM python:2.7-slim
WORKDIR /flask
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "routes.py"]

OR

FROM python:2.7-slim
WORKDIR /flask
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY routes.py ./
CMD ["python", "routes.py"]

Make sure to bind it to all hosts i.e 0.0.0.0 instead of localhost.

Ref - Configure Flask dev server to be visible across the network

Upvotes: 1

Related Questions