Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)

I thought I'd use Python a little, but I didn't want to install various things to build the environment. So I tried to build an environment using Docker.

We aim to create an environment where you can develop with VS Code (or code-server), using both an interactive environment and the Web. In particular, I was addicted to connecting VS Code's Python extension to Jupyter that runs on Python in Docker, so I will leave the environment construction procedure as a memo in this article.

Requirements

--Python program can be executed with CLI -Python can be executed interactively using Jupyter -Using Flask, you can output Python on the Web. -Code completion using Python runtime with Visual Studio Code or code \ -server Etc. can be done

Build a development environment that can do all of the above.

environment

Operation confirmed in the following environment

Environment construction procedure

First, cut a suitable working directory.

$ mkdir python-dev && cd $_

Initialize Git if you like.

$ git init .
$ wget https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore --output-document=.gitignore

Use Poetry for package management. Write Dockerfile and docker-compose.yml for building the environment with Poetry.

Dockerfile


FROM python:3.8.3-slim
WORKDIR /app

RUN pip install poetry

Specify the container image that contains the version of Python you want to use. In the future, I will use the slim image because I want to reduce the size of the image at first, while keeping in mind that I will want to install various dependent packages in the image.

docker-compose.yml


version: '3.4'
services:
  web:
    build: .
    volumes:
      - .:/app
    environment:
      - FLASK_APP=index.py
    ports:
      - 127.0.0.1:5000:5000
    command: "poetry run flask run --host=0.0.0.0"
  note:
    build: .
    volumes:
      - .:/app
    ports:
      - 127.0.0.1:8888:8888
    command: "poetry run jupyter notebook --no-browser --ip=0.0.0.0 --port=8888 --allow-root --NotebookApp.token='' --NotebookApp.password='' --NotebookApp.disable_check_xsrf=True"

This time, for the convenience of accessing Jupyter in the container from the host, some settings are included to disable the security function of Jupyter.

If you run it on a public server rather than on your computer's local environment, you should avoid this setting.

Once docker is ready, set up Poetry.

$ docker-compose run --rm web poetry init --no-interaction
$ docker-compose run --rm web poetry add jupyter notebook flask

Add a command to $ poetry install when building Docker.

Dockerfile


FROM python:3.8.3-slim
WORKDIR /app

RUN pip install poetry
COPY pyproject.toml ./
RUN poetry install

Place index.py to launch in Flask.

index.py


from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"

Build and you're done.

$ docker-compose build

How to Use

After $ docker-compose up

--Use with CLI - $ docker-compose run --rm web python --version --Use on the Web --Go to http://127.0.0.1:5000. --Use with Jupyter --Go to http://127.0.0.1:8888.

Use Python in Docker with VS Code

First, install the following in VS Code.

-Remote Development Extension (ms-vscode-remote.vscode-remote-extensionpack)

Select the target container with the Docker extension on VS Code and click "Attach Visual Studio Code". image.png

Open the VS Code window to access the container remotely and install Python Extension (ms-python.python) "Install on Container" Install with the button and reload VS Code.

Success if the Python displayed in VS Code > Python: Select Interpreter etc. is in Docker (Python 3.8.3 in / usr / local / bin / python in this example) is.

image.png

Use Jupyter on VS Code

In VS Code attached to the container as above, in > Python: Specify local or remote Jupyter server for connections, select ʻExisting and enter http: // localhost: 8888 / . Alternatively, create a file called .vscode/settings.json`.

json:.vscode/settings.json


{
  "python.dataScience.jupyterServerURI": "http://localhost:8888/"
}

You can use Jupyter on VSCode by reloading VSCode and doing > Python: Create New Blank Jupyter Notebook.

image.png

If you are asked for a password, just type Enter with the empty string.

image.png

Use Python in Docker with code-server

If you use code \ -server instead of VSCode, it will work in the browser, so especially in the case of Chromebook, it will work lightly on Chrome instead of Linux GUI environment. I will. Since extensions such as Python can be included in the container, there is also the advantage that it can be coded for each development environment.

The disadvantage is that the image capacity is large. (Requires an additional 1GB)

To use code-server, make sure to build the container with code-server and extensions.

Dockerfile


FROM python:3.8.3-slim AS python
WORKDIR /app

RUN pip install poetry
COPY pyproject.toml ./
RUN poetry install

FROM python AS code-server

RUN apt-get update && apt-get install -y curl
RUN curl -fsSL https://code-server.dev/install.sh | sh
RUN code-server \
  --install-extension ms-python.python \
  --install-extension ms-ceintl.vscode-language-pack-ja

Add a service that starts code-server to docker-compose as well.

docker-compose.yml


version: '3.4'
services:
  web:
    build:
      context: .
      target: python
    volumes:
      - .:/app
    environment:
      - FLASK_APP=index.py
    ports:
      - 127.0.0.1:5000:5000
    command: "poetry run flask run --host=0.0.0.0"
  note:
    build:
      context: .
      target: python
    volumes:
      - .:/app
    ports:
      - 127.0.0.1:8888:8888
    command: "poetry run jupyter notebook --no-browser --ip=0.0.0.0 --port=8888 --allow-root --NotebookApp.token='' --NotebookApp.password='' --NotebookApp.disable_check_xsrf=True"
  code:
    build:
      context: .
      target: code-server
    ports:
      - 127.0.0.1:8080:8080
    volumes:
      - ./:/app
    entrypoint: "code-server --auth none --bind-addr=0.0.0.0:8080 /app"

Now do $ docker-compose build and you're done. (It will take some time) You can use VS Code on your browser by visiting http://127.0.0.1:8080.

To use Jupyter with code-server, use > Python: Specify local or remote Jupyter server for connections to specify the container where jupyter is running, such ashttp: // note: 8888 /.

json:.vscode/settings.json


{
  "python.dataScience.jupyterServerURI": "http://note:8888/"
}

References

In addition, the full text of the code created in this article is in the following repository.

https://github.com/s2terminal/python-dev

Recommended Posts

Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
Create a simple Python development environment with VS Code and Docker
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
How to build Python and Jupyter execution environment with VS Code
[Python] Build a Django development environment with Docker
Build a python execution environment with VS Code
Build a Flask development environment at low cost using Docker
UpNext2 Development Record # 1 Build Python CI environment in VS Code
Build Jupyter Lab (Python) environment with Docker
Build and try an OpenCV & Python environment in minutes using Docker
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build Python development environment with Visual Studio Code
Use Python in Anaconda environment with VS Code
Build PyPy and Python execution environment with Docker
How to debug a Python program by remotely connecting to a Docker container in WSL2 environment with VS Code
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Cross-compiling Raspberry Pi and building a remote debugging development environment with VS Code
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a machine learning application development environment with Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a Python development environment using pyenv on MacOS
Build a development environment with Poetry Django Docker Pycharm
Build a lightweight Fast API development environment using Docker
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
Build a basic Data Science environment (Jupyter, Python, R, Julia, standard library) with Docker.
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Set up a Python development environment with Visual Studio Code
Building a Python environment on a Mac and using Jupyter lab
Build a Go development environment with VS Code's Remote Containers
Create a VS Code + Docker development environment on a Linux VM
Build a Python development environment in Eclipse (add HTML editor)
[Django] Build a Django container (Docker) development environment quickly with PyCharm
How to build a python2.7 series development environment with Vagrant
Create a simple Python development environment with VSCode & Docker Desktop
[No venv required] The strongest Python development environment created with Remote Containers [VS Code / Docker]
Are you still using Jupyter Notebook? Enjoy a comfortable Python life with Jupyter Life (.py) with VS Code ?!
Build a go environment using Docker
Build Mysql + Python environment with docker
Application development with Docker + Python + Flask
How to build a Python virtual execution environment using Visual Studio Code and pipenv on a Windows machine (also Jupyter notebook)
Create a Python environment for professionals in VS Code on Windows
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a local development environment for Lambda + Python using Serverless Framework
Build a Python environment on your Mac with Anaconda and PyCharm
Build a detonation velocity website with Cloud Run and Python (Flask)
Build a Python execution environment using GPU with GCP Compute engine
Debug settings in virtual environment when using Pipenv with VS Code
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
[Linux] Build a jenkins environment with Docker
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
[Linux] Build a Docker environment with Amazon Linux 2
Prepare a Python virtual environment for your project with venv with VS Code
I was addicted to creating a Python venv environment with VS Code
I set the environment variable with Docker and displayed it in Python