docker部署opencv而且容器内的cv2.show显示到主机外面
- 目前是在debian12系统中测试
1. 配置ssh打开x服务器转发图形
修改文件/etc/ssh/sshd_config, 搜索X11
ForwardX11 yes,X11Forwarding yes
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
Include /etc/ssh/sshd_config.d/*.conf
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
KbdInteractiveAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the KbdInteractiveAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin prohibit-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
# override default of no subsystems
Subsystem sftp /usr/lib/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
X11Forwarding yes
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
Dockerfile[可以修改ssh的user和password]
FROM python:3.10
# 安装必要的软件包和依赖项
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
ENV QT_QPA_PLATFORM_PLUGIN_PATH=/usr/local/lib/python3.10/site-packages/cv2/qt/plugins
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && \
apt-get update && \
apt-get install -y --no-install-recommends \
openssh-server \
xauth \
xorg \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# 创建 SSH 所需的目录
RUN mkdir /run/sshd
# 设置 SSH 服务器配置
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
sed -i 's/#X11Forwarding no/X11Forwarding yes/' /etc/ssh/sshd_config
# 添加用户并设置密码
RUN useradd -m user && \
echo "user:password" | chpasswd
# 配置 SSH 登录环境变量
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile && \
echo "export DISPLAY=:0" >> /etc/profile
# 设置工作目录
WORKDIR /code/smart_watch_dog/
# 安装 Python 依赖项
ADD requirements.txt requirements.txt
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
pip config set install.trusted-host mirrors.aliyun.com && \
pip install --upgrade pip && \
pip install -Ur requirements.txt && \
pip cache purge
# 添加项目代码
ADD . .
# 暴露 SSH 端口
EXPOSE 22
# 启动 SSH 服务器和你的 Python 应用程序
CMD ["/usr/sbin/sshd", "-D"]
部署命令
- 打包docker image
docker build -t phd-w
- 运行容器
docker run -d -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=:0 -p 2222:22 phd-w
- 在
主机连接到容器的ssh x图像转发
ssh -X -p 2222 user@localhost
输入yes, 密码...就行感觉这里好像不用也可以
- 进入到容器内执行启动项目
python smart_watch_dog.py