Database in Docker in Seconds - Never Struggle With Databases Again
Database in Docker in Seconds - Never Struggle With Databases Again

In this post I want to show you how to install any database on any operational system without pain.

Problems?

So what problems do we have? When we try to install some database on an operational system (especially on Windows) we can get different errors. Across the years lots of my students while going through my courses installed different databases on their machines and they got lots of different errors.

Mongo problems

For example in MongoDB you must create a database directory where MongoDB will store it's data. Some people have problems with access to this folder, then MongoDB won't work. Which means it doesn't always work out of the box.

The same I can say about PostgreSQL. Actually there we have such this which is called EDB Installer. It installs on all operational systems PostgreSQL in the same way. But even then people have problems.

Use Docker

So what is the solution? Just use Docker. This is the solution which works for all my students. Why? Docker isolates our database inside Linux container. Then in doesn't really matter if you are using Linux, MacOS or Windows. It will work anywhere.

In Docker you don't need to create additional folders, configure some rules or writes - it will just work out of the box. So what do you need to do that?

Docker desktop

First of all you must install Docker Desktop on your computer. But you need it only on Windows and MacOS. On Linux it doesn't make a lot of sense as docker works smoothly there.

Docker desktop app

When you start Docker Desktop is looks like this. This is just a list of containers that you have available on your machine. For you it is an empty list. Now we can jump to terminal and check if docker is properly installed.

docker --version

How to use

Now I want to show you 3 single liners for PostgreSQL, MySQL and MongoDB to install and start database so you can connect to it and use it.

Let's say that we want to use MongoDB

docker run --name mongo-db -p 27017:27017 -e MONGO_INITDB_ROOT_PASSWORD=123 -e MONGO_INITDB_ROOT_USERNAME=admin -d mongo

Here we create a container with name mongo-db and image mongo. By using -p we expose port for using outside of the container on our local machine. As MongoDB works on port 27017 which is exactly what we exposed. Additionally we provided environment variables for root password and username.

Mongodb started

You can see that mongo-db container is highlighted in green as it is running. So we can directly connect to Mongodb with these credentials and we are good to go.

The same we can do with PostgreSQL.

docker run --name postgres-db -p 5432:5432 -e POSTGRES_PASSWORD=123 -d postgres

As you can see the command is similar. We just changed the port and an image to postgres. Our postgres is ready to connect.

And the last command that I want to show you is MySQL.

docker run --name mysql-db -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123 -d mysql

Mysql

You can see that as it is not downloaded yet on my machine it fetches the image and starts it.

How to access them?

Now the main question is how to access the databases it they are inside the containers. This is what we need to write

docker exec -it postgres-db psql -U postgres

It executes specific command on selected container. Here we want to execute psql -U postgres on the container postgres-db. After this we jump directly to the psql console which allows us to fully access our databases. You can do exactly the same commands with Mongodb for example by writing.

docker exec -it mongo-db mongosh

And actually if you want to learn how to setup Docker from empty folder to a fully functional deployed application make sure to check Docker and Docker Compose - Project Deployment From Scratch.