Tuesday, March 17, 2020

7. DOCKER & KUBERNETES : Create Image of MYSQL DB and Create tables in it

7. DOCKER & KUBERNETES : Create Image of MYSQL DB and Create tables in it

Requirement: Create Image of MYSQL DB and create table and insert few records init. ##################
Step1: Keep ready one createdb.sql (creates the table and insert one row)
Step2: Create docker file and use createdb.sql init
Step3: Create Image using dockerfile
Step4: Run and test the Image
########################################################################
----------------------MYSQL in hub.docker.com---------------------------------------------
//Will use mysql image

docker pull mysql:5.7

docker images  //mysql is 456MB

docker inspect mysql:5.7  //ExposedPort 3306 port, CMD : mysqld,  volume : 

//In container we cannot modify in running container is
1. Docker image associated
2  ID - Readonly
3. Volume (data inside volume can change, but cannot add volume - Readonly, means during container creation if volume is assigned then later addition is not possible
4. Cannot increase decrease ports - Readonly

//What cannot change in Docker image
//Entire Docker image is readonly

//Overwrite and image is possible
//Overwrite a container is not possible (overright - ID, Volume, Porsts, DockerImage not possible)

//we will mount some thing to 
https://github.com/dhanan77/RESTAPI22.05/blob/master/createdb.sql

Step1: Keep ready one createdb.sql (creates the table and insert one row)
vi createdb.sql

//paste below code
####################################################
CREATE DATABASE IF NOT EXISTS `pythonlogin` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `pythonlogin`;

CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(100) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `accounts` (`id`, `username`, `password`, `email`) VALUES (1, 'test', 'test', 'test@test.com');

####################################################
Esc :wq!


ls

Step2: Create docker file and use createdb.sql init
vi Dockerfile2
####################################################
#mysql 5.7 Implementation of Custom DB created.
FROM mysql:5.7
label maintainer dj@dba-ops.com
ENV MYSQL_ROOT_PASSWORD=admin
COPY createdb.sql /docker-entrypoint-initdb.d/createdb.sql
####################################################
Esc :wq!

Step3: Create Image using dockerfile
//newmysql is image name
docker build -t newmysql -f Dockerfile2 .

vi .dockerignore 

      *.tar  //add this

Esc :wq!

ls -a  //should see .dockerignore

cat .dockerignore

Step4: Run and test the Image "newmysql" by creating the container "testdb"
docker run --name testdb -d newmysql  //it will give id

//check whether "testdb" container is running or not
docker ps -a | grep testdb

//verify db is ready to for connection , means sql file given is executed or not
docker logs testdb  //it will show port and othr information.. and we see in the log createdb.sql is executed..


No comments:

Post a Comment