Docker in 5 minutes

I have been using Docker and Kubernetes for several years now but never really took any time to really read about it. There were a lot of gaps in my understanding of how Docker works and what really is possible with it. Just recently my employer started to offer Udemy’s subscription, so I took Hands on Docker course. Here are some of the important things I learned in this course.

Where do you run Docker containers

You run these in Windows or MacOS but most likely you will use Linux in production. And unlike Windows and MacOS, you can run Docker on Linux VPS.

Important Docker commands

# get running docker containers
docker ps 

# get all docker containers including those that have exited
docker ps -a

# stop container by its id or name
docker stop first_few_chars_of_id

# list docker images stored on the host
docker images

# remove image 
docker rmi image_name

# download a new image
docker pull image_name
docker pul image_name:specific_tag

# append a command
docker run image_name command to run with parameters
docker run ubuntu sleep 5

# execute command in a conatiner
docker exec container_id cat /etc/hosts

# interactive session inside container
docker exec -it conatiner_id /bin/bash

# run container in background/daemon mode
docker run -d webapp

# tail logs
docker logs -f conatiner_id

# attach to conatiner running in detach/daemon mode
docker attach container_id

Dallas Skyline at Night

Dallas Skyline at Night

Shot from Grand Hyatt.


Leetcode #13: Roman to Integers

class Solution {

    private $v = ['M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50, 'X' => 10, 'V' => 5, 'I' => 1];
    /**
     * @param String $s
     * @return Integer
     */
    function romanToInt($s) {
        $n = 0;
        $last = '';
        for ($i=0;$i<strlen($s); $i++) {
            $n += $this->v[$s[$i]];
            if (($s[$i] == 'V' || $s[$i] == 'X') && $last == 'I') { 
                $n = $n - 2;
            } elseif (($s[$i] == 'L' || $s[$i] == 'C') && $last == 'X') {
                $n -= 20;
            } elseif (($s[$i] == 'D' || $s[$i] == 'M') && $last == 'C') {
                $n -= 200;
            }
            $last = $s[$i];
        }
        
        return $n;
    }
}

Leetcode 690 Employee Importance Solution

This is my solution in PHP.

/**
* Definition for Employee.
* class Employee {
*     public $id = null;
*     public $importance = null;
*     public $subordinates = array();
*     function __construct($id, $importance, $subordinates) {
*         $this->id = $id;
*         $this->importance = $importance;
*         $this->subordinates = $subordinates;
*     }
* }
*/

class Solution {
/**
 * @param Employee[] $employees
 * @param Integer $id
 * @return Integer
 */

function getImportance($employees, $id) {
  if (empty($employees)) return 0;
  $emap = [];
  foreach ($employees as $e) {
    $emap[$e->id] = $e;
  }

  return $this->recurse($emap, $id);
 }

function recurse($emap, $id) {
  $e = $emap[$id];
  $ans = $e->importance;
  foreach ($e->subordinates as $sub) {
    $ans += $this->recurse($emap, $sub);
  }
  return $ans;
 }
}

LeetCode 42. Trapping Rain Water

/**
 * @param {number[]} height
 * @return {number}
 */
var trap = function(height) {
    let maxLeft = 0, maxRight = 0;

let left = 0;
let right = height.length -1;
let total = 0;

while (left < right) {
    if (height[left] < height[right]) {
        if (height[left]>maxLeft) {
            maxLeft = height[left];
        } else {
           total += maxLeft-height[left]
        }
        left++;
    } else {
        if (height[right]>maxRight) {
            maxRight = height[right];
        } else {
           total += maxRight-height[right]
        }
        right--;
    }    
}
return total;

};


LeetCode 11. Container With Most Water

/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function(height) {
    let left = 0;
    let right = height.length-1;
    let maxWater = 0;

while (left < right) {
    const water = (right-left) * Math.min(height[left], height[right]);

    if (water > maxWater) {
        maxWater = water;
    }

    if (height[left] < height[right]) {
        left++;
    } else {
        right--;
    }
}

return maxWater;

};


LeetCode 1. Two Sum

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    const numsToFind = {};
    
    for (let i=0; i<nums.length; i++) {
        const numToFind = nums[i];
        if (numsToFind[numToFind] >= 0) {
            return [i, numsToFind[numToFind]];
        }
        numsToFind[target - numToFind] = i;    
    }
};


LeetCode 1249. Minimum Remove to Make Valid Parentheses

/**
 * @param {string} s
 * @return {string}
 */
var minRemoveToMakeValid = function(s) {
    const stack = [];
    const charArray = s.split(“”);
    const rightBracketsToRemove = [];

for (let i=0;i<charArray.length;i++) {
    if (charArray[i] === '(') {
        stack.push(i);
    } else if (charArray[i] === ')' && stack.length > 0) {
        stack.pop();
    } else if (charArray[i] === ')') {
        charArray[i] = '';
    }
}

while (stack.length > 0) {
    const i = stack.pop();
    charArray[i] = '';
}

return charArray.join('');

};

Ref: https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/


LeetCode #20 Valid Parentheses

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    const stack = [];
    const brackets = {
        ‘(’: ‘)’,
        ‘{’: ‘}’,
        ‘[‘: ‘]’
    };

for (i=0; i<s.length; i++) {
    if (s.length === 0) return true;

    const c = s[i];
    if (brackets[c]) {
        stack.push(c);
    } else {
        const lastBracket = stack.pop(); 
        if (brackets[lastBracket] !== c) return false;
    }
}

if (stack.length > 0) return false;

return true;

};

Ref: https://leetcode.com/problems/valid-parentheses/


Brain is too tired to do any LeetCode tonight

Brain is too tired to do any LeetCode tonight. LeetCode challenges are fun but feel pointless. I don’t want to get burned out, so taking a break today.