Docker – Running MariaDB as a container
Docker simplifies database management, ensuring consistency across environments. Let us delve into a practical approach to understanding the management of MariaDB in a Docker container.
1. Introduction
Docker is an open-source platform used for the containerization of applications. It allows developers to package their applications along with their dependencies, libraries, and other necessary components into a single container that can run reliably and consistently on any platform. The containerization technology provided by Docker ensures that the application behaves the same way regardless of the underlying infrastructure. Some benefits of Docker are:
- Portability: Docker containers can run on any platform, regardless of the underlying infrastructure. This makes it easy to move applications between development, testing, and production environments.
- Scalability: Docker allows you to quickly and easily scale your application by adding or removing containers as needed, without having to make changes to the underlying infrastructure.
- Isolation: Docker provides a high level of isolation between applications, ensuring that each container runs independently of others, without interfering with each other.
- Efficiency: Docker containers are lightweight and efficient, consuming fewer resources than traditional virtual machines. This allows you to run more applications on the same hardware.
- Consistency: Docker ensures that applications behave the same way across different environments, making it easier to test and deploy new versions of your application.
- Security: Docker provides built-in security features that help protect your applications from external threats. Docker containers are isolated from each other and the underlying infrastructure, reducing the risk of attacks.
If someone needs to go through the Docker installation, please watch this video.
2. MariaDB in a Docker container
To set up MariaDB in Docker we will be making use of the docker-compose.yml
file. A Docker Compose YML file is a configuration file that defines how Docker containers should behave when started as part of a multi-container application. It specifies services, networks, and volumes, along with their settings and relationships, simplifying the deployment and management of interconnected Docker containers.
2.1 Creating and Starting the Container
Let us break down the key components of the Docker Compose file (mariadb-docker-compose.yml
) that will enable you to deploy a MariaDB container with specified configurations and persistent data storage.
- Version: The
version: '3.1'
indicates the version of the Docker Compose file syntax being used. - Services: The
services
section defines the containers that make up the application.image: mariadb:latest
: Specifies the Docker image to be used (latest version of MariaDB).container_name: my_mariadb
: Assign a custom name to the MariaDB container.restart: no
: Specifies that the container should not automatically restart.environment
: Sets environment variables for MariaDB root password, including user, password, and database.ports: - " 3306:3306"
: Maps port 3306 on the host to port 3306 on the MariaDB container.volumes: - ./data:/var/lib/mysql
: Mounts the./data
directory on the host to the/var/lib/mysql
directory in the container. This allows data to persist even if the container is stopped or removed
version: '3.1' services: mariadb: image: mariadb:latest container_name: my_mariadb environment: MYSQL_ROOT_PASSWORD: mysecretrootpassword MYSQL_DATABASE: welcome MYSQL_USER: mariadb MYSQL_PASSWORD: mysecretpassword ports: - "3306:3306" volumes: - ./data:/var/lib/mysql restart: no
2.1.1 Starting the Container
The docker-compose up
command will read the configuration file, and create and start the service defined within it. The command will start the MariaDB container with the given settings and expose it on a port 3306
of the host system while persisting data in data
directory on the host.
docker-compose -f mariadb-docker-compose.yml up
2.1.2 Verifying the Container
The docker ps -a
command will list both running and stopped containers. We will use the filter
attribute to display the mariadb container.
docker ps -a --filter "name=my_mariadb"
2.2 Accessing the Container Logs
The docker-compose logs
command is useful for troubleshooting, monitoring, or simply observing the activities of the containers defined in the compose configuration.
docker-compose -f mariadb-docker-compose.yml logs
2.3 Accessing the Container
The docker exec
command is useful in running an interactive shell inside the container. This command helps in troubleshooting, inspecting the container’s file system, or running specific commands.
docker exec -it my_mariadb mariadb -u mariadb -p
Once you’re inside the container we can trigger some sql commands. You can download the SQL scripts from the Downloads section.
2.4 Stopping the Container
The docker-compose down
command is useful to stop the container defined in the compose configuration.
docker-compose -f mariadb-docker-compose.yml stop
2.5 Re-running the Container
To re-run the container trigger the docker-compose up
command.
docker-compose -f mariadb-docker-compose.yml up
2.6 Removing the Container
The docker-compose down
command is useful for stopping and removing the containers, networks, and volumes defined in the compose configuration.
docker-compose -f mariadb-docker-compose.yml down
3. Conclusion
In conclusion, leveraging MariaDB within a Docker container provides a flexible and efficient solution for database management. Docker’s containerization technology allows for the encapsulation of the MariaDB database, ensuring consistency across various environments and simplifying deployment processes. The ability to define configurations, set up environments, and manage dependencies using a Docker Compose file streamlines the deployment and scaling of MariaDB instances. Furthermore, Docker facilitates seamless collaboration by encapsulating the database and its dependencies, reducing compatibility issues. With the added benefit of easy portability, MariaDB in a Docker container enhances development workflows, making it a versatile choice for both local development environments and production deployments. The combination of MariaDB’s robust relational database capabilities and Docker’s containerization brings a powerful and convenient solution to modern database management challenges.
You can download the commands from the Downloads section.
4. Download the Code
This was a tutorial to download the commands used in this article.
You can download the code of this example here: MariaDB in a Docker container