[DOCKER] I tried Flask with Remote-Containers of VS Code

Overview

This is a memo because you can create a Python environment in a Docker container with an extension called Remote-Containers of VS Code.

Purpose of using Docker container

There are two purposes.

The first is that the local environment is clean. There are things such as pyenv and anaconda that switch the environment, but in the end it becomes locally dependent. Even if I make requirements, what is the Python version? I don't know if it's a command, I can't enter it for some reason, I can't mix pip and conda, and sometimes I don't know what it is, but I feel like I want to graduate.

The second is easy to deploy. These days, I sometimes calculate and provide services on the cloud instead of on my local PC, so I'd like to do that with a few Dockerfiles.

That's why you won't get these right away, but in this article I'll record up to Flask in Remote-Container.

First result

I put the final file on github. yo16 / simple_flask

Final file structure.


/simple_flask
    /.devcontainer
        Dockerfile
        devcontainer.json
    /simple_app
        /templates
            default.html
        app.py
    requirements.txt

But in this article, I don't want to tell you the result or contents of the creation, it's a procedure in VS Code, so it doesn't really matter. If you are tired of making a simple source of Flask, please download and use it.

Step 0: Prepare the folder

Create a folder to create this environment. This time "simple_flask".

File structure up to this point.


/simple_app

Step 1: Prepare Flask

Anything is fine, so I will make a simple one.

File structure up to this point.


/simple_flask
    /simple_app
        /templates
            default.html
        app.py

I won't explain the contents of Flask, but app.py has ʻapp.run ()or@ app.route ('/'). Doing this python app.py` will launch the Flask site.

Step 2: Open with VS Code and prepare Docker-Containers

  1. Open the top / simple_flask folder with VS Code
  2. Search for "remote-containers" in the extension on the left toolbar.
  3. Installation

20200926_2.PNG

Step 3: Create a Docker environment

  1. Click on the green area like> <at the bottom left of VS Code

  2. Select "Remote-Containers: Open Folder in Container ..." from the ones that appear on the screen. 20200926_4.PNG

  3. Select the "simple_flask" folder and open it

  4. Select "Python 3" as it will appear above again 20200926_5.PNG

  5. It will come out again, so select the appropriate Python version 20200926_6.PNG

  6. It will come out again, so it's OK (this is the last) By the way, if you check off here and even if 0 is selected, something will be installed, so check it in the later procedure. 20200926_7.PNG

  7. If you can do so far, the screen below will be displayed. The point is a linux-like screen at the lower right terminal. (It really is) This is already in the Docker container and bash is running. 20200926_8.png

So when I try ls there, it looks like this, and "simple_app" is also firmly placed.


root@c249cd5c6b06:/workspaces/simple_flask# ls -la
total 4
drwxrwxrwx 1 root root 4096 Sep 26 14:49 .
drwxr-xr-x 3 root root 4096 Sep 26 13:54 ..
drwxrwxrwx 1 root root 4096 Sep 26 14:49 .devcontainer
drwxrwxrwx 1 root root 4096 Sep 26 14:08 simple_app

Python is also included.


root@c249cd5c6b06:/workspaces/simple_flask# python -V
Python 3.8.5

Step 4: Install Flask

Since the Docker image of python was selected in step 3-4, Python is included, but since it is in its original state, there is no Flask. So I will put it in. First, put this on the console.


root@c249cd5c6b06:/workspaces/simple_flask# pip install flask
Requirement already satisfied: flask in /usr/local/lib/python3.8/site-packages (1.1.2)
Requirement already satisfied: Werkzeug>=0.15 in /usr/local/lib/python3.8/site-packages (from flask) (1.0.1)
Requirement already satisfied: click>=5.1 in /usr/local/lib/python3.8/site-packages (from flask) (7.1.2)
Requirement already satisfied: itsdangerous>=0.24 in /usr/local/lib/python3.8/site-packages (from flask) (1.1.0)
Requirement already satisfied: Jinja2>=2.10.1 in /usr/local/lib/python3.8/site-packages (from flask) (2.11.2)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.8/site-packages (from Jinja2>=2.10.1->flask) (1.1.1)

That's in ... (sweat Well, I think that I may want to put other things in, so in that case I will proceed with this procedure, so I will proceed with the tay that was not included.

Next, create requirements.txt so that the modules that are currently included will be installed automatically when the Docker container is created.

root@c249cd5c6b06:/workspaces/simple_flask# pip freeze > requirements.txt

If you do this, you can of course do it in the Docker container, but you can also do it in VS Code. You can open it normally with VS Code to check and edit it.

20200926_9.PNG

Use this in the next step.

Step 5: Check and modify the Dockerfile

The one that works when creating a container is ./dockercontainer/Dockerfile.

If you open this file with VS Code, you'll see a lot of things that work when you open a Docker container. (I'm using the image of microsoft.)

Here, as explained in step 2-6, Node.js is to be installed with or without installation, so comment out that area and use nodejs. I will try not to. You don't need the simple Flask in this article, but if you need it, use it.

On the contrary, the requirements.txt created in step 4 is not used, so uncomment it. Copy it to / tmp / pip-tmp /, pip install, and delete it when you're done.

The modified Dockerfile is below.

# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.140.1/containers/python-3/.devcontainer/base.Dockerfile

# [Choice] Python version: 3, 3.8, 3.7, 3.6
ARG VARIANT="3"
FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT}

# [Option] Install Node.js
#### comment out
# ARG INSTALL_NODE="true"
# ARG NODE_VERSION="lts/*"
# RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "source /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
#### removed "#"
COPY requirements.txt /tmp/pip-tmp/
RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
   && rm -rf /tmp/pip-tmp

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1

Step 6: Start the Docker container again

Recreate the container again using the modified Dockerfile.

  1. Click on the green area at the bottom left 20200926_10.PNG

  2. Select "Remote-Containers: Rebuild Container" from the pull-down above 20200926_11.PNG

That's all! Thank you for your hard work.

Operation check

If you start app.py with python from the lower right terminal, the usual Flask server will start up.

root@acebfd6333f5:/workspaces/simple_flask# python ./simple_app/app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

That's it!

Recommended Posts

I tried Flask with Remote-Containers of VS Code
I tried handwriting recognition of runes with scikit-learn
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried to get the authentication code of Qiita API with Python.
I tried to extract features with SIFT of OpenCV
[Python] I immediately tried using Pylance's VS Code extension.
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried input interpolation on UE4 Python VS Code
Work memo that I tried i18n with Flask app
I tried "morphology conversion" of images with Python + OpenCV
I tried running the sample code of the Ansible module
I tried fp-growth with python
I tried scraping with Python
Python with VS Code (Windows 10)
I tried Learning-to-Rank with Elasticsearch!
I tried clustering with PyCaret
Debug Python with VS Code
I tried gRPC with Python
I tried scraping with python
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I tried to find the average of the sequence with TensorFlow
I tried starting Django's server with VScode instead of Pycharm
Settings to debug the contents of the library with VS Code
I tried to implement ListNet of rank learning with Chainer
I tried handwriting recognition of runes with CNN using Keras
I tried a stochastic simulation of a bingo game with Python
I tried trimming efficiently with OpenCV
I tried summarizing sentences with summpy
I tried web scraping with python.
I tried moving food with SinGAN
I tried using GrabCut of OpenCV
I tried implementing DeepPose with PyTorch
Try running Jupyter with VS Code
I tried face detection with MTCNN
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried sentence generation with GPT-2
I tried learning LightGBM with Yellowbrick
Install python with mac vs code
I tried face recognition with OpenCV
I tried VS Code's Jupyter notebook
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried standalone deployment of play with fabric [AWS operation with boto] [Play deployment]
Get the strongest environment with VS Code, Remote-Containers and remote docker-daemon
I tried to automate the watering of the planter with Raspberry Pi
I tried to create a list of prime numbers with python
How to develop containers on remote servers with VS Code Remote-Containers
I tried to fix "I tried stochastic simulation of bingo game with Python"
The diagrams were interesting so I tried wrapping them with flask
I tried to expand the size of the logical volume with LVM
I tried running the DNN part of OpenPose with Chainer CPU
I tried to improve the efficiency of daily work with Python
I tried to automatically collect images of Kanna Hashimoto with Python! !!
I tried to make a mechanism of exclusive control with Go
I tried multiple regression analysis with polynomial regression
I tried sending an SMS with Twilio
I tried using Amazon SQS with django-celery
I tried to implement Autoencoder with TensorFlow
I tried a formation flight of a small drone Tello with ESP32: DJI Tello drone formation flight