Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]

Introduction

My name is Ryosuke Kamei and I am an IT consultant based on the idea of "gentle IT"! Currently, in my work, I am doing upstream processes focusing on requirements analysis and requirements definition, but since I am also writing programs other than my main business, I will write technical articles! As part of our friendly IT activities, we will introduce the goods and services programs created with "Raspberry Pi 3" (commonly known as Raspberry Pi), which has the philosophy of "providing inexpensive PCs that can be programmed for education"!

Purpose

Build a Python-MySQL environment with Docker on Raspberry Pi. Raspberry Pi struggled with few images because the architecture is very different. First, record the process of trial and error. If it works, I will write the easy construction version!

Architectural differences

Since Raspberry Pi is based on the armv7l architecture, Docker also needs to use an image based on the armv7l architecture, but the current situation is that there are few.

When looking for an image of Raspberry Pi

Images that can be used with Raspberry Pi have "rpi-", so please refer to them when searching.

table of contents

  1. Preparation
  2. Launch hypriot / rpi-python
  3. Install the libraries required for Python3 installation
  4. Install Python3
  5. Install pip3
  6. Install the tools needed for Python development
  7. Ready to install bottle
  8. Check if the server is running
  9. Install the MySQL driver
  10. Create data in the database
  11. Put the script on the application server
  12. Launch the application

1. Preparation

If you do not have Docker or development environment, please refer to the following. If you have Docker installed, you can skip it!

1-1. Install Dokcer

See Install Docker on RaspberryPi3!

1-2. Convenient functions (SSH / VNC / AFP)

It is convenient to implement the following chapter of Raspberry Pi 3 installation → wireless LAN → Japanese input / output → operation from Mac (SSH / VNC server / AFP).

  1. Access by SSH
  2. VNC settings (manipulate Raspberry Pi from Mac with GUI)
  3. AFP settings (access Raspberry Pi file system on Mac)

2. Launch hypriot / rpi-python

A pirate (laughs) who calls himself hypriot provides an image for Raspberry Pi, so I'm grateful to use it.

hypriot/rpi-Start python and log in with bash


sudo docker run -it hypriot/rpi-python /bin/bash

If you think about it, this image is based on Python 2.7 ... (as of September 4, 2016) I want to use Python3 series, so install it.

3. Install the libraries required for Python3 installation

The usual magic (library updates and updates)


# apt-get update
# apt-get upgrade -y

Try installing a library that seems to be needed


# apt-get install -y vim
# apt-get install -y sudo
# apt-get install -y wget
# apt-get install -y git

4. Install Python3

4-1. Install the libraries required for Python3 installation.

Install the libraries required for Python3 installation


# apt-get install -y build-essential libncursesw5-dev libgdbm-dev libc6-dev zlib1g-dev libsqlite3-dev tk-dev libssl-dev openssl libbz2-dev libreadline-dev

4-2. Install Python3

Python3 compiles from source. (I've been touching make and make install for the first time in 10 years because of Python3 installation)

Download Python 3 source


# wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

Extract the compressed Python-3.5.2.tgz with tar.

bash:Python-3.5.2.Expand tgz


# tar xvf Python-3.5.2.tgz 

Move

Move


# cd Python-3.5.2

Install with make. (This will take some time)

Install with make


# ./configure && make && make install

Check when the installation is complete.

Confirm Python3 installation


# python3 -V

5. Install pip3

Install pip3


# apt-get install -y python3-pip

Upgraded pip3


# pip3 install --upgrade pip

Confirm pip3 installation


# pip3 -V

6. Install the tools needed for Python development

If you want to know more details, please ask Mr. Google.

Virtual development environment construction virtualenv


# pip3 install virtualenv

Lightweight framework bottle


# pip3 install bottle

Python interactive mode extension ipython


# pip3 install ipython

Coding style/Syntax check flake8


# pip3 install flake8

7. Create user

Create groups and users


# groupadd web
# useradd -d /home/bottle -m bottle

Log in as the created user


# su -m bottle
$ 

no problem!

8. Check if the server is running

index with vim.Create html (user is bottle)


$ vim index.html

index.html


<html>
<body>
Hello World
</body>
</html>

Start with simple HTTP


$ python3 -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 ...

Access with a browser ラズパイとDocker.png

9. Install the MySQL driver

Download mysql-connector-python with git clone

git clone


# git clone https://github.com/mysql/mysql-connector-python.git

Move


# cd mysql-connector-python

Build and install


# python3 ./setup.py build
# python3 ./setup.py install

10. Create data in the database

Exit the application server once. Enter Control + P, Control + Q.

Log in to the database server. If it is already running, log in with the following command.

Log in to the database server


pi@raspberrypi $ sudo docker exec -it {Database server container name}

Log in to MySQL


mysql_server # mysql -u{User name} -p
(Enter password)

Access database (premise already exists)


mysql> use sample;

Create table


mysql>
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Insert data


mysql>
INSERT INTO `users` (`id`, `name`) VALUES (1, 'pyhons');

Exit MySQL


mysql> quit;

Exit the database container


mysql_server # (Contrl + p, Control + q)

11. Put the script on the application server

Log in to the application server


pi@raspberrypi $ sudo docker exec -it {Application server container name}

Move to a suitable place


# cd /home/bottle

server.Create py


# vim server.py

server.py


from bottle import route, run
#MySQL driver is mysql.connector
import mysql.connector
#The IP address of host is$ docker inspect {Database container name}Find out in
#MySQL users, passwords and databases are docker-compose.What was set in yml
connector = mysql.connector.connect(
            user='python',
            password='python',
            host='178.10.0.3', #Database server IP address
            database='sample'  #Created database
)

cursor = connector.cursor()
cursor.execute("select * from users")

disp = ""
for row in cursor.fetchall():
    disp = "ID:" + str(row[0]) + "name:" + row[1]

cursor.close
connector.close
			
@route('/hello')
def hello():

    return "Get from DB"+disp

run(host='0.0.0.0', port=8080, debug=True, reloader=True)

12. Launch the application

Application launch


# /usr/local/bin/python3 /home/bottle/server.py

When opened in a browser スクリーンショット 2016-09-04 14.18.55.png

In the next article, I'll try to launch it in one shot with Dockerfile and dokcer-compose.yml. (Is it big? Is it big?)

site map

Raspberry Pi 3 setup

Install Raspberry Pi 3 → Wireless LAN → Japanese input / output → Operate from Mac

Build a Python + MySQL environment with Docker on Raspberry Pi 3!

Install Docker on RaspberryPi3 Build a Python + bottle + MySQL environment with Docker on RaspberryPi3![Easy construction] Build a Python + bottle + MySQL environment with Docker on RaspberryPi3![Trial and error]

Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!

Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!

Finally ... Make a radio control using python on Raspberry Pi 3!

Motor moves while pressing the button The motor moves while the magnet is brought closer The motor moves when the magnet is brought closer and stops automatically

Let's play with Raspberry Pi 3 (Raspberry Pi) and python Record of Raspberry Pi struggle

Programming with Node-RED programming with Raspberry Pi 3 and programming normally Light the LED with python on Raspberry Pi 3 (Hello World) Detect switch status on Raspberry Pi 3 Run a servo motor using python on Raspberry Pi 3 Control the motor with a motor driver using python on Raspberry Pi 3! Detect slide switch using python on Raspberry Pi 3! Detect magnet switch using python on Raspberry Pi 3! Detect temperature using python on Raspberry Pi 3! Sound the buzzer using python on Raspberry Pi 3! Detect analog signals with A / D converter using python on Raspberry Pi 3! Detect "brightness" using python on Raspberry Pi 3! Detect "temperature (using A / D converter)" using python on Raspberry Pi 3! Output to "7-segment LED" using python on Raspberry Pi 3! Use python on Raspberry Pi 3 to light the LED with switch control! Use python on Raspberry Pi 3 and turn on the LED when it gets dark!

Rules focused on test-driven development

Coding rules "Let's write gentle code" (FuelPHP) Naming convention "Friendly to yourself, team-friendly, and unseen members after 3 months"

Web application development with Docker + Python

Install Python3, related libraries pip, virtualenv and frameworks Django, bottle, Flask on CentOS on Docker! With a Dockerfile that summarizes these!

Easy to develop environment construction (Docker + PHP)

PHP environment + Eclipse is linked to Apache using Docker Building FuelPHP development environment using Docker Create CRUD skeleton using initial settings of FuelPHP development environment using Docker and scaffold FuelPHP database migration

Recommended Posts

Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Build Mysql + Python environment with docker
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a Python environment on your Mac with Anaconda and PyCharm
Build a python environment with ansible on centos6
[Python] Build a Django development environment with Docker
Build PyPy and Python execution environment with Docker
Build a python virtual environment with virtualenv and virtualenvwrapper
How to build a Django (python) environment on docker
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python3 environment on CentOS7
Build Jupyter Lab (Python) environment with Docker
Build Python3 and OpenCV environment on Ubuntu 18.04
Build a python environment on MacOS (Catallina)
A memo with Python2.7 and Python3 on CentOS
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Build Python environment with Anaconda on Mac
[Linux] Build a jenkins environment with Docker
Build a python virtual environment with pyenv
Build a Python + OpenCV environment on Cloud9
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Build a modern Python environment with Neovim
Build NGINX + NGINX Unit + MySQL environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Build a LAMP environment on your local Docker
Build a WardPress environment on AWS with pulumi
Build python environment with pyenv on EC2 (ubuntu)
Building a python environment with virtualenv and direnv
Simply build a Python 3 execution environment on Windows
Build a Django environment on Raspberry Pi (MySQL)
Build a Python environment on Mac (Mountain Lion)
Create a python3 build environment with Sublime Text3
Build a Python development environment on your Mac
Build a virtual environment with pyenv and venv
Build a Python environment with OSX El capitan
Quickly build a Python Django environment with IntelliJ
Build a Python machine learning environment with a container
Build a Python development environment on Raspberry Pi
Build a python execution environment with VS Code
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
Build a TensorFlow development environment on Amazon EC2 with command copy and paste
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
Install Python3 on Mac and build environment [Definitive Edition]
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
Building a Docker working environment for R and Python
Build a machine learning Python environment on Mac OS
Build a python environment for each directory with pyenv-virtualenv
I made a Python3 environment on Ubuntu with direnv.
Build a GVim-based Python development environment on Windows 10 (1) Installation
Build a Python development environment on Mac OS X
Build a Python virtual environment using venv (Django + MySQL ①)
Build a Python environment on your Mac using pyenv
Build a machine learning application development environment with Python
Build a Python development environment using pyenv on MacOS
Create a decent shell and python environment on Windows