Build Docker Images with dockerfile

On this article, will see how to build docker images from dockerfile, for example i will create docker images who run pxc-57 with timezone Asia/Jakata

Steps :

create directory for place of dockerfile

# mkdir mydocker_build

change to dir mydocker_build and create new file with name Dockerfile

# cd mydocker_build
# vi Dockerfile

Content of dockerfile :

FROM debian:8.5
LABEL maintainer "erick_s"
RUN apt-get update
RUN apt-get install -y wget vim locales net-tools
RUN locale-gen en_US.UTF-8
RUN localedef -i en_US -f UTF-8 en_US.UTF-8
ENV DEBIAN_FRONTEND noninteractive
RUN wget https://repo.percona.com/apt/percona-release_0.1-4.jessie_all.deb
RUN dpkg -i percona-release_0.1-4.jessie_all.deb
RUN apt-get update
RUN apt-get install -y percona-xtrabackup-24 percona-xtradb-cluster-57 percona-xtradb-cluster-client-5.7 percona-xtradb-cluster-common-5.7 percona-xtradb-cluster-server-5.7 psmisc rsync socat tcpd libperconaserverclient20-dev 
RUN apt-get install -y python-mysqldb
RUN apt-get autoclean
RUN service mysql stop
RUN rm -rf /var/lib/apt/lists/*
RUN rm -rf /var/lib/mysql/*
RUN mv /etc/mysql/my.cnf /var/lib/mysql/my.cnf.old
RUN echo Asia/Jakarta > /etc/timezone
RUN ln -sf /usr/share/zoneinfo/Asia/Jakarta /etc/localtime
RUN dpkg-reconfigure -f noninteractive tzdata
CMD ["/bin/bash"]

Next run docker build to building docker images

# docker build -t debian .

after all process finish, you can see new docker images. run docker images to see it on the top

# docker images 

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
debian              latest              4d2ba797cc5b        10 seconds ago      614.4 MB

Commit docker images

To commit your new docker images, run your docker images for something like this

# docker run -itd --name=mysql-57 --hostname=dbnode 4d2ba797cc5b /bin/bash
e33b0de986d442018f509e3f458cc1467c02840a66a2277d1e04881fe4b7d633

Show active container

# docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
e33b0de986d4        4d2ba797cc5b        "/bin/bash"         2 minutes ago       Up 2 minutes                                 mysql-57

Lets to commit you docker images

# docker commit e33b0de986d4 zsdiary/percona57 
# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
zsdiary/percona57   latest              2ca746b405bb        19 seconds ago      614.4 MB

Tag your docker images

# docker tag 2ca746b405bb zsdiary/percona57:5.7

Show your docker images to see changes

# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
zsdiary/percona57   5.7                 2ca746b405bb        About a minute ago   614.4 MB
zsdiary/percona57   latest              2ca746b405bb        About a minute ago   614.4 MB

Yey… docker images is ready to use

Push your docker images to dockerhub

If you want to save your docker images, docker have place for it. you can use dockerhub to push your docker images, for example i will push my docker images to my dockerhub, before it you must login and create repository on dockerhub

# docker login --username=ericks --email=erick.1383@gmail.com

Push the docker images

# docker push zsdiary/percona57:5.7

Finish, hope this article will help you

Thank you

Advertisements

MySQL Log rotate

Berikut adalah cara untuk melakukan logrotate harian terhadap log yang ada pada mysql

Langkah-langkah :

Buat user baru di database mysql yang nantinya akan menjalankan perintah flush logs

mysql> CREATE USER 'log'@'localhost' IDENTIFIED BY 'Passw0rd';
mysql> GRANT RELOAD, SHUTDOWN, PROCESS, SHOW DATABASES, SUPER, LOCK TABLES ON *.* TO 'log'@'localhost'; 
mysql> flush privileges;

Buatlah file debian.cnf dan isikan baris berikut

 
# vi /etc/mysql/debian.cnf

[client]
host     = localhost
user     = log
password = passw0rd
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
user     = log
password = passw0rd
socket   = /var/run/mysqld/mysqld.sock
basedir  = /usr

Selanjutnya pada direktori /etc/logrotate.d buatlah file dengan nama mysql, namun jika sebelumnya sudah ada maka edit saja file sesuaikan nama log yang ingin di rotate

# vi /etc/logrotate.d/mysql

/var/log/mysql/mysql-error.log /var/log/mysql/mysql-slow.log {
	daily
	rotate 5
	missingok
	create 640 mysql adm
	compress
	sharedscripts
	postrotate
		test -x /usr/bin/mysqladmin || exit 0

		# If this fails, check debian.conf! 
		MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
		if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then
		  # Really no mysqld or rather a missing debian-sys-maint user?
		  # If this occurs and is not a error please report a bug.
		  if ps cax | grep -q mysqld; then
		    exit 1
		  fi 
		else
		  $MYADMIN flush-logs
		fi
	endscript
}

Script tersebut akan dijalankan oleh cron daily setiap malam, namun jika ingin mengetahui apa script tersebut berjalan dengan benar atau tidak, bisa menggunakan perintah berikut

# logrotate -v -f /etc/logrotate.d/mysql

reading config file /etc/logrotate.d/mysql

Handling 1 logs

rotating pattern: /var/log/mysql.log /var/log/mysql/mysql-error.log /var/log/mysql/mysql-slow.log  forced from command line (7 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/mysql.log
  log /var/log/mysql.log does not exist -- skipping
considering log /var/log/mysql/mysql-error.log
  log needs rotating
considering log /var/log/mysql/mysql-slow.log
  log needs rotating
rotating log /var/log/mysql/mysql-error.log, log->rotateCount is 7
dateext suffix '-20170510'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/mysql/mysql-error.log.7.gz to /var/log/mysql/mysql-error.log.8.gz (rotatecount 7, logstart 1, i 7), 
old log /var/log/mysql/mysql-error.log.7.gz does not exist
renaming /var/log/mysql/mysql-error.log.6.gz to /var/log/mysql/mysql-error.log.7.gz (rotatecount 7, logstart 1, i 6), 
old log /var/log/mysql/mysql-error.log.6.gz does not exist
renaming /var/log/mysql/mysql-error.log.5.gz to /var/log/mysql/mysql-error.log.6.gz (rotatecount 7, logstart 1, i 5), 
old log /var/log/mysql/mysql-error.log.5.gz does not exist
renaming /var/log/mysql/mysql-error.log.4.gz to /var/log/mysql/mysql-error.log.5.gz (rotatecount 7, logstart 1, i 4), 
old log /var/log/mysql/mysql-error.log.4.gz does not exist
renaming /var/log/mysql/mysql-error.log.3.gz to /var/log/mysql/mysql-error.log.4.gz (rotatecount 7, logstart 1, i 3), 
old log /var/log/mysql/mysql-error.log.3.gz does not exist
renaming /var/log/mysql/mysql-error.log.2.gz to /var/log/mysql/mysql-error.log.3.gz (rotatecount 7, logstart 1, i 2), 
old log /var/log/mysql/mysql-error.log.2.gz does not exist
renaming /var/log/mysql/mysql-error.log.1.gz to /var/log/mysql/mysql-error.log.2.gz (rotatecount 7, logstart 1, i 1), 
old log /var/log/mysql/mysql-error.log.1.gz does not exist
renaming /var/log/mysql/mysql-error.log.0.gz to /var/log/mysql/mysql-error.log.1.gz (rotatecount 7, logstart 1, i 0), 
old log /var/log/mysql/mysql-error.log.0.gz does not exist
log /var/log/mysql/mysql-error.log.8.gz doesn't exist -- won't try to dispose of it
rotating log /var/log/mysql/mysql-slow.log, log->rotateCount is 7
dateext suffix '-20170510'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/mysql/mysql-slow.log.7.gz to /var/log/mysql/mysql-slow.log.8.gz (rotatecount 7, logstart 1, i 7), 
old log /var/log/mysql/mysql-slow.log.7.gz does not exist
renaming /var/log/mysql/mysql-slow.log.6.gz to /var/log/mysql/mysql-slow.log.7.gz (rotatecount 7, logstart 1, i 6), 
old log /var/log/mysql/mysql-slow.log.6.gz does not exist
renaming /var/log/mysql/mysql-slow.log.5.gz to /var/log/mysql/mysql-slow.log.6.gz (rotatecount 7, logstart 1, i 5), 
old log /var/log/mysql/mysql-slow.log.5.gz does not exist
renaming /var/log/mysql/mysql-slow.log.4.gz to /var/log/mysql/mysql-slow.log.5.gz (rotatecount 7, logstart 1, i 4), 
old log /var/log/mysql/mysql-slow.log.4.gz does not exist
renaming /var/log/mysql/mysql-slow.log.3.gz to /var/log/mysql/mysql-slow.log.4.gz (rotatecount 7, logstart 1, i 3), 
old log /var/log/mysql/mysql-slow.log.3.gz does not exist
renaming /var/log/mysql/mysql-slow.log.2.gz to /var/log/mysql/mysql-slow.log.3.gz (rotatecount 7, logstart 1, i 2), 
old log /var/log/mysql/mysql-slow.log.2.gz does not exist
renaming /var/log/mysql/mysql-slow.log.1.gz to /var/log/mysql/mysql-slow.log.2.gz (rotatecount 7, logstart 1, i 1), 
old log /var/log/mysql/mysql-slow.log.1.gz does not exist
renaming /var/log/mysql/mysql-slow.log.0.gz to /var/log/mysql/mysql-slow.log.1.gz (rotatecount 7, logstart 1, i 0), 
old log /var/log/mysql/mysql-slow.log.0.gz does not exist
log /var/log/mysql/mysql-slow.log.8.gz doesn't exist -- won't try to dispose of it
renaming /var/log/mysql/mysql-error.log to /var/log/mysql/mysql-error.log.1
creating new /var/log/mysql/mysql-error.log mode = 0640 uid = 110 gid = 4
renaming /var/log/mysql/mysql-slow.log to /var/log/mysql/mysql-slow.log.1
creating new /var/log/mysql/mysql-slow.log mode = 0640 uid = 110 gid = 4
running postrotate script
compressing log with: /bin/gzip
compressing log with: /bin/gzip

cek hasil logrotate

# ls -al /var/log/mysql

-rw-r-----  1 mysql adm     10277 Nov  2  2016 error.log
-rw-r-----  1 mysql adm         0 May 10 11:41 mysql-error.log
-rw-r-----  1 mysql root  3970514 May 10 10:44 mysql-error.log.1.gz
-rw-r-----  1 mysql adm    245054 May 10 11:46 mysql-slow.log
-rw-r-----  1 mysql adm        20 May 10 06:25 mysql-slow.log.1.gz