Build a Flask development environment at low cost using Docker

Introduction

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.

Preparation

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

Source

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)

procedure

Image creation

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

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

Run Flask

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! `.

Conclusion

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.

Recommended Posts

Build a Flask development environment at low cost using Docker
Build a lightweight Fast API development environment using Docker
Build a go environment using Docker
[Python] Build a Django development environment with Docker
[Mac] Build a Python 3.x environment at the fastest speed using Docker
Build a Django development environment using pyenv-virtualenv on Mac
Build a Python development environment using pyenv on MacOS
Build a development environment with Poetry Django Docker Pycharm
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
[Memo] Build a development environment for Django + Nuxt.js with Docker
[Django] Build a Django container (Docker) development environment quickly with PyCharm
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Easily build a development environment with Laragon
[Linux] Build a jenkins environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
Build a LAMP environment on your local Docker
Build a C language development environment with a container
Build Django + NGINX + PostgreSQL development environment with Docker
Build a Django development environment with Doker Toolbox
Build a Kubernetes environment for development on Ubuntu
Build a mruby development environment for ESP32 (Linux)
Build a Python development environment on Raspberry Pi
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Build a local development environment for Laravel6.X on Mac
Build a GVim-based Python development environment on Windows 10 (1) Installation
How to build a Django (python) environment on docker
I want to easily build a model-based development environment
Build a Python development environment on Mac OS X
Build a Python virtual environment using venv (Django + MySQL ①)
[Go + Gin] I tried to build a Docker environment
Build a Python environment on your Mac using pyenv
Build a machine learning application development environment with Python
How to build a development environment for TensorFlow (1.0.0) (Mac)
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
Building a Jupyter Lab development environment on WSL2 using Anaconda3
Build a Go development environment with VS Code's Remote Containers
Build a web API server at explosive speed using hug
Easily build a GCP environment for Kaggle at high speed
Create a VS Code + Docker development environment on a Linux VM
Build a Python development environment in Eclipse (add HTML editor)
Build a LAMP environment [CentOS 7]
Build a GVim-based Python development environment on Windows 10 (2) Basic settings
Build a comfortable development environment with VSCode x Remote Development x Pipenv
[ev3dev × Python] Build ev3dev development environment
Build a machine learning environment
How to build a python2.7 series development environment with Vagrant
Create a simple Python development environment with VSCode & Docker Desktop
Create a Todo app with Django ① Build an environment with Docker
Build a Python environment offline
Until building a Python development environment using pyenv on Ubuntu 20.04
How to build a LAMP environment using Vagrant and VirtulBox Note
[Docker] Build an environment of python (Flask) + GraphQL (graphene) + MySQL (sqlalchemy)
Build a Chainer environment using CUDA and cuDNN on a p2 instance
Try using virtualenv, which can build a virtual environment for Python
The simplest way to build a Spleeter usage environment using Windows
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Build and try an OpenCV & Python environment in minutes using Docker
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry