Part 6: A Place for Everything
This series of posts is dedicated to building tools that support a simulation of simplified D&D gameplay. To implement the simulation I’ll be using git, Python, PostgreSQL, Elastic, and Docker. The idea behind these blog posts is to document my progress through this project and interesting bits I find along the way. If you’d like to start from the beginning, have a look at part 1, where I talked about where this idea came from, and what I’d like to accomplish. The github repository for this project holds the latest version of the files mentioned in these posts. To follow along, make the following directories and clone the repository. In this part, I’ll be setting up a database that will hold the data for the simulation.
(rpg) ~$ mkdir docker_data (rpg) ~$ mkdir Git (rpg) ~$ cd Git (rpg) ~/Git$ git clone git@github.com:mdbdba/python_rpg_sim.git
To build the simulation, I am using Docker CE to build a database container. It allows me to define exactly define the space where the database run, which hopefully means that anyone wanting to follow along will have an easier time doing so. Later in the series, when I add a web server and a document service to the mix this approach will let me define those services and the network between them the same way. In addition to that, Docker has saved my laptop from a sea of confused versions of software and their dependencies. No worrying about things half uninstalling or not going away at all. For that, I continue to sing their praise.
Docker has excellent documentation on how to set up their software. For this project I am using Docker CE and Docker Compose. Once they are set up, getting the PostgreSQL database running will be easy. The repository’s docker sub directory holds the definition for the “pgs” database container in a file called “dockerFile” and builds it to the specs in the docker-compose.yml file. Using the “docker-compose build” command will create a Docker image for the database.
(rpg) ~$ cd python_rpg_sim/docker (rpg) ~/Git/python_rpg_sim/docker$ docker-compose build pgs Building pgs Step 1/2 : FROM library/postgres:10 10: Pulling from library/postgres f17d81b4b692: Pull complete c1f213be5edb: Pull complete 9c79723dc510: Pull complete 603a66804109: Pull complete b4f1b901e523: Pull complete 99d9650419de: Pull complete 02d87bb25bad: Pull complete 333a24caa91e: Pull complete 4397f5e806b6: Pull complete 8d2ae79c725e: Pull complete c5d43dcb4656: Pull complete 31057a95d6b0: Pull complete f53849339b6c: Pull complete e30b608d9fa9: Pull complete Digest: sha256:d525a6cc26d06c8481870f17ae8a98a6022eaee6ce48959f392f0111b386e6c5 Status: Downloaded newer image for postgres:10 ---> fc21dac1eb4e Step 2/2 : COPY sql/init.sql /docker-entrypoint-initdb.d/ ---> b08a4aab7322 Removing intermediate container e36369809d6b Successfully built b08a4aab7322 Successfully tagged mdbdba/postgres:10
Using the “docker-compose up” command will create a container that runs the database service. (If you’re like me and start asking “Sure, but how do I stop it?” as soon as someone tells you how to start something, the answer is: “docker-compose down”)
(rpg) ~/Git/python_rpg_sim/docker$ docker-compose up -d pgs Creating network "docker_default" with the default driver Creating pgs ... done
A quick check with the “docker ps -a” command to make sure things came up okay.
(rpg) ~/Git/python_rpg_sim/docker$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21288885fa87 mdbdba/postgres:10 "docker-entrypoint..." 6 seconds ago Up Less than a second 0.0.0.0:5433->5432/tcp pgs
Ta-da! One database container called “pgs” ready to go. Two things to note from the output above. The name of the container is the last field “pgs” and the field before that, “0.0.0.0:5433->5432/tcp”. That output means that to get at this PostgreSQL database, this computer’s port 5433 is how to get there.
So, having a database running is okay, but for it to be of any use, I’ve got to be able to connect to it. There are many database query tools that support PostgreSQL. For this series, I will be using a product called DBeaver. It’s the one that works easiest for me, and like Docker, has a community edition. Below is a quick setup for a connection using that tool. If you’d like to use another tool, the information for the connection will be the same. ( the password for the rpg_admin user can be found in the python_rpg_sim/docker/pgs/sql/init.sql file)
Once you have DBeaver installed click on the “New Connection” button.
Select the database type, PostgreSQL. Then Next.
Add the details about the connection. The password for the rpg_admin user is in the python_rpg_sim/docker/pgs/sql/init.sql file.
Click the “Test Connection” button and you should get back some good news.
Click OK in the Success window and then click Finish in the Create New Connection window. You should then see the connection you created in the left pane. Double clicking on it will expand out the databases that exist there. For this series we are going to be working with the “rpg” database and we’ll be putting all of our information in the dnd_5e schema.
Now that the database exists and there’s a way to look into it, that’s a good bit of work for today. After getting out of DBeaver, to take down the database container use the “docker-compose down” command.
(rpg) ~/Git/python_rpg_sim/docker$ docker-compose down Stopping pgs ... done Removing pgs ... done Removing network docker_default
In the next post I’ll look how using source control for databases helps for a project like this and demonstrate that by loading a fair amount of data. Until then, go play!