Dockerize MySql and Mariadb using docker compose

Robioki Denis
Sep 23, 2021

--

Photo by Caspar Camille Rubin on Unsplash

Hello friend, in this article we gonna learn how to initialize mysql database in docker container using docker compose.

make sure you was installed docker before, check using command

$docker -v
Docker version 20.10.8, build 3967b7d

Create directory to store docker-compose file

$mkdir databases/mysql
$cd databases/mysql

Creating docker compose yml file

$touch docker-compose.yml

Open docker-compose file

$nano docker-compose.yml

Edit docker-compose file

version: '3.1'
services:
db:
image: mysql
container_name: mydb
command: --default-authentication-plugin=mysql_native_password
restart: always
volumes:
- ./data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_USER: user1
MYSQL_PASSWORD : mypassword
ports:
- 3306:3306

Change

  • root password : rotpassword
  • username user : user1
  • password user : mypassword
  • ports : default 3306

Lets build container

docker-compose up -d

Check container using command

$docker-compose psmydb   docker-entrypoint.sh --def ...   Up   0.0.0.0:3306 - 3306/tcp,:::3306->3306/tcp, 33060/tcp

Testing Client …

That’s it….

--

--

Robioki Denis

DevOps Engineer & Fullstack Developer, someone who has a high curiosity 😁