I just want to have a Docker Image of Postgresql on Docker Hub, with preloaded data saved, so I can pull it and create a container whenever I want, even sharing the image with other people for testing general stuff (just as a code repo on Github). I also want to update the preloaded data with data that I’m currently working on, for personal projects (never including sensitive data).
Hope these instructions might help you too, Developer or SysAdmin.
Requirements
Of course, having a Docker Hub account, with Docker engine installed and Docker client authenticated, are necessary in order to work with Docker. But besides that, without having a image repo created on Docker Hub, there’s no place to upload locally built images.
This is the repo which I created:
Postgresql-based Container
First, I need to create a container based on Postgres official image repo (version 12).
I used to be more detail-oriented on creating containers (docker pull, docker create and docker start), but for the general operations, docker run is way more practical:
$ docker run -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres:12
Unable to find image 'postgres:12' locally
12: Pulling from library/postgres
a803e7c4b030: Pull complete
89aefdc7e2ee: Pull complete
9ed34906f4b2: Pull complete
731d5308799a: Pull complete
91955551cb08: Pull complete
f3e0af904c74: Pull complete
906febd4182a: Pull complete
e7f594a72a36: Pull complete
0857ffe75e3a: Pull complete
29faa49de6a6: Pull complete
de18a1e43334: Pull complete
caec1f1d9715: Pull complete
c75a666c6c4d: Pull complete
Digest: sha256:81ab067d5573cdd38f6dac3de637aa1d6e923c3504bd93a79f1fb397675342f4
Status: Downloaded newer image for postgres:12
22f9858b88b5c8fca20bf7263aca18a4335836b6519bdebfa50d99823979113d
Creating Some Data
By the way, I’m taking a course on advanced DBMS topics, and for the tests that I need right now, this is enough:
$ psql -U postgres -h 127.0.0.1
Password for user postgres:
psql (12.16 (Ubuntu 12.16-0ubuntu0.20.04.1))
Type "help" for help.
postgres=# CREATE TABLE accounts (id SERIAL PRIMARY KEY UNIQUE NOT NULL, holder VARCHAR(64) NOT NULL, balance INTEGER NOT NULL);
CREATE TABLE
postgres=# INSERT INTO accounts (holder, balance) VALUES ('Dennis Ritchie', 1000), ('Steve Jobs', 1000);
INSERT 0 2
postgres=# SELECT * FROM accounts;
id | holder | balance
----+----------------+