Skip to main content

Docker - Setting MariaDB

In this chapter on Docker - Setting MariaDB, we will show how to run a MariaDB database with Docker containers. MariaDB is an open-source database management system. It works well and can grow with your needs. This makes it very important for modern web apps. By learning how to set up MariaDB in Docker, we can make our development work easier and improve how we deploy our apps.

First, we will talk about what we need to use Docker and MariaDB. Then, we will help you install Docker. After that, we will pull the MariaDB image and run your first container. Next, we will look at network setup, environment variables, and good ways to manage data. We will also talk about how to secure your MariaDB instance. This will help us understand Docker - Setting MariaDB fully.

Introduction to MariaDB

MariaDB is a free relational database management system. It comes from MySQL. The original MySQL developers made it because they were worried about Oracle buying MySQL. MariaDB keeps the same features as MySQL. This makes it easier for us to switch between the two.

Here are some key features of MariaDB:

  • Better Performance: MariaDB works faster and scales better than MySQL. This is especially true for complex queries.
  • Improved Security: MariaDB has features like data-at-rest encryption. It also has better user management and role-based access control.
  • Storage Engines: MariaDB supports many storage engines. Some of them are InnoDB, Aria, and MyRocks. We can choose the best one for our needs.
  • Community Support: MariaDB is open-source. It has a lively community that helps with its development. This means we get regular updates and improvements.

MariaDB is a strong choice for developers and businesses. It offers a reliable database solution. If we want more information on setting up databases with Docker, we can check out Docker - Setting MySQL or Docker - Setting MongoDB.

Prerequisites for Docker and MariaDB

Before we set up MariaDB in a Docker container, we need to have some things ready:

  1. Operating System: We need a compatible OS like Linux, macOS, or Windows. Using a Linux distribution is better for performance.

  2. Docker Installed: We must install Docker on our system. We can follow the installation guide for Docker.

  3. Docker Compose (Optional): If we want to manage apps with multiple Docker containers, it helps to have Docker Compose.

  4. Basic Command Line Knowledge: We should know how to use the command line. This knowledge is important for running Docker commands.

  5. Network Configuration: We need to check our firewall settings. They should allow Docker to create networks and talk over the needed ports. Usually, we use port 3306 for MariaDB.

  6. Data Storage: We should understand Docker volumes. This is important for keeping MariaDB data safe.

  7. Docker Hub Account: This is not a must, but it is good to have an account for pulling images from Docker Hub.

With these things ready, we can start setting up MariaDB in Docker containers.

Installing Docker

To set up MariaDB in Docker, we first need to install Docker on our system. Docker works on many operating systems like Windows, macOS, and Linux. Let’s follow the steps for each system.

For Windows and macOS:

  1. Download Docker Desktop: Go to the Docker Hub and get the installer for your OS.
  2. Run the Installer: Open the file we downloaded and follow the steps to install.
  3. Start Docker: After we install, we open Docker Desktop. We can check if it is running by looking for the Docker whale icon in the system tray.

For Linux:

  1. Update Package Index:

    sudo apt-get update
  2. Install Required Packages:

    sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
  3. Add Docker GPG Key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  4. Set Up the Stable Repository:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  5. Install Docker:

    sudo apt-get update
    sudo apt-get install docker-ce

After we install, we can check if Docker is running by using this command:

docker --version

Now that we have Docker, we can pull the MariaDB image and set it up in our Docker environment. For more help on setting up different services, we can look at Docker - Setting MySQL.

Pulling the MariaDB Image

To start using MariaDB in Docker, we first need to pull the official MariaDB image from Docker Hub. This image has all we need to run a MariaDB server in a container.

We can use this command to pull the latest MariaDB image:

docker pull mariadb:latest

If we want to get a specific version like 10.5, we can use the version tag:

docker pull mariadb:10.5

After we pull the image, we can check if it is on our local machine by running:

docker images

This command shows all the images we downloaded, including MariaDB. Now we can create a Docker network and run our MariaDB container.

For more details on Docker images, we can look at the article on what are Docker images. Setting up MariaDB with Docker is simple and good for development and testing.

Creating a Docker Network for MariaDB

Creating a Docker network just for MariaDB is important. It helps your MariaDB container talk safely and easily with other containers. Docker networks give us a way to keep things separate and help containers find each other.

To make a Docker network for MariaDB, we can follow these steps:

  1. Open a terminal on our Docker host.

  2. Create a new network by using this command:

    docker network create mariadb-network

    This command makes a bridge network called mariadb-network.

  3. Check if the network is created by listing all Docker networks:

    docker network ls

    We should see mariadb-network in the list of networks.

When we use a special network, our MariaDB container can connect to other containers like app servers. It does this without showing its ports to the host. This makes things safer.

If we want to know more about Docker networking, we can look at Docker Networking. This setup helps us with Docker - Setting MariaDB. It makes a strong environment for our database applications.

Running a MariaDB Container

To run a MariaDB container with Docker, we first need to pull the MariaDB image from Docker Hub. After we get the image, we can easily create and run a container with a simple command. Here is how we do it:

  1. Run the Container: We can use this Docker command to run a MariaDB container. Make sure to change your_password to a strong password for the database root user.

    docker run -d \
      --name mariadb \
      -e MYSQL_ROOT_PASSWORD=your_password \
      -p 3306:3306 \
      mariadb:latest
    • -d: This runs the container in detached mode.
    • --name mariadb: This gives the container the name “mariadb”.
    • -e MYSQL_ROOT_PASSWORD=your_password: This sets the root password for MariaDB.
    • -p 3306:3306: This maps port 3306 of the container to port 3306 on the host.
  2. Verify the Container: After we run the command, we can check if the container is running by using:

    docker ps
  3. Accessing the Container: We can connect to the MariaDB server by using a MySQL client or any database tool. Just use localhost and port 3306.

Running a MariaDB container helps us quickly set up a database system for our applications. For more details on how to manage Docker containers, we can check Docker - Working with Containers and Docker - Container Linking.

Configuring Environment Variables

When we set up MariaDB in a Docker container, we need to configure environment variables. This helps us customize the database. Docker lets us pass these variables when we run the container. This way, we can set up our MariaDB instance correctly without changing the internal files of the container.

Here are some important environment variables for MariaDB:

  • MYSQL_ROOT_PASSWORD: This sets the password for the root user.
  • MYSQL_DATABASE: This makes a database with the name we choose when it starts.
  • MYSQL_USER: This creates a new user who can access the database.
  • MYSQL_PASSWORD: This sets the password for the new user we created.

To set these environment variables, we can use the -e option with the docker run command. Here is an example:

docker run --name my-mariadb -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=mydb -e MYSQL_USER=myuser -e MYSQL_PASSWORD=mypassword -d mariadb:latest

This command will start a new MariaDB container called my-mariadb. It will initialize with the credentials and the database we specified. Configuring environment variables well helps us secure our MariaDB instance. It also makes sure it works for our application needs. For more details, we can look at the documentation on Docker - Setting MariaDB.

Connecting to the MariaDB Container

To connect to our running MariaDB container, we can use different methods. We can use the MySQL command-line client, graphical database management tools, or programming languages that can connect to databases.

  1. Using the MySQL Command-Line Client: If we have the MySQL client installed, we can connect to our MariaDB container with this command:

    docker exec -it <container_name> mysql -u <username> -p

    We need to replace <container_name> with the name of our MariaDB container. Replace <username> with the MariaDB user like root. It will ask us to enter the password.

  2. Using a GUI Tool: We can use tools like MySQL Workbench, DBeaver, or phpMyAdmin to connect to the MariaDB instance. Here are the connection details we need:

    • Host: localhost or the Docker host IP
    • Port: 3306 (this is the default port for MariaDB)
    • Username: Our MariaDB user
    • Password: Our MariaDB password
  3. From a Programming Language: We can also connect using programming languages like Python, PHP, or Node.js. For example, in Python with mysql-connector, we can do it like this:

    import mysql.connector
    
    connection = mysql.connector.connect(
        host='localhost',
        user='your_username',
        password='your_password',
        database='your_database'
    )

By using these methods, we can easily connect to our MariaDB container. Then we can manage our databases well. For more improvements, we can check Docker - Setting MySQL or look into Docker networking for advanced settings.

Managing Data with Volumes

When we set up MariaDB in Docker, managing data is important. We need to keep our data safe when the container restarts or gets updated. Docker volumes help us store data that lasts. With volumes, we can make sure our MariaDB data does not disappear when we remove or recreate the container.

To manage MariaDB data with Docker volumes, let’s follow these steps:

  1. Create a Docker Volume: We use this command to create a volume for MariaDB data storage:

    docker volume create mariadb_data
  2. Run the MariaDB Container with Volume: When we start our MariaDB container, we need to use the -v option to specify the volume:

    docker run -d \
      --name mariadb \
      -e MYSQL_ROOT_PASSWORD=my-secret-pw \
      -v mariadb_data:/var/lib/mysql \
      -p 3306:3306 \
      mariadb:latest
  3. Accessing Data: We can check the volume and manage the data inside it by using:

    docker volume inspect mariadb_data

Using volumes makes data management easier. It also helps us with security and backup plans for our MariaDB. If we want to know more about Docker volumes, we can check Docker Volumes. By managing data well with volumes, we keep our Docker - Setting MariaDB environment stable and reliable.

Securing the MariaDB Instance

Securing our MariaDB instance running in Docker is very important. It helps us stop unauthorized access and data leaks. Here are some key steps to make our MariaDB container more secure:

  1. Use Strong Passwords: We should always set strong passwords for the root user and any other database users. It’s best to avoid default passwords.

  2. Limit User Privileges: Let’s create users with only the necessary privileges. We can use the GRANT command to give specific permissions. This way, users only access the databases they need.

  3. Configure Bind Address: By default, MariaDB listens on all interfaces. We can change the my.cnf file to bind to 127.0.0.1 if we only need local access. We do this by adding the following to our configuration:

    [mysqld]
    bind-address = 127.0.0.1
  4. Network Security: We should use Docker networks to isolate our MariaDB container. It is important to make sure that only trusted services can access the database.

  5. Regular Updates: We need to keep our MariaDB image updated. We can use the command docker pull mariadb often to get the latest security patches.

  6. Data Encryption: It is good to think about enabling SSL for database connections. This helps encrypt data while it travels. We can also use encrypted volumes for data storage.

By following these steps, we can greatly improve the security of our MariaDB instance. For more details on Docker security, please check Docker Security Practices.

Docker - Setting MariaDB - Full Example

We will show how to set up MariaDB with Docker. This full example covers all steps we talked about before. This setup works well for development and testing.

  1. Create a Docker Network:
    First, we need to create a special network for the MariaDB container. This helps keep the communication with other containers separate.

    docker network create mariadb-network
  2. Run a MariaDB Container:
    Next, we run a command to start the MariaDB container. We will set the root password and database name with environment variables.

    docker run --name mariadb-container \
      --network mariadb-network \
      -e MYSQL_ROOT_PASSWORD=rootpassword \
      -e MYSQL_DATABASE=mydatabase \
      -p 3306:3306 \
      -d mariadb:latest
  3. Verify the Setup:
    Now, we check if the container runs well.

    docker ps
  4. Connect to the MariaDB Instance:
    To connect to the MariaDB container, we use this command.

    docker exec -it mariadb-container mysql -u root -p
  5. Manage Data with Volumes:
    If we want to keep data safe, we can create a volume.

    docker run --name mariadb-container \
      --network mariadb-network \
      -e MYSQL_ROOT_PASSWORD=rootpassword \
      -e MYSQL_DATABASE=mydatabase \
      -v mariadb-data:/var/lib/mysql \
      -d mariadb:latest

This full example shows how to set up a MariaDB instance with Docker. For more details on advanced setups, check out topics like Docker - Setting MySQL or Docker - Working with Containers.

Conclusion

In this article about Docker - Setting MariaDB, we talked about important steps. First, we installed Docker. Then, we pulled the MariaDB image. Finally, we configured our environment. These tips make it easier to manage your database in a container. It also improves flexibility and security.

For more learning, we can check out other topics. You can look at Docker - Setting MySQL or Docker - Setting Nginx. This will help us grow our Docker skills.

Comments