服务器Docker容器使用
·
0.基础指令
# 查看容器
docker ps
docker images
1.删除旧容器
docker rm -f novnc-test 2>/dev/null
2.删除旧镜像
docker rmi ubuntu-novnc-vnc:22.04 2>/dev/null
3.确认删除情况
docker ps -a | grep novnc-test
docker images | grep ubuntu-novnc-vnc
4.重新加载镜像
cd docker-images
docker load -i ubuntu-novnc-vnc-22.04.tar
docker images | grep ubuntu-novnc-vnc
5.映射容器(GPU驱动)
docker run -d \
--name novnc-test \
--gpus all \
--ipc=host \
--cap-add=NET_ADMIN \
--device=/dev/net/tun \
-p 2222:22 \
-v /longhorn/workspace:/root/workspace \
ubuntu-novnc-vnc:22.04
国产GPU
docker run -d \
--name novnc-test7 \
--device=/dev/dri/renderD128 \
--device=/dev/dri/renderD129 \
--device=/dev/dri/renderD130 \
--device=/dev/dri/renderD131 \
--device=/dev/dri/card1 \
--device=/dev/dri/card2 \
--device=/dev/dri/card3 \
--device=/dev/dri/card4 \
--group-add video \
--ipc=host \
--cap-add=NET_ADMIN \
--device=/dev/net/tun \
-p 2227:22 \
-v /longhorn/workspace/Fault_Diagnosis:/root/workspace \
ubuntu-novnc-vnc:22.04
A卡
docker run -d \
--name novnc-test \
--gpus all \
--cap-add=NET_ADMIN \
--device=/dev/net/tun \
-p 2222:22 \
-v /longhorn/workspace:/root/workspace \
ubuntu-novnc-vnc:22.04
6.进入容器
docker exec -it novnc-test bash
7.安装ssh
apt update &&
apt install -y openssh-server &&
mkdir -p /run/sshd &&
echo 'root:1' | chpasswd &&
sed -i 's/^#\\?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config &&
sed -i 's/^#\\?PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config &&
service ssh restart || /etc/init.d/ssh restart || (/usr/sbin/sshd &) &&
echo "SSH 配置完成,自动退出容器" &&
exit 0
docker exec -it novnc-test sh -c "echo 'root:1' | chpasswd"
docker exec -it novnc-test sh -lc "sed -ri 's/^#?PermitRootLogin.*/PermitRootLogin yes/; s/^#?PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config"
docker exec -it novnc-test sh -lc "service ssh restart || /etc/init.d/ssh restart || pkill sshd; /usr/sbin/sshd & sleep 1"
8.端口防火墙设置
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload
firewall-cmd --list-ports | grep 2222
# 验证防火墙
docker ps --filter "name=novnc-test"
docker port novnc-test
ss -lntp | grep 2222
9.运行跳板转发
# 允许 ProxyJump/端口转发
sed -i 's/^AllowTcpForwarding no/AllowTcpForwarding yes/' /etc/ssh/sshd_config
grep -q '^PermitOpen' /etc/ssh/sshd_config || echo 'PermitOpen any' >> /etc/ssh/sshd_config
sshd -t && systemctl restart sshd
# 验证
grep -nE 'AllowTcpForwarding|PermitOpen' /etc/ssh/sshd_config
10.本机连接(带VNC)
Host k3s-node02
HostName 172.16.108.22
User root
Port 22
# ==================== 第一个容器(原来的)====================
Host NVIDIA
HostName 127.0.0.1
User root
Port 2222
ProxyJump k3s-node02
LocalForward 6080 127.0.0.1:6080
ServerAliveInterval 30
ServerAliveCountMax 3
# ==================== 第二个容器(新增的)====================
Host NVIDIA2
HostName 127.0.0.1
User root
Port 2223 # 这里改成 2223
ProxyJump k3s-node02
LocalForward 6081 127.0.0.1:6081 # 这里改成 6081
ServerAliveInterval 30
ServerAliveCountMax 3
http://localhost:6080
http://localhost:6081
11.本地连接
# 命令行验证
ssh -v NVIDIA
ssh -N -L 2222:127.0.0.1:2222 root@172.16.108.22
更多推荐
所有评论(0)