As said in my earlier post, my python application has a PostgreSQL backend running on a separate VM. But in order to fully appreciate Docker, I decided to try out PostgreSQL on Docker.
First, let’s pull the official PostgreSQL image for alpine
$ docker pull postgres:15-alpine
Then run the image and include the environment variable POSTGRES_PASSWORD
for now. We will also add a volume for data persistence.
$ docker run -itd --rm --name psql \
> -e POSTGRES_PASSWORD=******** \
> -v ./dbdata:/var/lib/postgresql/data \
> postgres:15-alpine
Now, let’s look at the container ip address so we can access it using psql
.
$ docker inspect bridge
# output truncated
...
"ConfigOnly": false,
"Containers": {
"6c414502dbe431761f838726180b073b579a855c178a996ff57e9b8510a33f4d": {
"Name": "psql",
"EndpointID": "562ba21b5b1c1cf18e535887c0c77366f6f087c4f2db6b154e0ccdb7f970eede",
"MacAddress": "22:81:bd:2d:1c:52",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
...
To access the container using psql
$ psql -h 172.17.0.2 -d postgres -U postgres
Password for user postgres:
psql (14.12 (Ubuntu 14.12-0ubuntu0.22.04.1), server 15.7)
WARNING: psql major version 14, server major version 15.
Some psql features might not work.
Type "help" for help.
postgres=#
The warning message is because I have a version 14
psql
installed on my machine but I accessed a version 15 server
That’s it! Now, I can configure my python application to connect to this database container and I will then deploy them using docker-compose