Docker

Is it Good to Run Database in Docker?

Docker is a well-liked, free, and rapidly growing platform. It is mostly used to deploy and share the application and software in Docker containers. Docker containers are small executable packages that encapsulate all the project dependencies along with the source code. The Docker user also uses a Docker container to execute applications, software, and databases of different platforms.

This article will demonstrate whether is it good to run the database in Docker.

Is it Good to Run Databases in Docker?

When a user requires database portability, isolation, and reproducibility, running a database in a Docker container can be beneficial. However, it will have an impact on the system’s functionality and put users in danger of data loss because containers hold data temporarily, and if they fail or stop working, the data may be erased. In this case, mounting the volume can be the best scenario to save data. Running a database in Docker is also a complex task and users need to have deep knowledge about Docker and its components.

How to Run a Database in Docker?

Multiple Docker images are available for different databases in Docker Hub’s official registry such as postgres, mariadb, mysql, mongodb, elasticsearch, and many more. To utilize the database inside a Docker container, follow the listed steps.

Step 1: Pull Image and Execute Database in Docker Container

To automatically pull the database image and fire up the container, use the “docker run <option> <database-image>” command. For instance, we have executed the “mariadb” image to use MariaDB in the container:

docker run --detach --name MariaDB-cont --env MARIADB_USER=Root --env MARIADB_ROOT_PASSWORD=root mariadb:latest

In the above command:

  • –detach” is utilized to execute the container in the background.
  • –name” specifies the container’s name.
  • –env” is used to define the environment variable. For instance, we have defined a database username and root user password.
  • mariadb:latest” is an official Docker image to run the MariaDB container:

Step 2: Start Container Bash Shell

Next, execute the container shell to access the MariaDB database inside the container. For this purpose, simply use the “docker exec -it <container-name> bash” command:

docker exec -it MariaDB-cont bash

Step 3: Access and Use Database

Next, access the MariaDB by executing the below command inside the container. Here, the “-u” option specify the user name, and “-p” option is used to provide the user password:

mysql -u root -p

To check whether the MariaDB is executing in the container check its version using the MariaDB instance:

SELECT VERSION();

The output shows that we have successfully run the MariaDB inside the container:

That’s all about running a database in Docker.

Conclusion

Running a database in a Docker container can be beneficial for some reasons such as portability, isolation, and reproducibility. However, it will impact the system’s performance, and data in a container can have a data loss risk. Another reason is Docker structure is complex so running a database in Docker can also be a difficult task. This article has demonstrated whether is it good to run the database in Docker.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.