Virtual environment construction with Docker + Flask (Python) + Jupyter notebook

As a memorandum because I forget how to build it every time.

file organization

project
    L Dockerfile
    L app.py

Dockerfile Since there is a possibility to install a library that uses java, default-jdk is added.

Dockerfile


FROM python:3.6
RUN apt-get update && apt-get install -y \
    default-jdk \
    build-essential \
    gfortran \
    libblas-dev \
    liblapack-dev \
    libxft-dev \
    swig \
    && rm -rf /var/lib/apt/lists/*
RUN echo 'export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"' >> ~/.bash_profile && \
    . ~/.bash_profile && \
    cd ~ &&\
    git clone https://github.com/taku910/mecab.git && \
    cd mecab/mecab && \
    ./configure  --enable-utf8-only && \
    make && \
    make check && \
    make install && \
    cd ../mecab-ipadic && \
    ./configure --with-charset=utf8 && \
    make && \
    make install &&\
    cd ~ &&\
    git clone --depth 1 https://github.com/neologd/mecab-ipadic-neologd.git && \
    cd mecab-ipadic-neologd && \
    ./bin/install-mecab-ipadic-neologd -n -y
RUN pip3 install --upgrade pyzmq --install-option="--zmq=bundled" && \
    pip3 install --upgrade jupyter && \
    pip3 install --upgrade \
    pandas \
    neologdn \
    Flask \
    numpy \
    Pillow \
    tensorflow \
ENV LD_LIBRARY_PATH "/usr/local/lib:$LD_LIBRARY_PATH"
VOLUME /notebook
WORKDIR /notebook
EXPOSE 8888
ENTRYPOINT jupyter notebook --ip=0.0.0.0 --allow-root --no-browser

Creating a Docker image

Go to the Dockerfile directory and run the docker build command. It will take a few minutes to build.

$ cd project
$ docker build -t image_name --force-rm=true .

# (-t image name)Decide the image name yourself
# (--force-rm=true)If the image build fails, the image will be deleted automatically

Creating a Docker container

When you execute the drawing command, Jupyter notebook will start automatically, so you can use Jupyter notebook by accessing the displayed URL.

#Execute the following command in the same directory as above
$ docker run -v `pwd`:/notebook -p 8888:8888 -p 5000:5000 -it --name container_name image_id /bin/bash

# http://127.0.0.0:8888/?token=####################
#An address like this will appear, so copy and paste each token.

Install additional libraries

If you are working on Jupyter notebook and want to install the missing libraries, from outside the container, enter the container with the command below.

$ cd project
$ docker exec -it container_name /bin/bash

#When you enter the container with the above command, the terminal will switch as shown below.
root@username:/notebook#You will be able to enter commands here.

#Installation example
root@username:/notebook# pip install numpy

Flask environment construction

Create app.py in the same directory as Dockerfile and execute the following code and command to start the application.

app.py


from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello World!"

if __name__ == '__main__':
    app.run()
$ python hello.py

Recommended Posts

Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Analytical environment construction with Docker (jupyter notebook + PostgreSQL)
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Build Jupyter Lab (Python) environment with Docker
Virtual environment with Python 3.6
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
Try using conda virtual environment with Jupyter Notebook
From Python environment construction to virtual environment construction with anaconda
Prepare python3 environment with Docker
Switch virtual environment with jupyter
python3.8 venv environment jupyter notebook
[Python] OpenCV environment construction with Docker (cv2.imshow () also works)
Data analysis environment construction with Python (IPython notebook + Pandas)
Pepper-kun remote control environment construction with Docker + IPython Notebook
Get started with Python! ~ ① Environment construction ~
Build python virtual environment with virtualenv
Build Mysql + Python environment with docker
Easy Jupyter environment construction with Cloud9
Create a virtual environment with Python!
Building a virtual environment with Python 3
[MEMO] [Development environment construction] Jupyter Notebook
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
pytorch @ python3.8 environment construction with pipenv
Data science environment construction with Docker
[docker] python3.5 + numpy + matplotlib environment construction
Application development with Docker + Python + Flask
Reflect the virtual environment created with Miniconda in Jupyter notebook
From environment construction to deployment for flask + Heroku with Docker
I want to use a virtual environment with jupyter notebook!
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Python environment construction
Vue.js + Flask environment construction memorandum ~ with Anaconda3 ~
[Python] Create a virtual environment with Anaconda
Environment construction (python)
Create Python + uWSGI + Nginx environment with Docker
How to use jupyter notebook without polluting your environment with Docker
python environment construction
Python --Environment construction
Linking python and JavaScript with jupyter notebook
Python environment construction
Launch environment with LineBot + Heroku + Docker + Python
Build a python virtual environment with pyenv
python environment construction
Let's get along with Python # 0 (Environment construction)
Comfortable Jupyter Lab (Python) analysis environment created with Docker + VSCode + Remote Container
Environment construction of Flask / MySql / Apache / mod_wsgi / virtualenv with Redhat7 (Python2.7) November 2020
I wanted to use jupyter notebook with docker in pip environment (opticspy)
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
[Cloud102] # 1 Let's get started with Python (Part 2 Jupyter Notebook Construction AWS Edition)
Collecting information from Twitter with Python (Environment construction)
Prepare the execution environment of Python3 with Docker
Python (Windows 10) Virtual Environment / Package with VS Code
Create a virtual environment with conda in Python
[Python] Build a Django development environment with Docker
Installation of Python3 and Flask [Environment construction summary]
Create Nginx + uWSGI + Python (Django) environment with docker
Super easy! Python + Flask environment in Docker quickly
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
Work in a virtual environment with Python virtualenv.
First python ① Environment construction with pythonbrew & Hello World !!
Error running Jupyter Notebook in Anaconda virtual environment