I wanted to create a crisp environment and throw it away for hands-on or other uses. I made it so that it can be built in a few files and a few seconds.
I only do the basics, so if Docker works, it should be manageable. My execution environment is Public Beta of Docker for Mac.
version
$ docker --version
Docker version 1.12.0, build 8eab29e, experimental
Local directory structure
flask/
├ Dockerfile
├ requirements.txt
└ hello.py
Dockerfile
#Specifying the base image
FROM python:3.5.2-alpine
#Store the directory where the source is placed as a variable
ARG project_dir=/web/hello/
#Copy required files from local to container
ADD requirements.txt $project_dir
ADD hello.py $project_dir
# requirements.Install the package listed in txt
WORKDIR $project_dir
RUN pip install -r requirements.txt
#(When working inside a container) Install the required packages
RUN apk update
RUN apk add zsh vim tmux git tig
requirements.txt
$After running pip install flask$ pip freeze > requirements.txt
#### **`requirements.txt`**
```txt
click==6.6
Flask==0.11.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
Werkzeug==0.11.10
If you don't mind changing the package version Simply `` `$ pip install flask``` in the container is perfectly fine.
hello.py Borrowed from Official Page.
app.The argument of run is modified for development use.
#### **`hello.py`**
```py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello World!"
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)
Image creation
$ cd /path/to/flask/
$ docker build -t flask .
*'-t flask': Name the image you want to create'flask' *'.': Use Dockerfile in the current directory
Container startup
$ docker run -p 5000:5000 -it flask /bin/sh
*'-p 5000: 5000': Point host port 5000 to container port 5000 *'-it': Work with container on current input device *'flask': Specify image name *'/ bin / sh': Execute sh command in the started container
Only execute because host and port are already specified in hello.py
.
Run Flask
$ cd /path/to/hello/
$ python hello.py
If you access `localhost: 5000``` from your browser, you will see ``` Hello World!
`.
Until now, I used Ansible to make full use of pyenv and virtualenv. I'll throw it away anyway, and I think it's okay to roughly manage version control.
Python installation is left to the base image If you want to develop properly, you can read the Dockerfile or make it yourself.