Prepare the execution environment of Python3 with Docker

"Creating a service that edits the usage history of Mobile Suica so that it can be easily used for expense settlement" First of all, we will develop in Python, so create an environment where Python can be executed

Writer's environment

windows PC (Core i7-7600 / 16GB) Edit with VS code It is convenient to include Python and Docker extensions

policy

Since it is developed on a Windows PC, it is troublesome to do it directly. Python execution environment sets up a container with Docker

Preparation

Install ** Docker for Windows ** It is necessary to enable Hyper-V. Also install [** docker-compose **] * [Docker] Install Docker and Docker Compose on Windows 10 Pro 64bit

Create a Dockerfile

I will install various things, so build the image Save the following file as Dockerfile in a suitable directory somewhere

Dockerfile


FROM centos/python-36-centos7:latest
USER root
RUN yum -y install java-1.8.0-openjdk
RUN pip install flask pandas tabula-py uwsgi
WORKDIR /src
ENV PYTHONUNBUFFERED=1

Create docker-compose.yml

Since you edit the Python file on Windows and run it on Docker, you need to be able to see the Python file on Windows from the container. This can be specified with "-v" when launching the container, but since Windows is troublesome and I do not want to type the command every time, docker-compose makes one command

docker-compose.yml


version: '3.7'
services:

  testenv:
    build:
      context: ./app
      dockerfile: Dockerfile
    container_name: testenv
    tty: true
    command: /bin/sh -c "while :; do sleep 10; done"
    volumes:
      - ./app/src:/src

volumes:
  temp:

If there is no command: part, the container will drop in an instant When I google, most people solve it with tty: true, but for some reason it is useless in my environment

Place the Dockerfile and docker-compose.yml from earlier like this test.py is a Python file to run

│  docker-compose.yml
│
└─app
    │  Dockerfile
    │
    └─src
            test.py

Launch the container

$ docker-compose.exe up --build -d
Creating network "test_default" with the default driver
Building testenv
Step 1/6 : FROM centos/python-36-centos7:latest
 ---> 90c6a4022ee5
Step 2/6 : USER root
 ---> Using cache
 ---> 03e9f59a6dbc
Step 3/6 : RUN yum -y install java-1.8.0-openjdk
 ---> Using cache
 ---> 463337c7b050
Step 4/6 : RUN pip install flask pandas tabula-py PyPDF2 uwsgi crontab
 ---> Using cache
 ---> 26c28d2b838c
Step 5/6 : WORKDIR /src
 ---> Running in f5ab2a9b405e
Removing intermediate container f5ab2a9b405e
 ---> b998a42b8180
Step 6/6 : ENV PYTHONUNBUFFERED=1
 ---> Running in fd8d783f9e2f
Removing intermediate container fd8d783f9e2f
 ---> e1e0833bc33a

Successfully built e1e0833bc33a
Successfully tagged test_testenv:latest

See if you got up

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3b7e97ef1fd5        test_testenv        "container-entrypoin…"   4 seconds ago       Up 2 seconds        8080/tcp            testenv

Try to enter the container

$ winpty docker exec -it testenv bash
(app-root) bash-4.2# pwd
/src
(app-root) bash-4.2# ls
test.py
(app-root) bash-4.2# cat test.py
print('Hello, World')
(app-root) bash-4.2# python3 test.py
Hello, World

Now you can finally start writing the code.

Recommended Posts

Prepare the execution environment of Python3 with Docker
Prepare python3 environment with Docker
Note: Prepare the environment of CmdStanPy with docker
Preparing the execution environment of PyTorch with Docker November 2019
Build PyPy and Python execution environment with Docker
Build Mysql + Python environment with docker
Build PyPy execution environment with Docker
Prepare Python development environment with Atom
Prepare the development environment with anyenv
Unify the environment of the Python development team starting with Poetry
Build Jupyter Lab (Python) environment with Docker
Behind the flyer: Using Docker with Python
Check the existence of the file with python
About the virtual environment of python version 3.7
Build the execution environment of Jupyter Lab
Create Python + uWSGI + Nginx environment with Docker
Launch environment with LineBot + Heroku + Docker + Python
Prepare a distributed load test environment with the Python load test tool Locust
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Introduction to Python Let's prepare the development environment
Unification of Python environment
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
Calculate the total number of combinations with python
[Python] Build a Django development environment with Docker
the zen of Python
Use multiple versions of python environment with pyenv
Check the date of the flag duty with Python
I set the environment variable with Docker and displayed it in Python
Create Nginx + uWSGI + Python (Django) environment with docker
Prepare an environment to touch grib2 format files with python (Docker edition)
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
Use python with docker
Python environment with docker-compose
Prepare the environment of Chainer on EC2 spot instance with AWS Lambda
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
Virtual environment with Python 3.6
Build a python execution environment with VS Code
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Extract the table of image files with OneDrive & Python
Learn Nim with Python (from the beginning of the year).
Japanese can be used with Python in Docker environment
Recommendation of building a portable Python environment with conda
[Python] OpenCV environment construction with Docker (cv2.imshow () also works)
The story of sharing the pyenv environment with multiple users
Destroy the intermediate expression of the sweep method with Python
Use Python installed with pyenv for PL / Python execution environment
Visualize the range of interpolation and extrapolation with python
Calculate the regression coefficient of simple regression analysis with python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Summary of the basic flow of machine learning with Python
Example of pytest environment to fix database with Docker
Install Ubuntu 20.04 with GUI and prepare the development environment
Get the operation status of JR West with Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
Extract the band information of raster data with python
[Python] Chapter 01-02 About Python (Execution and installation of development environment)
Towards the retirement of Python2
Install Python environment with Anaconda