Run Your Local Database in Docker! 🐳 | by Marte Løge | Towards Data Science

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 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.
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.
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.
-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.
-e is a flag for environment variables. We added the POSTGRES_PASSWORD.
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, usernameand password set to postgres.
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 runningdocker start <docker_name> or docker stop <docker_name>.
docker postgres:latest if you want to ensure you use the latest Postgres image.
docker pull postgres to pull the latest Postgres image.
docker images 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).
docker rm <docker_ID> or docker rm <docker_name>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.
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
Database user: postgres
Database password: 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):
$ docker exec -it <DATABASE_NAME> bash
🎉Happy coding 🐳 Will you start using docker database images for your local setup?