How-to get size of some folder

du -sh /var
du -sh mysql/*
du -sh  .

Docker if you need to edit file on stopped docker

I had a problem with a container which wouldn’t start due to a bad config change I made. I was able to copy the file out of the stopped container and edit it. something like:

docker cp test-mysql:/etc/mysql/my.cnf .

Edit it and copy back

docker cp my.cnf test-mysql:/etc/mysql/my.cnf
docker start mysql
docker exec -ti mysql bash

Git reset local master to match remote master

git reset --hard origin/master

Build and run container from Dockerfile

Step 1 – Dockerfile

FROM debian:jessie
MAINTAINER user@server.com
RUN apt-get update
RUN apt-get install -y vim
RUN apt-get install -y apache2
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
RUN apt-get install -y php5
RUN apt-get install php5-mysql
RUN apt-get autoremove
 
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
 
EXPOSE 80
# Launch all services
# CMD ["apache2ctl", "-D", "FOREGROUND"]
COPY startup.sh /
RUN chmod 777 /startup.sh
CMD ["bash","/startup.sh"]

Step 2 – Create startup.sh

cat > startup.sh
#!/bin/bash
 
apache2ctl -D FOREGROUND

Step 3 – Building image

docker build -t aghayevcomdockerfile:latest .

Step 4 – Check if image was created

REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
aghayevcomdockerfile   latest          19e27a7ca24b        11 minutes ago      389MB
debian                 jessie              5d7d9c6338e8        3 weeks ago         129MB

Step 5 – Creating container

docker run -d -t -p 80:80 -p 443:443 -p 3306:3306 -v /home/user/Projects:/home -v /home/user/Servers:/opt --name aghayevcom aghayevcomdockerfile:latest

Step 6 – Check if container created

CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                NAMES
8946f43f47ea        aghayevcomdockerfile:version1.0   "bash /startup.sh"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   aghayevcom
 
p.s. Pay attention to COMMAND column, it should show which command will run first when u start container. In case of direct call of apache2ctl it shows this
#850703e85940        aghayevcomdockerfile:version1.0   "apache2ctl -D FOREG…"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   aghayevcom

Step 6 – Run container

docker start aghayevcom
 
p.s. It is possible to start container by direct calling Container ID
# docker start 8946f43f47ea

How to mount and unmount remote directory

Mounting

mkdir ~/remoteDir
ssh-keygen -t rsa
ssh-copy-id -i .ssh/id_rsa.pub user@hostname
sudo apt install sshfs
sshfs user@hostname:/ ~/remoteDir

Unmounting

fusermount -u ~/remoteDir

Apt sources.list to add or remove deb installation repositories

vi /etc/apt/sources.list

Simple php based mysql query script

<?php
 
$dbname = 'dbname';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'hostname';
 
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");
$test_query = "SELECT 1,2,3,4 FROM yourtable";
$result = mysql_query($test_query);
$tblCnt = 0;
while($tbl = mysql_fetch_array($result)) {
  $tblCnt++;
  echo $tbl[0]."<br />\n";
  echo $tbl[1]."<br />\n";
  echo $tbl[2]."<br />\n";
  echo $tbl[3]."<br />\n";
}
 
if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
}

How-to detach running process from terminal

If your job is running in terminal do

bg -- Move running process to background --
jobs -- list the jobs in background and get id of the job you want to detach --
disown %1 -- use job id from previous query for detaching from the terminal --

Mysql show full list of processes in memory

SHOW FULL PROCESSLIST;

Php Nearest Match

function getNearestMatch($number, $candidates) {
 
	$candidates = is_array($candidates) ? $candidates : array($candidates);
 
	$size = count($candidates);
	if ($size > 0) {
		//absolute difference between 0th element and input value
		$diff = abs($candidates[0] - $number);
		//element closest to input value
		$ret = $candidates[0];
		//loop through the rest of the array
		for ($i = 1; $i < $size; $i++) {
			$temp = abs($candidates[$i] - $number);
			if ($temp < $diff) {
				//set new difference and closest element
				$diff = $temp;
				$ret = $candidates[$i];
			}
		}
		return $ret;
	} else {
		return NULL;
	}
 
    }