Search This Blog

Monday, 12 December 2016

Some Docker stuff

I want to extend my Xmas Lights project so I can record when the lights have been on/off.  I figure a database is the best way to do this, so not wanting to learn something like Mongo at this point, I figured a Postgres DB was probably easiest.  Not wishing to get into the installation process I wondered whether Docker might hold a quick solution to my problem.  Here are a few things I learned.

Platform:
Windows 8.1 running HyperV - LAN IP 192.168.0.60
Linux VM : "CentOS Linux release 7.2.1511 (Core)", logged in as "root"

Setup docker
sudo yum install docker

Configure host firewall
firewall-cmd --permanent --zone=trusted --change-interface=docker0
firewall-cmd --permanent --zone=trusted --add-port=4243/tcp
firewall-cmd --reload
(interestingly, I found that I couldn't start containers with firewalld disabled)

Create Postgres container
docker run --name postgres -p 5432:5432 -d postgres
(I should really use "-e POSTGRES_PASSWORD=xxx", but omitting it seems to just allow the "postgres" login to work with a blank password)
docker inspect postgres
(output contains IP address : "IPAddress": "172.17.0.2".  This is the Docker internal address.  It appears that by default other containers on this internal network can see each other)

Create PGAdmin4 container
docker run --name pgadmin --link 1337c2af44ac:postgres -p 5050:5050 -d fenglc/pgadmin4
(not sure about the --link bit, I think the version of Docker I'm using is much newer and handles internal networking differently, but this command worked OK)

Use WebUI to create a database "xmas16" and a table "messages" with columns ID(integer) and message(text).  Put some test data in it.

Web browser to http://192.168.0.60/browser/ (IP of host CentOS VM) to access PGAdmin console.  Connected PGAdmin to Postgres server "172.17.0.2"

In Python, installed psycopg2 module using PIP

Python code (running on separate test PC or RPi):

import psycopg2
conn = psycopg2.connect("dbname='xmas16' user='postgres' host='192.168.0.60' password=''")
cur = conn.cursor()
cur.execute("select * from messages")
rows = cur.fetchall()
for row in rows:
    print (row[1])

Done!  My app wrote my test messages to the stdout.  Learning Docker took an hour or two, especially the firewalld bit, but I have a full PoC rig up and running with very little effort.  Considering my objective is to capture non-sensitive data from a Xmas light project for fun it's more than adequate.

No comments:

Post a Comment