How to setup Mysql/MariaDB using Docker

Install mariadb using docker in ubuntu

In this blog, I’ll guide you through setting up and configuring a MySQL database within Docker containers. The tutorial explores topics like connecting to MySQL servers, using MySQL clients to interface with containers.

1. Verify Installation

Check if Docker is installed correctly by running:

sudo docker --version

If you don't have docker in your machine here is the step-by-step instructions for installing Docker Engine.

2. Install MySQL/MariaDB Using Docker

Once Docker is installed, you can proceed to install MySQL or MariaDB.

a. Pull the Docker Image:

You need to download the Docker image for MySQL or MariaDB. You can specify any version you want to install. Ex: mariadb:10.11, mariadb:11.4 ..

For MySQL:

sudo docker pull mysql:latest

For MariaDB:

sudo docker pull mariadb:latest

docker pull: Downloads the image from Docker Hub, the official Docker repository. The Docker image contains all the necessary packages pre-installed. When you docker pull, it downloads this image with everything included

b. Run the Container:

Start the container with MySQL or MariaDB.

For MySQL:

sudo docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=rootpassword -d mysql:latest

For MariaDB:

sudo docker run --name mariadb-container -e MARIADB_ROOT_PASSWORD=rootpassword -d mariadb:latest

  • docker run: Creates and starts a new container.
  • docker run: Creates and starts a new container.
  • --name: Assigns a name to the container (mysql-container or mariadb-container).
  • -e MYSQL_ROOT_PASSWORD: Sets the environment variable for the MySQL root user password.
  • -d: Runs the container in detached mode (in the background).

c. Check Running Containers:

sudo docker ps -a

docker ps: Lists running containers, showing the container ID, image, and other details.

d. Access the Database:

You can connect to the MySQL/MariaDB instance using the command line inside the Docker container.

For MySQL:

sudo docker exec -it mysql-container mysql -uroot -p

For MariaDB:

sudo docker exec -it mariadb-container mariadb -uroot -p
  • docker exec: Runs a command inside a running container.
  • -it: Interactive mode with a TTY (Terminal).
  • mysql/mariadb: The MySQL or MariaDB client.

3. Manage Data Persistence

By default, data in Docker containers does not persist after the container is deleted. To keep data between container restarts, use Docker volumes or bind mounts.

a. Using Volumes:

Create a Docker volume and mount it to the container:

For MySQL:

sudo docker volume create mysql-data
sudo docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=rootpassword -v mysql-data:/var/lib/mysql -d mysql:latest

For MariaDB:

sudo docker volume create mariadb-data
sudo docker run --name mariadb-container -e MARIADB_ROOT_PASSWORD=rootpassword -v mariadb-data:/var/lib/mysql -d mariadb:latest
  • docker volume create: Creates a named volume.
  • -v mysql-data:/var/lib/mysql: Mounts the volume to the MySQL/MariaDB data directory inside the container.

b. Using Bind Mounts:

Mount a local directory to the container for persistent storage:

For MySQL:

sudo docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=rootpassword -v /path/to/local/mysql:/var/lib/mysql -d mysql:latest

For MariaDB:

sudo docker run --name mariadb-container -e MARIADB_ROOT_PASSWORD=rootpassword -v /path/to/local/mariadb:/var/lib/mysql -d mariadb:latest

/path/to/local/mysql or /path/to/local/mariadb: Replace with your local directory path.

By following these steps, you should be able to successfully install and manage MySQL or MariaDB in Ubuntu using Docker. This setup allows for efficient containerization and easy database management.




Here are some useful Docker commands for managing a Mysql/MariaDB container for you:

In this example, I used 'mysql' as my container name. You can replace it with your container name. 

If you want to dump entire databases from a MySQL Docker container using the mysqldump command, you typically use the --all-databases option.

sudo docker exec mysql mysqldump -u root -pYourPassword --all-databases > /path/to/file/mydb.sql

This command is used to retrieve the IP address of a Docker container named 'mysql'. 

sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql

Use the docker cp command to copy the SQL file into the MariaDB container.

sudo docker cp /path/to/file/mydb.sql mysql:/database.sql (mysql is CONTAINER_NAME_OR_ID)

This command is used to open an interactive terminal session within a running Docker container named 'mysql'.

sudo docker exec -it mysql bash
To stop the database container:
sudo docker stop mysql

List Running Containers

sudo docker ps

To remove existing database container

docker rm CONTAINER_ID

Thank You!