Run Your Local Database in Docker! 🐳
By: Marte Løge
Tags: Postgres, Docker Image, Docker, Containers, Software Development

Docker database image for local development:

Run Your Local Database in Docker! 🐳

How you can set up your local development database using docker. Fast and simple.

Photo by Ishant Mishra on Unsplash

When setting up a new project there is sometimes a comprehensive list of prerequisites to be able to run a project. In this short tutorial, we will set up a local database using the docker image. As an example, we will use PostgreSQL but you are free to pick your database of choice.

Prerequisite: The first and only thing you need is Docker. I downloaded Docker Desktop to get started on my mac (you are free to use whatever docker installation you prefer).

Let's get started! 🙌

✌️My 3 main reasons why

My three main reasons for using docker database image for your local development and why I think you should do it too:

It makes it easy for local development setup. A simple abstraction using a one-line setup for getting your database set up! It does not require you to have any databases installed at all (or a specific version of the database) on your computer. It isolates the database environment. It allows me to run multiple versions and instances at the same time without getting in trouble.

I usually add this as an option to project setup in my README file. People can then decide what to do — either doing it the manual way or do it the easy way 😄

You are not restricted to using the PostgreSQL image — docker has database images for all the common databases out there like Redis, MySQL, Oracle, etc. Find your favorite.

Let's ship that container!

We will spin up a new docker container using the Postgres image to get a database running:

$ docker run --name my_postgres -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres

There you go! We are basically done - in one line you have everything set up 😄 🤷‍♀ (If you don't have any images installed, docker will ask if you want to download it). Let's have a look at what we just created:

--name my postgres creates a docker container named my_postgres .

creates a docker container named . -p is the flag for setting the port. 5432:5432 maps the internal docker port to the external port so we can have a door from the outside to the docker image. Port 5432 is a Postgres specific port — if using another database image you might have to change this.

is the flag for setting the port. maps the internal docker port to the external port so we can have a door from the outside to the docker image. Port is a Postgres specific port — if using another database image you might have to change this. -e is a flag for environment variables. We added the POSTGRES_PASSWORD .

is a flag for environment variables. We added the . The last part postgres is the docker image we will use (if you use another database you should change this)

Now you have a docker container named my_postgres running with a PostgreSQL database on port 5432. The database has the name , username and password set to postgres.

👩‍💻 Some details

Ok, let's continue to look at some docker flags and commands:

-d if you want to run your database in the background as a daemon. Without the d flag, you need to have your container running in a separate terminal at all times to access your database. You can start and stop the container by running docker start <docker_name> or docker stop <docker_name> .

if you want to run your database in the background as a daemon. Without the flag, you need to have your container running in a separate terminal at all times to access your database. You can start and stop the container by running or . docker postgres:latest if you want to ensure you use the latest Postgres image.

if you want to ensure you use the latest Postgres image. docker pull postgres to pull the latest Postgres image.

to pull the latest Postgres image. docker images to list all images on your computer.

to list all images on your computer. docker ps -a to see all created containers (the ones having an assigned port is the running containers).

to see all created containers (the ones having an assigned port is the running containers). docker rm <docker_ID> or docker rm <docker_name> to remove a docker container.

or to remove a docker container. docker volume prune to wipe all database data from your docker container. Even though you have several docker containers running you will still access the same data as it is written to the same place.

💅 Where can I admire my new database?

You can pick up your favorite database GUI tool and connect to your database. I like to use DBeaver. Connect to our newly created database running in our Docker container by using:

Database name: postgres

postgres Database user: postgres

postgres Database password: postgres

postgres Port: 5432

If you like to work with your database in the terminal you can access the database your docker container typing this command (our docker in this tutorial was named my_postgres):