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"!
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!
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.
Images that can be used with Raspberry Pi have "rpi-", so please refer to them when searching.
If you do not have Docker or development environment, please refer to the following. If you have Docker installed, you can skip it!
See Install Docker on RaspberryPi3!
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).
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.
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
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
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
Install pip3
# apt-get install -y python3-pip
Upgraded pip3
# pip3 install --upgrade pip
Confirm pip3 installation
# pip3 -V
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
Create groups and users
# groupadd web
# useradd -d /home/bottle -m bottle
Log in as the created user
# su -m bottle
$
no problem!
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
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
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)
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)
Application launch
# /usr/local/bin/python3 /home/bottle/server.py
When opened in a browser
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?)
Install Raspberry Pi 3 → Wireless LAN → Japanese input / output → Operate from Mac
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!
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
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!
Coding rules "Let's write gentle code" (FuelPHP) Naming convention "Friendly to yourself, team-friendly, and unseen members after 3 months"
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