当前位置:   article > 正文

[docker]-wsl使用docker部分小技巧_wsl内使用docker

wsl内使用docker

导语:汇总一下在wsl中扩容根路径大小 ,在局域网暴露端口,获取磁盘uuid

扩容/路径的大小

powershell中执行

wsl --shutdown
diskpart.exe
  • 1
  • 2

因为我是用发行版安装的 直接扩容E:\ubuntu18_04-disk\ext4.vhdx

 Select vdisk file="E:\ubuntu18_04-disk\ext4.vhdx"
  detail vdisk
   expand vdisk maximum=512000
 detail vdisk
   exit
  • 1
  • 2
  • 3
  • 4
  • 5

powershell

wsl.exe
sudo mount -t devtmpfs none /dev
mount | grep ext4
# 可以通过lsblk看到sdb和sda都是挂到/的  所以扩容sdb就是扩容/,sda不支持ext4
sudo resize2fs /dev/sdb 512000M
  • 1
  • 2
  • 3
  • 4
  • 5

参考

https://docs.microsoft.com/zh-cn/windows/wsl/vhd-size

获取uuid

因为交付的实际需要一个唯一的uuid 原先是使用/sys/class/dmi/id/product_uuid,但是在wsl中没有这个文件。

更换为

blkid  /dev/sda1
  • 1

容器运行时需要特权模式 --privileged=true 否则就算通过挂载的方式 获取到的也为空值。

暴露端口

方法1

以管理员在powershell中执行

netsh interface ip add address "vEthernet (WSL)" 172.16.10.174 255.255.255.0
wsl -d  Ubuntu-18.04 -u root ip addr add 172.16.10.174/24 broadcast 172.16.10.255 dev eth0 label eth0:1
  • 1
  • 2

这样执行后的确是绑定了对应的ip,但是在外网无法ping通,也不能通过对应ip的端口号访问docker中的服务。仅仅只能在docker中ping的通而已,不知道是不是我操作的问题。

参考

https://blog.csdn.net/weixin_32300981/article/details/112651567

方法二推荐

以管理员在powershell中执行。不要关闭防火墙,否则会报错

和资料中的稍有改动,因为我用原来的命令获取winip和wslip,发现格式不对。自己调整了一下。大佬完整的脚本贴在最后了,我没测试。这个重启后wsl的ip会变,可以尝试固定wsl的ip,这样不用每次开机都执行。

# 获取 Windows 和 WSL2 的 ip

# $winip = bash.exe -c "ip route | grep default | awk '{print `$3}'"
# default via 172.20.16.1 dev eth0
$winip = bash.exe -c "ip route | grep default |cut -d ' ' -f 3"
# $wslip = bash.exe -c "hostname -I | awk '{print `$1}'"
# 172.20.30.87 172.16.10.174 172.18.0.1 172.17.0.1
$wslip = bash.exe -c "hostname -I |cut -d ' '  -f 1 "
$found1 = $winip -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
$found2 = $wslip -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if( !($found1 -and $found2) ){
  # 如果没找到 wsl 的 ip, 就退出执行
  echo "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

# 你需要映射到局域网中端口
$ports=@(80,81,22222,22,800,11380,11381);

# 监听的 ip,这么写是可以来自局域网
$addr='0.0.0.0';
# 监听的端口,就是谁来访问自己
$ports_a = $ports -join ",";

# 移除旧的防火墙规则
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' " | Out-Null

# 允许防火墙规则通过这些端口
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP"  | Out-Null
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP"  | Out-Null

# 使用 portproxy 让 Windows 转发端口
# https://docs.microsoft.com/en-us/windows-server/networking/technologies/netsh/netsh-interface-portproxy
for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr"  | Out-Null
  iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$wslip"  | Out-Null
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

先安装一个ssh

# service ssh status 可以看到ssh服务并不存在  netstat -luntp 也看不到22端口或者其他22端口
apt update
apt install openssh-server
# 如有需要则修改 /etc/ssh/sshd_config中的Port 22 
# 如果需要使用root登录需要修改/etc/ssh/sshd_config 修改PermitRootLogin prohibit-password 为 PermitRootLogin yes
service ssh start
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

测试连接

修改配置后用root登录

意外情况 174也可以访问

在windows外部的其他局域网中通过windows ip可以访问,但是额外加的ip 174不可以

typeset -U PATH
export EDITOR=nvim
export TIME_STYLE='long-iso'
export CC=gcc-9
export GPG_TTY=$(tty)

if type brew &>/dev/null; then
      FPATH=$(brew --prefix)/share/zsh/site-functions:$FPATH
fi

# alias
alias l="exa -la"
alias ls="exa"
alias la="exa -lah"
alias b=brew
alias vim="nvim"
alias y=yarn
alias py="python3"
alias ipy="ipython"
alias ohmyzsh="cd ~/.oh-my-zsh"
alias vizsh="vim ~/.zshrc"
alias c="code-insiders"
alias e="explorer.exe"
alias q="cd $HOME && clear"
alias cdtmp='cd `mktemp -d /tmp/artin-XXXXXX`'
alias ws="cd ~/0Workspace"
alias le="cd ~/0learn-everything"
alias udtheme="cp -r ~/dotfiles/zsh-theme/. ~/.oh-my-zsh/custom/themes/"
alias udwsl='powershell.exe "sudo powershell -ExecutionPolicy ByPass -File ~\dotfiles\windows\wsl2.ps1"'
alias cls=clear
alias rmrf="rm -rf"
alias srmrf="sudo rm -rf"
alias vimrc="vim ~/.config/nvim/init.vim"
alias ping="nali-ping"
alias dig="nali-dig"
alias d=docker
alias lzd=lazydocker
alias dco="docker-compose"
alias traceroute="nali-traceroute"
alias tracepath="nali-tracepath"
alias nslookup="nali-nslookup"
alias shutdown="wsl.exe --shutdown"
alias clip="clip.exe"
alias apt-update="sudo apt-get update && sudo apt-get -y upgrade"
alias ncdux="ncdu -X ~/dotfiles/_rc/ncduignore"
alias lg=lazygit
alias top=htop
alias fd=fdfind
alias g=git
alias tk=take
alias cg=cargo
alias gcid="git log | head -1 | awk '{print substr(\$2,1,7)}'"
alias src="source ~/.zshrc"

alias -s gz='tar -xzvf'
alias -s tgz='tar -xzvf'
alias -s zip='unzip'
alias -s bz2='tar -xjvf'

# Enable aliases to be sudo’ed
# http://askubuntu.com/questions/22037/aliases-not-available-when-using-sudo
alias _='sudo '

# Avoid stupidity with trash-cli:
# https://github.com/sindresorhus/trash-cli
# or use default rm -i
if (( $+commands[trash] )); then
  alias rm='trash'
else
  alias rm='rm -i'
fi

if [ -d "$HOME/bin" ] ; then
    export PATH="$HOME/bin:$PATH"
fi

if [ -d "$HOME/.local/bin" ] ; then
    export PATH="$HOME/.local/bin:$PATH"
fi

eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)

export PATH="~/.npm-global/bin:$PATH"
export PATH="$HOME/.poetry/bin:$PATH"
export PATH="$HOME/.cargo/bin:$PATH"
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

setopt no_nomatch

# Proxy configuration
getIp() {
    export winip=$(ip route | grep default | awk '{print $3}')
    export wslip=$(hostname -I | awk '{print $1}')
    export PROXY_SOCKS5="socks5://${winip}:7890"
    export PROXY_HTTP="http://${winip}:7890"
}

proxy_git() {
    ssh_proxy="${winip}:7890"
    git config --global http.https://github.com.proxy ${PROXY_HTTP}
    if ! grep -qF "Host github.com" ~/.ssh/config ; then
        echo "Host github.com" >> ~/.ssh/config
        echo "    User git" >> ~/.ssh/config
        echo "    ProxyCommand nc -X 5 -x ${ssh_proxy} %h %p" >> ~/.ssh/config
    else
        lino=$(($(awk '/Host github.com/{print NR}'  ~/.ssh/config)+2))
        sed -i "${lino}c\    ProxyCommand nc -X 5 -x ${ssh_proxy} %h %p" ~/.ssh/config
    fi
}

winip_() {
    getIp
    echo ${winip}
}

wslip_() {
    getIp
    echo ${wslip}
}

x11() {
    getIp
    if [ ! $1 ]; then
        # null
        export DISPLAY=${winip}:0.0
    else
        export DISPLAY=${winip}:$1.0
    fi
    echo $DISPLAY
    export XDG_SESSION_TYPE=x11
    export XDG_RUNTIME_DIR=/tmp/runtime-root
    export LIBGL_ALWAYS_INDIRECT=1
    export PULSE_SERVER=tcp:$winip
}

ip_() {
    getIp
    https --follow -b https://api.ip.sb/geoip/$1
    echo "WIN ip: ${winip}"
    echo "WSL ip: ${wslip}"
}

proxy_npm() {
    getIp
    npm config set proxy ${PROXY_HTTP}
    npm config set https-proxy ${PROXY_HTTP}
    yarn config set proxy ${PROXY_HTTP}
    yarn config set https-proxy ${PROXY_HTTP}
}

unpro_npm() {
    npm config delete proxy
    npm config delete https-proxy
    yarn config delete proxy
    yarn config delete https-proxy
}

proxy() {
    getIp
    # pip can read http_proxy & https_proxy
    export http_proxy="${PROXY_HTTP}"
    export HTTP_PROXY="${PROXY_HTTP}"
    export https_proxy="${PROXY_HTTP}"
    export HTTPS_PROXY="${PROXY_HTTP}"
    export ftp_proxy="${PROXY_HTTP}"
    export FTP_PROXY="${PROXY_HTTP}"
    export rsync_proxy="${PROXY_HTTP}"
    export RSYNC_PROXY="${PROXY_HTTP}"
    export ALL_PROXY="${PROXY_SOCKS5}"
    export all_proxy="${PROXY_SOCKS5}"
    proxy_git
    if [ ! $1 ]; then
        ip_
    fi
    echo "Acquire::http::Proxy \"${PROXY_HTTP}\";" | sudo tee /etc/apt/apt.conf.d/proxy.conf >/dev/null 2>&1
    echo "Acquire::https::Proxy \"${PROXY_HTTP}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf >/dev/null 2>&1
}

unpro () {
    unset http_proxy
    unset HTTP_PROXY
    unset https_proxy
    unset HTTPS_PROXY
    unset ftp_proxy
    unset FTP_PROXY
    unset rsync_proxy
    unset RSYNC_PROXY
    unset ALL_PROXY
    unset all_proxy
    sudo rm /etc/apt/apt.conf.d/proxy.conf
    git config --global --unset http.https://github.com.proxy
    ip_
}

git-config() {
    echo -n "Please input Git Username: "
    read username
    echo -n "Please input Git Email: "
    read email
    echo -n "Done!"
    git config --global user.name "${username}"
    git config --global user.email "${email}"
    git config --global credential.helper store
    git config --global pull.rebase true
    git config --global alias.s status
    git config --global alias.sb "status -sb"
    git config --global alias.d diff
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.last "log -1 HEAD"
    git config --global alias.cane "commit --amend --no-edit"
    git config --global alias.pr "pull --rebase"
    git config --global alias.lo "log --oneline -n 10"
    git config --global alias.a "add ."
    git config --global alias.cm "commit -m"
    git config --global alias.rh "reset --hard"
    git config --global alias.f "fetch"
}

ssh_start() {
  sshd_status=$(service ssh status)
  if [[ $sshd_status = *"is not running"* ]]; then
    sudo service ssh --full-restart
  fi
}

set_max_user_watches() {
    if ! grep -qF "max_user_watches" /etc/sysctl.conf ; then
        echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
    fi
    sudo sysctl -p >/dev/null 2>&1
    sudo sysctl --system >/dev/null 2>&1
}

expose_local(){
    sudo sysctl -w net.ipv4.conf.all.route_localnet=1 >/dev/null 2>&1
    sudo iptables -t nat -I PREROUTING -p tcp -j DNAT --to-destination 127.0.0.1
}

put_win_fonts() {
    # 将windows的字体放入ubuntu里
    sudo mkdir /usr/share/fonts/windows
    sudo cp -r /mnt/c/Windows/Fonts/*.ttf /usr/share/fonts/windows/
    fc-cache
}

bk() {
    cp ~/.zshrc ~/dotfiles/ubuntu_wsl/zshrc
    cp ~/.config/nvim/init.vim ~/dotfiles/ubuntu_wsl/init.vim
    cp ~/.config/nvim/coc-settings.json ~/dotfiles/ubuntu_wsl/coc-settings.json
    brew bundle dump --describe --force --no-upgrade --file="~/dotfiles/ubuntu_wsl/Brewfile"
}

ln_never_wrong() {
    ln -s "$1" "$2"
    symlinks -d $(dirname "$2")
    if [ ! -f "$2" ]; then
        echo "bloody hell… I got it wrong again"
        ln -s "$2" "$1"
    fi
}

gpg-login() {
    export GPG_TTY=$TTY
    echo "test" | gpg --clearsign > /dev/null 2>&1
}

gpg-logout() {
    echo RELOADAGENT | gpg-connect-agent
}

u-clean() {
    echo "clean"
    sudo apt-get clean
    echo "auto clean"
    sudo apt-get autoclean -y
    echo "auto remove"
    sudo apt-get autoremove -y
    echo "purge deborphan"
    deborphan | xargs sudo apt-get purge -y
    deborphan
    sudo aptitude search ?obsolete
    echo "aptitude purge"
    sudo aptitude purge ~o
}

u-update() {
    sudo apt-get update && sudo apt-get -y upgrade
    brew upgrade --verbose
    nali update -y
    tldr --update
}

zsh_history_fix() {
    mv ~/.zsh_history ~/.zsh_history_bad
    strings ~/.zsh_history_bad > ~/.zsh_history
    fc -R ~/.zsh_history
    rm ~/.zsh_history_bad
}

cdlast() {
  cd -
  ls -lrth --color=auto | tail
  zle reset-prompt
}
zle -N cdlast
bindkey '^Q' cdlast

# Kills a process running on a specified tcp port
killport() {
  for port in "$@"
  do
    echo "Killing process on port: $port"
    fuser -n tcp -k $port;
  done
}

# MVP
# Move and make parent directories
mvp() {
    source="$1"
    target="$2"
    target_dir="$(dirname "$target")"
    mkdir --parents $target_dir; mv $source $target
}

cleantmp() {
    sudo find /tmp -type f -atime +10 -delete
}

eval "$(pyenv init -)"
eval "$(starship init zsh)"

if [[ ! -f $HOME/.zinit/bin/zinit.zsh ]]; then
    print -P "%F{33}▓▒░ %F{220}Installing %F{33}DHARMA%F{220} Initiative Plugin Manager (%F{33}zdharma/zinit%F{220})…%f"
    command mkdir -p "$HOME/.zinit" && command chmod g-rwX "$HOME/.zinit"
    command git clone https://github.com/zdharma/zinit "$HOME/.zinit/bin" && \
        print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
        print -P "%F{160}▓▒░ The clone has failed.%f%b"
fi

source "$HOME/.zinit/bin/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit

zinit snippet OMZP::sudo
zinit snippet OMZ::lib/history.zsh
zinit snippet OMZP::extract

zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions
zinit light zsh-users/zsh-syntax-highlighting
zinit light sukkaw/zsh-ipip
zinit light agkozak/zsh-z
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354

参考

https://bytem.io/posts/wsl2-network-tricks/

https://github.com/bytemain/dotfiles/blob/master/ubuntu_wsl/zshrc

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/385324
推荐阅读
相关标签
  

闽ICP备14008679号