Docker and Mariadb container install and connect HeidiSQL

1. Step
(1) Install docker
(2) Mariadb container install
(3) connect HeidiSQL

2. Prerequisites
(1) Install gnome-terminal

$ sudo apt install gnome-terminal

(2) Uninstall old versions

$ sudo apt-get remove docker docker-engine docker.io containerd runc

3. Install Docker
(1) Set up the repository

 $ sudo apt-get update

 $ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

(2) Add Docker’s official GPG key:

$ sudo mkdir -p /etc/apt/keyrings

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

(3) Use the following command to set up the repository:

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

(4) Install Docker Engine

$ sudo apt-get update

$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

(5) List the versions available in your repo:

$ apt-cache madison docker-ce

(6) comfirm docker status

 
$ sudo systemctl status docker

4. Run Docker commands without sudo
(1) Check for docker group existence

$ cat /etc/group |grep docker

(2) Add username to docker group

$ sudo usermod -aG docker ${USER}

(3) Apply new group membership

$ su - ${USER}

(4) Confirm added to docker group

$ id -nG
$ cat /etc/group |grep docker

5. Using Docker commands

$ docker info

6. Verify that Docker Engine is installed correctly by running the hello-world image

$ service docker start
$ docker run hello-world

7. Download image to computer
(1) Search image

$ docker search ubuntu

(2) Download image to computer

$ docker pull ubuntu

(3) View image list

$ docker images
REPOSITORY            TAG       IMAGE ID       CREATED              SIZE
ubuntu                latest    216c552ea5ba   11 days ago          77.8MB
hello-world           latest    feb5d9fea6a5   12 months ago        13.3kB

(4) Confirm CONTAINER ID

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                       PORTS     NAMES
45dfc14c1dc9   ubuntu        "bash"                   13 minutes ago   Exited (137) 5 minutes ago             crazy_curie
c1670baef172   hello-world   "/hello"                 59 minutes ago   Exited (0) 59 minutes ago              vigorous_nash

8. Run Docker container
(1) docker run

$ docker run -it ubuntu
root@ad90866c9f1b:/# 
root@ad90866c9f1b:/# exit

(2) check CONTAINER ID and NAMES

$ docker ps -l
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
45dfc14c1dc9   ubuntu    "bash"    7 minutes ago   Up 7 minutes             crazy_curie

9. How to save container state as new Docker image
(1) new Docker image

$ docker commit -m "added Node.js" -a "wsd01" 45dfc14c1dc9 wsd01/ubuntu-nodejs
sha256:f2930c27c1455fdcb3abd1b05547ae22724bc19bca32b24bb3f51ea0e36c0d10

10. Install docker-mariadb image
(1) Search image

$ docker search mariadb

(2) Download image to computer

$ docker pull mariacb

(3) Initirize a mariadb server instance & start
– name mariadb_1 (docker NAMES)
– MARIADB_USER=admin
– MARIADB_PASSWORD=123456
– MARIADB_ROOT_PASSWORD=12345678
– dp 3307:3306 ( Local side port number : Container side port number)

$ docker run --detach --name mariadb_1 --env MARIADB_USER=admin --env MARIADB_PASSWORD=123456 --env MARIADB_ROOT_PASSWORD=12345678  -dp 3307:3306 mariadb:latest

(4) check CONTAINER ID and NAMES
$ docker ps -a

CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS                     PORTS                                       NAMES
b83838cded47   mariadb:latest   "docker-entrypoint.s…"   32 minutes ago   Up 15 minutes              0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mariadb_1
45dfc14c1dc9   ubuntu           "bash"                   5 hours ago      Exited (137) 4 hours ago                                               crazy_curie
c1670baef172   hello-world      "/hello"                 5 hours ago      Exited (0) 5 hours ago                                                 vigorous_nash

(5) Start mariadb_1

$ docker start mariadb_1

(6) Show container list

$ docker container ls
CONTAINER ID   IMAGE            COMMAND                  CREATED        STATUS         PORTS                                       NAMES
b83838cded47   mariadb:latest   "docker-entrypoint.s…"   28 hours ago   Up 2 seconds   0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mariadb_1

(7) HeidiSQL session manager
– Session name:docker-mariadb_1
– user : admin
– port 3307
– Host : 127.0.0.1

(8) Connect mariadb_1

(9) Stop mariadb_1

$ docker stop mariadb_1

(10) Show container list

$ docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

(11) Container shell access and update and exit

$ docker start mariadb_1
$ docker exec -it mariadb_1 bash
root@700724a54424:/# apt-get update
root@700724a54424:/# exit

(13) Viewing MariaDB logs

$ docker logs mariadb_1

11. Mariadb permissions
(1) exec mariadb

$ docker exec -it mariadb_1 bash

(2) login mysql

root@700724a54424:/# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.9.3-MariaDB-1:10.9.3+maria~ubu2204 mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

(3)

MariaDB [(none)]>  create user 'admin'@'localhost' identified by '123456';

(4) grant admin

MariaDB [(none)]>  grant all privileges on *.* to 'admin'@'%' identified by '123456' WITH GRANT OPTION;

(5) show user

MariaDB [(none)]>  SELECT Host, User FROM mysql.user;
+-----------+-------------+
| Host      | User        |
+-----------+-------------+
| %         | admin       |
| %         | root        |
| localhost | admin       |
| localhost | mariadb.sys |
| localhost | root        |
+-----------+-------------+
5 rows in set (0.010 sec)

(4) exit

MariaDB [(none)]> Ctrl-C -- exit!
Aborted
root@b83838cded47:/# exit
$ 

12. pip install
(1) Force install pip

$ mkdir mariadb
$ cd  mariadb
$ python3 -m pip install mysql-connector-python
/usr/bin/python3: No module named pip
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 2509k  100 2509k    0     0  4950k      0 --:--:-- --:--:-- --:--:-- 4959k
/Mariadb$ sudo python3 get-pip.py --force-reinstall
Collecting pip
  Downloading pip-22.3-py3-none-any.whl (2.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 9.1 MB/s eta 0:00:00
Collecting setuptools
  Downloading setuptools-65.5.0-py3-none-any.whl (1.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 10.4 MB/s eta 0:00:00
Collecting wheel
  Downloading wheel-0.37.1-py2.py3-none-any.whl (35 kB)
Installing collected packages: wheel, setuptools, pip
Successfully installed pip-22.3 setuptools-65.5.0 wheel-0.37.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
$ python3 -m pip install mysql-connector-python
Defaulting to user installation because normal site-packages is not writeable
Collecting mysql-connector-python
  Downloading mysql_connector_python-8.0.31-cp310-cp310-manylinux1_x86_64.whl (23.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 23.5/23.5 MB 10.1 MB/s eta 0:00:00
Requirement already satisfied: protobuf<=3.20.1,>=3.11.0 in /usr/lib/python3/dist-packages (from mysql-connector-python) (3.12.4)
Installing collected packages: mysql-connector-python
Successfully installed mysql-connector-python-8.0.31
$ 

13. Create Data
(1) Create Data

$ docker exec -it mariadb_1 bash
# mysql -u root -p
MariaDB [(none)]> CREATE DATABASE sample;
MariaDB [(none)]> USE sample;
MariaDB [sample]> CREATE TABLE table_sample (id INT AUTO_INCREMENT NOT NULL PRIMARY KEY, name VARCHAR(10));
MariaDB [sample]> INSERT INTO table_sample (name) VALUES ('example_0'), ('example_1'), ('example_2');
MariaDB [sample]> SELECT * FROM table_sample;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | example_0  |
|  2 | example_1  |
|  3 | example_2  |
+----+-----------+
3 rows in set (0.001 sec)

MariaDB [sample]> exit
Bye
root@b83838cded47:/# exit
exit
$

(2) Confirm with HeidiSQL

14. Exec query
(1) connect.py

$ cat connect.py
import mysql.connector

con = mysql.connector.connect(db="sample",
                              host="localhost",
                              port=3307,
                              user="admin",
                              password="123456")
cur = con.cursor()
query = "SELECT id, name FROM table_sample"

cur.execute(query)
rows = cur.fetchall()
print(rows)
cur.close()
con.close()

(2) exec connect.py

$ python3 connect.py 
[(1, 'example_0'), (2, 'example_1'), (3, 'example_2')]