Database in a Docker container — how to start and what’s it about

Photo by Radek Grzybowski on Unsplash
$ docker run --name postgres-docker -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres
  • it pulls the postgres Docker image from Docker Hub,
  • sets the POSTGRES_PASSWORDenvironment variable value to postgres,
  • names (--name) the Docker container to be postgres-docker ,
  • maps container’s internal 5432 port to external 5432 port, so we’ll be able to enter it from outside,
  • and enables to run the Docker container in the background (-d).
$ docker exec -it postgres-docker bash
root@377ef2b9b13e:/# psql -U postgrespsql (11.4 (Debian 11.4-1.pgdg90+1))
Type "help" for help.
postgres=# CREATE TABLE public.persons (id int PRIMARY KEY, lastName varchar(255), firstName varchar(255), address varchar(255));

Create own PostgreSQL Docker image from Dockerfile

FROM postgres 
COPY init.sql /docker-entrypoint-initdb.d/
  • first, it tells Docker to pull postgres image (we’ve covered it already in previous step),
  • then we set up values of two environment (ENV) variables POSTGRES_PASSWORD and POSTGRES_DB to be postgres and testdb respectively (list of all available variables in this image can be found in Docker Hub),
  • and finally input (COPY) an init.sql file, located in the same folder as Dockerfile, to the /docker-entrypoint-initdb.d/ folder located in postgres Docker image that we’re using. By default all scripts located in this folder will be automatically ran during container startup.
CREATE TABLE public.persons (
firstName varchar(255),
lastName varchar(255),
address varchar(255)
$ docker build -t my-postgres-image .Sending build context to Docker daemon  62.46kB
Step 1/4 : FROM postgres
---> 79db2bf18b4a
Step 2/4 : ENV POSTGRES_PASSWORD postgres
---> Running in 0e9f8331845e
Removing intermediate container 0e9f8331845e
---> 01fb59dfd17f
Step 3/4 : ENV POSTGRES_DB testdb
---> Running in 2d424d207e71
Removing intermediate container 2d424d207e71
---> 2139195ef615
Step 4/4 : COPY init.sql /docker-entrypoint-initdb.d/
---> d627b332ac02
Successfully built d627b332ac02
Successfully tagged my-postgres-image:latest
$ docker images -aREPOSITORY          TAG     IMAGE ID      CREATED               
my-postgres-image latest d627b332ac02 About a minute ago
$ docker run -d --name my-postgres-container -p 5555:5432 my-postgres-image
INSERT INTO public.persons 
(id, firstname, lastname, address)
(1, 'Luke', 'Skywalker', 'Tatooine'),
(2, 'Leia', 'Organa', 'Alderaan'),
(3, 'Han', 'Solo', 'Corellia');
$ docker stop my-postgres-containermy-postgres-container
$ docker container start my-postgres-container

Should I put database into Docker on production?

Docker Volumes

$ docker container inspect my-postgres-container"Mounts": [{
"Type": "volume",
"Name": "453e993be5d9f6f863313c3e111e5f53dc65eeb34bff42e5b",
"Source": "/var/lib/docker/volumes/453e993be5d9f6f863313c3e111e5f53dc65eeb34bff42e5b/_data",
"Destination": "/var/lib/postgresql/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
$ docker run -d --name my-postgres-volume -p 7777:5432 -v postgres-volume:/var/lib/postgresql/data my-postgres-image2109993939fdc9fe290c3536bdee09dd4cfba2ff369cf15a17bc841afe0c056f
$ docker container inspect my-postgres-volume"Mounts": [{
"Type": "volume",
"Name": "postgres-volume",
"Source": "/var/lib/docker/volumes/postgres-volume/_data",
"Destination": "/var/lib/postgresql/data",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
$ docker volume lsDRIVER         VOLUME NAME
local 453e993be5d9f6f863313c3e111e5f53dc65eeb34bff42e5b
local postgres-volume
$ docker volume create --name my-postgres-volumemy-postgres-volume
$ docker run -d --name my-postgres-volume-2 -p 2222:5432 -v my-postgres-volume:/var/lib/postgresql/data my-postgres-image
$ docker run -d --name my-postgres-volume-3 -p 3333:5432 -v my-postgres-volume:/var/lib/postgresql/data my-postgres-image
"Mounts": [{
"Type": "volume",
"Name": "my-postgres-volume",
"Source": "/var/lib/docker/volumes/my-postgres-volume/_data",
"Destination": "/var/lib/postgresql/data",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
$ docker stop container my-postgres-volume-3$ docker start my-postgres-volume-3

Clearance of unused volumes

$ docker volume rm {volume_name}
$ docker volume prune




Java Software Developer, DevOps newbie, constant learner, podcast enthusiast.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Creating a Machine Learning Auto-Shoot bot for CS:GO. Part 5.

How to use Azure search index

Crafting Compilers: What Are Programming Languages?

Become an Autonomous Mobile Robotics Programmer (This Weekend)

Become an Autonomous Mobile Robotics Programmer (This Weekend)

Let’s study distributed systems — 1. Introduction

HDFS Architecture guide

My coding journey: building a Contact Manager

How to fabricate a solid relationship with Developer and Tester?

Making Flutter development easier with Fvm

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Wojciech Krzywiec

Wojciech Krzywiec

Java Software Developer, DevOps newbie, constant learner, podcast enthusiast.

More from Medium

Inter-Microservice Communication with OpenFeign using AWS AppMesh

Liquibase — Keep your database on track

Event Driven Microservice for dummies

What exactly is Apache Maven?