Skip to content

Git & Docker Operations

Git

Git-related operations

Switch Branch

shell
# Check branches
git branch
# Switch to main branch
git checkout main
# Check branches
git branch
# Switch to main branch
git checkout main

Add SSH Key to Machine

shell
git config --global user.name 'dooy'
git config --global user.email 'dooy520@qq.com'
ssh-keygen -t rsa -C 'dooy520@qq.com'

cat ~/.ssh/id_rsa.pub # Get public key
git config --global user.name 'dooy'
git config --global user.email 'dooy520@qq.com'
ssh-keygen -t rsa -C 'dooy520@qq.com'

cat ~/.ssh/id_rsa.pub # Get public key

After getting the public key, add it to GitHub at https://github.com/settings/keys

shell
# Test after adding
ssh -T git@github.com
# Test after adding
ssh -T git@github.com

Token Access

https://github.com/settings/tokens?type=beta

Git SSH Acceleration

vim ~/.ssh/config

shell
Host github.com
    HostName 154.91.179.100
    User git
    Port 6088
Host github.com
    HostName 154.91.179.100
    User git
    Port 6088

Merge Fork

shell
git remote add upstream https://github.com/Test-2022/git-learn.git  # original repo URL
git remote -v
# --- do the above only once ---
git fetch upstream
git checkout main
git merge upstream/main
git remote add upstream https://github.com/Test-2022/git-learn.git  # original repo URL
git remote -v
# --- do the above only once ---
git fetch upstream
git checkout main
git merge upstream/main

Git Rollback

shell
# View history to find the version you want to roll back to
git log
# Roll back to the target version
git reset --hard <commit-hash>
# Push to remote
git push origin main --force
# View history to find the version you want to roll back to
git log
# Roll back to the target version
git reset --hard <commit-hash>
# Push to remote
git push origin main --force

Merge PR

https://zhuanlan.zhihu.com/p/456872464?utm_id=0

Docker

shell
docker commit  php  php56fpm:me
docker commit --change='ENTRYPOINT cd /nserver && npm run sk.server ' node18 nServer:1
docker commit  php  php56fpm:me
docker commit --change='ENTRYPOINT cd /nserver && npm run sk.server ' node18 nServer:1

Install Docker

shell
#!/bin/bash

# Step 1: Update apt package index
sudo apt-get update

# Step 2: Install required packages
sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

# Step 3: Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Step 4: Set up Docker stable repository
echo "deb [signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Step 5: Update apt package index
sudo apt-get update

# Step 6: Install Docker
sudo apt-get install -y docker-ce docker-ce-cli containerd.io

# Step 7: Verify Docker installation
docker --version

# Step 8: Add user to docker group (optional)
sudo usermod -aG docker $USER
echo "Please log out and log back in to apply the changes for Docker to run without sudo."
#!/bin/bash

# Step 1: Update apt package index
sudo apt-get update

# Step 2: Install required packages
sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

# Step 3: Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Step 4: Set up Docker stable repository
echo "deb [signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Step 5: Update apt package index
sudo apt-get update

# Step 6: Install Docker
sudo apt-get install -y docker-ce docker-ce-cli containerd.io

# Step 7: Verify Docker installation
docker --version

# Step 8: Add user to docker group (optional)
sudo usermod -aG docker $USER
echo "Please log out and log back in to apply the changes for Docker to run without sudo."

Tag a Local Image

shell
  # docker tag image1:latest (local image) ydlhero/pigaiphp:tagname (new tag)
  docker tag elastic:2.4 ydlhero/elastic:2.4
  docker push ydlhero/pigaiphp:5.6

  docker tag nserver:1 ydlhero/nserver:1
  docker tag engine7095:last ydlhero/engine7095:last

  docker commit  penly_service  penly:service01
  docker tag penly:service01 ydlhero/penly:service01
  docker push ydlhero/penly:service01
  # docker tag image1:latest (local image) ydlhero/pigaiphp:tagname (new tag)
  docker tag elastic:2.4 ydlhero/elastic:2.4
  docker push ydlhero/pigaiphp:5.6

  docker tag nserver:1 ydlhero/nserver:1
  docker tag engine7095:last ydlhero/engine7095:last

  docker commit  penly_service  penly:service01
  docker tag penly:service01 ydlhero/penly:service01
  docker push ydlhero/penly:service01

Export / Import Images

shell
  docker save ydlhero/pigaiphp:5.6 -o php5.6.docker.tag
  docker load -i php5.6.docker.tag

  # Start pigai php container
  docker run --name php -v /data/app:/data/app -p 9000:9000 -itd ydlhero/pigaiphp:5.6
  docker save ydlhero/pigaiphp:5.6 -o php5.6.docker.tag
  docker load -i php5.6.docker.tag

  # Start pigai php container
  docker run --name php -v /data/app:/data/app -p 9000:9000 -itd ydlhero/pigaiphp:5.6

Inspect Mount Directories

docker inspect xstroke --format='{{json .Mounts}}'
docker inspect xstroke --format='{{json .Mounts}}'

Docker Cleanup

shell
docker system prune  # Clear cache, will remove containers
docker image prune   # Remove unused images only
docker volume prune  # Clean up volumes
docker system prune  # Clear cache, will remove containers
docker image prune   # Remove unused images only
docker volume prune  # Clean up volumes

Delete Docker Container Logs

shell
echo "" > /var/lib/docker/containers/<container-id>/<container-id>-json.log
echo "" > /var/lib/docker/containers/<container-id>/<container-id>-json.log
  • Note: this breaks docker log --tail 100 <container-id>
  • Specify log options at startup instead:
shell
docker run \
  --log-driver json-file \
  --log-opt max-size=10m \    # max 10MB per log file
  --log-opt max-file=3 \      # keep at most 3 old log files
  your_image
docker run \
  --log-driver json-file \
  --log-opt max-size=10m \    # max 10MB per log file
  --log-opt max-file=3 \      # keep at most 3 old log files
  your_image

Install sunoproxy on Port 9999

shell
git clone https://github.com/Dooy/cockroachai.git
cd cockroachai
./docker-install.sh
cd sunoproxy
./deploy.sh
git clone https://github.com/Dooy/cockroachai.git
cd cockroachai
./docker-install.sh
cd sunoproxy
./deploy.sh

Install Homebrew

shell
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

MySQL

Clear bin-log

sql
-- Clear all logs before binlog.000010
PURGE BINARY LOGS TO 'binlog.000010';

-- Clear logs by date
PURGE BINARY LOGS BEFORE '2024-09-01 00:00:00';
-- Clear all logs before binlog.000010
PURGE BINARY LOGS TO 'binlog.000010';

-- Clear logs by date
PURGE BINARY LOGS BEFORE '2024-09-01 00:00:00';

Truncate Table Data

sql
TRUNCATE TABLE logs
TRUNCATE TABLE logs

VSCode Auto-Format on Save

To configure Prettier to auto-format on save in VSCode:

  1. Install Prettier extension — search "Prettier - Code formatter" in the marketplace.

  2. Enable format on save — open Settings (Ctrl+, / Cmd+,), search editor.formatOnSave and enable it.

  3. Set default formatter — search default formatter, set Editor: Default Formatter to esbenp.prettier-vscode.

  4. Prettier config — create .prettierrc in your project:

    json
    {
      "semi": true,
      "singleQuote": true,
      "tabWidth": 2,
      "trailingComma": "es5"
    }
    {
      "semi": true,
      "singleQuote": true,
      "tabWidth": 2,
      "trailingComma": "es5"
    }
  5. Workspace settings — create .vscode/settings.json:

    json
    {
      "editor.formatOnSave": true,
      "editor.defaultFormatter": "esbenp.prettier-vscode"
    }
    {
      "editor.formatOnSave": true,
      "editor.defaultFormatter": "esbenp.prettier-vscode"
    }

Useful Tools

  • Surge — powerful packet capture tool
  • AdsPower — fingerprint browser for daily work

Intercept Network Requests (New Approach)

Inject into a webpage and override XMLHttpRequest.prototype.send and fetch — suitable for browser extensions.

js
const origFetch = window.fetch;

window.fetch = async (...args) => {
  const [resource, config] = args;
  let requestUrl;
  if (typeof resource === "string") {
    requestUrl = resource;
  } else if (resource instanceof Request) {
    requestUrl = resource.url;
  }

  if (config.method != "POST" || !requestUrl.includes("/overview/home/asr")) {
    return origFetch(...args);
  }

  if (config && config.body instanceof FormData) {
    const entries = {};
    for (const [k, v] of config.body.entries()) {
      if (v instanceof File) {
        entries[k] = { name: v.name, size: v.size, type: v.type };
      } else {
        entries[k] = v;
      }
    }
    config.body = JSON.stringify(entries);
    config.headers["Content-Type"] = "application/json";
  }
  return origFetch(...args);
};
const origFetch = window.fetch;

window.fetch = async (...args) => {
  const [resource, config] = args;
  let requestUrl;
  if (typeof resource === "string") {
    requestUrl = resource;
  } else if (resource instanceof Request) {
    requestUrl = resource.url;
  }

  if (config.method != "POST" || !requestUrl.includes("/overview/home/asr")) {
    return origFetch(...args);
  }

  if (config && config.body instanceof FormData) {
    const entries = {};
    for (const [k, v] of config.body.entries()) {
      if (v instanceof File) {
        entries[k] = { name: v.name, size: v.size, type: v.type };
      } else {
        entries[k] = v;
      }
    }
    config.body = JSON.stringify(entries);
    config.headers["Content-Type"] = "application/json";
  }
  return origFetch(...args);
};

Main Site Recovery Process

Most outages are caused by MQ. Restart sequence:

shell
docker start mq php74 redis
cd /data/app/my-deploy/api
sudo docker-compose down
sudo ./ai.sh
sudo /usr/local/openresty/nginx/sbin/nginx
docker start mq php74 redis
cd /data/app/my-deploy/api
sudo docker-compose down
sudo ./ai.sh
sudo /usr/local/openresty/nginx/sbin/nginx