Docker交互式Shell运行器
项目描述
Docker交互式Shell运行器
docker-shell
命令可以快速轻松地在Docker容器内启动交互式shell,具有以下功能
-
支持Windows和Linux容器。
-
当前工作目录将自动绑定到容器并设置为容器的当前工作目录。
-
当在Linux主机系统下运行Linux容器时,将自动启用主机网络模式,以消除暴露单个端口或端口范围的需求。
-
当在安装了NVIDIA二进制驱动程序和NVIDIA容器工具包的Linux主机系统下运行启用GPU的Linux容器时,将自动启用GPU支持。
-
为运行流行的shell提供了简短别名
dbash
对应于GNU Bashdsh
对应于Bourne shelldzsh
对应于Zshdcmd
对应于Windows命令提示符dpwsh
对应于Windows PowerShelldpwsh
对应于PowerShell Core
请查看developer-images存储库,以获取专为与docker-shell一起使用而设计的容器镜像的示例。
内容
需求
-
Docker支持的操作系统之一(Windows系统需满足版本1809或更高版本的要求)
- Linux: Docker支持的Linux发行版的64位版本(CentOS 7+、Debian 7.7+、Fedora 26+、Ubuntu 14.04+)
- macOS: 在2010年或更新的Mac硬件上运行的macOS 10.10.3 Yosemite或更高版本
- Windows 10: 64位Windows 10 Pro/Enterprise/Education版本1809或更高版本,或64位Windows 10 Home版本2004或更高版本,并启用WSL2
- Windows Server: Windows Server 2019 / Windows Server版本1809或更高版本
-
适用于您的操作系统的适当Docker安装
- 在Windows 10或macOS上使用Docker Desktop
- 在Linux上使用Docker社区版(CE)
- 在Windows Server上使用Docker企业版(EE)
-
Python版本3.5或更高版本
安装
要安装docker-shell
,请运行以下命令
# This may need to be prefixed with sudo under Linux and macOS
pip3 install docker-shell
使用
基本使用
要在Windows、macOS或Linux下在官方Python容器中启动GNU Bash shell,请运行以下任一命令
# Long version
docker-shell bash python
# Short version
dbash python
要在Windows下在Windows Server Core容器中启动Windows命令提示符,请运行以下任一命令
# Long version
docker-shell cmd mcr.microsoft.com/windows/servercore:ltsc2019
# Short version
dcmd mcr.microsoft.com/windows/servercore:ltsc2019
要在Windows下在Windows Server Core容器中启动PowerShell会话,请运行以下任一命令
# Long version
docker-shell powershell mcr.microsoft.com/windows/servercore:ltsc2019
# Short versions
dps mcr.microsoft.com/windows/servercore:ltsc2019
dpowershell mcr.microsoft.com/windows/servercore:ltsc2019
向Docker传递额外的标志
在命令行上指定的任何附加标志将自动传播到底层的docker run命令
# The flags `-u 1000 --name mycontainer` will be passed directly to Docker
docker-shell bash python -u 1000 --name mycontainer
使用别名标签进行操作
当处理长的镜像标签时,通常使用docker tag命令来创建简短的别名会更方便,例如
# Add a shorter tag alias for the Windows Server Core image
docker tag mcr.microsoft.com/windows/servercore:ltsc2019 windows:latest
# Run a Windows Command Prompt using the short tag
dcmd windows
使用图像标签指定额外的选项
如果在使用特定镜像时始终使用绑定挂载或命令行标志,则可以使用Dockerfile中的镜像标签来指定这些标志,而docker-shell
将自动应用它们
FROM my-base-image
# The directory specified by the environment variable `SOMEVAR` will be bind-mounted irrespective of the host platform
LABEL docker-shell.mounts.1="\$SOMEVAR:/data"
# The named volume specified by the special `VOLUME` variable (see below for details) will be bind-mounted irrespective of the host platform
LABEL docker-shell.mounts.2="\$VOLUME:/project-data"
# The current user's Desktop directory will be bind-mounted irrespective of the host platform
LABEL docker-shell.mounts.3="~/Desktop:/desktop"
# This specific user's Desktop directory will be bind-mounted only when running on Linux hosts
LABEL docker-shell.linux.mounts.1="/home/user/Desktop:/user-desktop"
# This specific user's Desktop directory will be bind-mounted only when running on macOS hosts
LABEL docker-shell.mac.mounts.1="/Users/user/Desktop:/user-desktop"
# This specific user's Desktop directory will be bind-mounted only when running on Windows hosts
LABEL docker-shell.windows.mounts.1="C:/Users/User/Desktop:/user-desktop"
# The command-line flags `-u 1000` will be applied irrespective of the host platform
LABEL docker-shell.args.1="-u"
LABEL docker-shell.args.2="1000"
# The command-line flags `--priveleged` will be applied only when running on Linux hosts
LABEL docker-shell.linux.args.1="--priveleged"
# The command-line flags `-m 1GB` will be applied only when running on macOS hosts
LABEL docker-shell.mac.args.1="-m"
LABEL docker-shell.mac.args.2="1GB"
# The command-line flags `-m 2GB` will be applied only when running on Windows hosts
LABEL docker-shell.windows.args.1="-m"
LABEL docker-shell.windows.args.2="2GB"
在标签中可用的特殊变量
通过镜像标签指定附加选项时,支持以下特殊变量
CWD
:解析为主机系统上的当前工作目录HOSTIP
:解析为主机系统在本地网络上的IP地址,如果没有网络接口,则为127.0.0.1
VOLUME
:解析为主机系统当前工作目录的绝对路径的SHA-256哈希值,适用于用作与特定项目关联的数据的卷名称,但出于性能或兼容性原因不希望在主机文件系统中存储文件
Windows 10下的自动Docker守护进程选择
在Windows 10下运行Docker Desktop时,docker-shell可以根据指定的shell自动在Windows容器模式和Linux容器模式之间切换。(Windows容器模式用于cmd
和powershell
,而Linux容器模式用于所有其他shell。)因为这种行为会改变系统状态,可能会被认为具有侵略性,所以默认禁用。要启用自动守护进程选择,请将环境变量DOCKERSHELL_WINDOWS_AUTO_SELECT
设置为1
。
法律
版权所有 © 2020,Adam Rehn。在MIT许可下授权,有关详细信息,请参阅文件LICENSE
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。