\( \newcommand{\N}{\mathbb{N}} \newcommand{\R}{\mathbb{R}} \newcommand{\C}{\mathbb{C}} \newcommand{\Q}{\mathbb{Q}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\P}{\mathcal P} \newcommand{\B}{\mathcal B} \newcommand{\F}{\mathbb{F}} \newcommand{\E}{\mathcal E} \newcommand{\brac}[1]{\left(#1\right)} \newcommand{\abs}[1]{\left|#1\right|} \newcommand{\matrixx}[1]{\begin{bmatrix}#1\end {bmatrix}} \newcommand{\vmatrixx}[1]{\begin{vmatrix} #1\end{vmatrix}} \newcommand{\lims}{\mathop{\overline{\lim}}} \newcommand{\limi}{\mathop{\underline{\lim}}} \newcommand{\limn}{\lim_{n\to\infty}} \newcommand{\limsn}{\lims_{n\to\infty}} \newcommand{\limin}{\limi_{n\to\infty}} \newcommand{\nul}{\mathop{\mathrm{Nul}}} \newcommand{\col}{\mathop{\mathrm{Col}}} \newcommand{\rank}{\mathop{\mathrm{Rank}}} \newcommand{\dis}{\displaystyle} \newcommand{\spann}{\mathop{\mathrm{span}}} \newcommand{\range}{\mathop{\mathrm{range}}} \newcommand{\inner}[1]{\langle #1 \rangle} \newcommand{\innerr}[1]{\left\langle #1 \right \rangle} \newcommand{\ol}[1]{\overline{#1}} \newcommand{\toto}{\rightrightarrows} \newcommand{\upto}{\nearrow} \newcommand{\downto}{\searrow} \newcommand{\qed}{\quad \blacksquare} \newcommand{\tr}{\mathop{\mathrm{tr}}} \newcommand{\bm}{\boldsymbol} \newcommand{\cupp}{\bigcup} \newcommand{\capp}{\bigcap} \newcommand{\sqcupp}{\bigsqcup} \newcommand{\re}{\mathop{\mathrm{Re}}} \newcommand{\im}{\mathop{\mathrm{Im}}} \newcommand{\comma}{\text{,}} \newcommand{\foot}{\text{。}} \)

Thursday, May 28, 2020

Redux Setup that also applies to React-Native

import { createStore, combineReducers } from "redux";
import trackFormReducer from "./reducers/trackFormReducer";
const rootReducer = combineReducers({
  trackFormReducer: trackFormReducer
});

const store = createStore(
  rootReducer /* preloadedState, */,
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);

export default store;
and
import { Provider as StoreProvider } from "react-redux";
and wrap the app component by StoreProvider

Wednesday, May 20, 2020

Docker Exercise: docker-compose

The following create drupal service with persistent named datas: (docker-compose.yml)
version: "2"

services:
  drupal:
    image: drupal
    ports:
      - "8080:80"
    volumes:
      - drupal-modules:/var/www/html/modules
      - drupal-profiles:/var/www/html/profiles
      - drupal-sites:/var/www/html/sites
      - drupal-themes:/var/www/html/themes
  postgres:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=1234

volumes:
  drupal-modules:
  drupal-profiles:
  drupal-sites:
  drupal-themes:
The options can be found in the official docker page from hub.docker.com. cd into the directory that contains the above docker-compose.yml and run docker-compose up.

When we are done, we use docker-compose down -v to remove everything.

Note that the service name is implicitly also the hostname of the service. For instance, when we try to connect to postgres database inside the network from one of the container (for example, our drupal service), the hostname is can be put as postgres.

Another example of docker-compose:
version: "2"

services:
  proxy:
    build:
      context: .
      dockerfile: nginx.Dockerfile
    image: nginx-custom
    ports:
      - "80:80"
  web:
    image: httpd
    volumes:
      - ./html:/usr/local/apache2/htdocs/

If image is not found, it will run the build command and tag it with the image name. There are at least two ways to customize what to build.
  1. use COPY in Dockerfile
  2. use volumes in docker-compose.yml.

Docker Exercise

FROM node:14.2.0-alpine3.10

EXPOSE 3000

RUN apk add --update tini

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package.json package.json

RUN npm install

COPY . .

CMD ["tini", "--", "node", "./bin/www"]
cd into the directory that contains the above as Dockerfile, and run
docker build -t testnode
The above will be built as an image and tagged with testnode. After we make sure testnode can be run properly (by running docker container run --rm -p 80:3000 testnode), we change the tag name as follows:
docker tag testnode machingclee/testnode
and then
docker push machingclee/testnode
if we want.

Tuesday, May 12, 2020

Standard docker command

The following create a new container that redirect traffic to our host 192.168.99.100 at port 3306 into port 3306 of the running container.
docker container run -d -p 3306:3306 --name db -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
To test linux, we can run another container that run an image of minimal version of ubuntu as follow:
docker container run -it --name ubuntu ubuntu 
Here -it (i.e., -i and -t) allows us to kind of SSH into the container and get the shell ready. By typing
exit
inside the shell we return to our docker prompt. To go back to our shell inside ubuntu container, we run
docker container start -ai ubuntu

Friday, May 8, 2020

Bash Script to Batch Renaming

The following replaces "abcd_mixdown.mp3" to "abcd_vo.mp3" for every mp3 file of the current directory.
for mp3 in *.mp3; do renamedmp3=$(echo "$mp3" | sed 's/_mixdown.mp3$/.mp3/'); mv ./"$mp3" ./"$renamedmp3; done"
The next one goes even further, it does the same thing with all current and subdirectory:
#!/bin/bash

list=$(find -name '*.mp3' | grep '_mixdown\.mp3');

for mp3FilePath in $list
do
    newMp3FilePath=$(echo "$mp3FilePath" | sed 's/_mixdown\.mp3/_vo\.mp3/')
    mv $mp3FilePath $newMp3FilePath
done