SECRET OF CSS

Run Databases With Docker | Nicholas Obert


Run your database with a single shell command

0*PTjPdPGiwrHLzTuy
Photo by Josh Rakower on Unsplash

If you have worked with databases, you would probably agree that the installation and setup process is quite lengthy, and there are often numerous errors to fix before your database is ready for development.

All this time you spend trying to get your favorite database to work could be better used for the actual software development rather than wasted on debugging.

This is where Docker comes to the rescue. If you’ve been living under a rock, Docker is an open-source containerization platform that enables developers to package applications into easy-to-work-with, standardized containers and effortlessly distribute them without caring about system compatibility issues.

In this article, I will show you how to use Docker to seamlessly run a database instance on any machine without needing painful setups, along with a simple automation script.

Note that I will be using the PostgreSQL database management system as an example because it’s what I generally use in my development, but these steps and concepts are also valid for other databases like MySQL.

The first step is to install Docker on your machine. Since the installation process highly depends on your operating system, I will just redirect you to the official download page where you can find detailed instructions for macOS, Windows, and Linux:

After the installation is complete, you can move on to the next step. Note that you may get some errors when connecting to the Docker daemon. In that case, the Docker process is probably not running, so start it. Alternatively, you may get an error about missing permission to connect to the Docker socket. In that case, you might need to add your user to the docker group. Anyway, eventual troubleshooting is beyond the scope of this article, so let’s move on.

If you wish to use a different database, you can just change Postgres with your own choice.

To start with Postgres, you just have to pull the postgres image from the Docker Hub. This is as easy as typing the following command in the console:

docker pull <image>
docker pull postgres

Alternatively, you can just run a Postgres container directly, and Docker will pull the image automatically for you if it’s not already cached:

docker run --rm -it -p <port mapping> [--name <optional container name>] -e POSTGRES_PASSWORD=<password> -e POSTGRES_USER=<user> [-e POSTGRES_DB=<optional database name>] <image>docker run --rm -it -p 5432:5432 --name "my_pg" -e POSTGRES_PASSWORD="postgres" -e POSTGRES_USER="postgres" -e POSTGRES_DB="my_db" postgres

To better understand this command:

  • --rm automatically removes the container when it exits, omit to preserve the container.
  • -it allows you to interact with the container from the console.
  • -p specifies the port mapping (5432 is the default Postgres port).
  • --name specifies the container name, useful for later management. Alternatively, Docker will assign a random name.
  • -e specifies an environment variable, in this case, the database user and password.

If you wish to use a different version of Postgres than the latest, you can specify it after the image name like this:

<image>:<version>
postgres:13
postgres:latest

At this point, you have a running Postgres instance you can connect to. Now you can connect a bash console to your running container using:

docker exec -it <container id> bash

or alternatively:

docker exec -it <container name> bash

To get the container id or name, you can list all active containers with the following:

docker ps

To stop the container, you can press Ctrl+C in the interactive terminal where you launched the Postgres instance. Alternatively, you can run the following command:

docker stop <contaier id>

So far, you have everything you need to start using your development database server in a Docker container. However, you can save even more time with a simple database setup script to create eventual tables, indices, and other structures.

Here is a very primitive bash script that first launches the Docker container with your selected image and creates a database with the specified name if it doesn’t already exist. Then it tries to connect to the running instance and apply the specified SQL setup commands.

This is a database setup script template in bash. However, for finer control over the container and the database instance, you may want to take a look at Dockerfile or Docker Compose.

Instead of going through the pain of setting up your new development database, you should consider using Docker to run a containerized instance of your database server without the need to install anything directly on your system.

Just as easily as you created your containers, you can delete them with:

docker ps -a # list the containers
docker rm <container>

or remove the images with:

docker images # list the images
docker image rm <image>

Without worrying about any garbage that may be left on your system after removing the database.

By the way, you may also consider running other applications in a Docker container beside your database servers. Moreover, you can even choose to containerize your whole development environment and have different, specialized containers already packaged with everything you need to start developing without the need for installing any additional software or worrying about dependencies and conflicts.

For more information about developing inside a Docker container, check out this guide for Visual Studio Code.

Your time is the most valuable asset you can’t have enough of. Don’t let the clock run in vain.

I hope you enjoyed this article. If you have anything to add, please share your thoughts in a comment. Thanks for reading!

If you’re interested in other useful tools to save more time as a developer, you should check out this story below:



News Credit

%d bloggers like this: