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:
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:
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:
To check whether the MariaDB is executing in the container check its version using the MariaDB instance:
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.