win7设置docker默认服务端地址

win7通过Docker Toolbox安装docker后,输入指令默认连接的是虚拟机中的default节点。连接远程docker服务时,需要使用docker -H tcp://ip:port [Commands]指令来操作,输入比较繁琐。通过添加bat脚本,实现自动添加-H tcp://ip:port参数。

1.开启docker远程访问

要本地连接远程docker服务,服务必须开启远程访问功能。参照开启docker远程访问设置服务器。

2.本地调整

2.1 docker.exe重命名

为保持使用习惯,bat脚本命名为docker.bat,命令行输入docker可直接调用。故需重命名docker.exedocker2.exe防止冲突。

2.2 添加docker.bat

Docker Toolbox根目录下新建docker.bat文件,内容如下。其中192.168.0.1:2375开启docker远程访问中所开通的远程docker服务地址。Docker Toolbox目录需添加至环境变量。

@echo off

:: 清空参数字符串
set str=

:: 参数拼接
:joint
if "%1"=="" goto docker
set str=%str% %1
shift
goto joint

:: 指令与参数拼接
:docker
docker2 -H tcp://192.168.0.1:2375 %str%

2.3 添加快速切换功能

实际在使用时,我们可能有多台远程docker服务器,而有时也会连接本地docker。每次手动修改docker.bat太过繁琐,故在Docker Toolbox下添加脚本,快速切换docker默认服务端地址。
脚本命名为dockerip.bat(根据个人喜好随意修改),只需命令行下输入dockerip,即可快速切换docker默认服务端地址。

@echo off
echo.-----------------------------------------------------------------
echo.修改docker远程服务器地址
set ip0=localhost
set ip1=192.168.0.1:2375
set ip2=192.168.0.2:2375

:: 选择待设置ip
:main
echo.-----------------------------------------------------------------
echo.0 %ip0%
echo.1 %ip1%
echo.2 %ip2%
set /p choice=请输入待设置ip编号,按回车键确认: 
if %choice%==0 set ip=%ip0%
if %choice%==1 set ip=%ip1%
if %choice%==2 set ip=%ip2%
if "%ip%"=="" (
echo.输入错误,请重新输入!
goto main
)else (
echo.输入有效,即将替换远程docker地址为"%ip%"
goto begin
)

:: 执行替换
:begin
echo.-----------------------------------------------------------------
:: 切换到工作路径,按实际Docker Toolbox路径修改I啊
pushd "D:\Program Files\Docker Toolbox"
:: 备份原docker.bat文件...
copy docker.bat docker.bat.bak>nul
:: 查找并删除原记录...
find /v "docker2" <docker.bat >docker.bat.new
:: 添加新记录...
if "%ip%"=="%ip0%" (
@echo docker2 %%str%% >>docker.bat.new
)else (
@echo docker2 -H tcp://%ip% %%str%% >>docker.bat.new
)
:: 保存修改后文件...
move docker.bat.new docker.bat>nul
popd
echo.操作完成,请按任意键退出...
echo.------------------------------------------------------------------
@Pause>nul

3.使用验证

C:\Users\11111> dockerip
-----------------------------------------------------------------
修改docker远程服务器地址
-----------------------------------------------------------------
0 localhost
1 192.168.0.1:2375
2 192.168.0.2:2375
请输入待设置ip编号,按回车键确认: 1
输入有效,即将替换远程docker地址为"192.168.0.1:2375"
-----------------------------------------------------------------
操作完成,请按任意键退出...
------------------------------------------------------------------
C:\Users\11111> docker version
Client:
 Version:           19.03.1
 API version:       1.38 (downgraded from 1.40)
 Go version:        go1.12.7
 Git commit:        74b1e89e8a
 Built:             Wed Jul 31 15:18:18 2019
 OS/Arch:           windows/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       e68fc7a
  Built:            Tue Aug 21 17:23:21 2018
  OS/Arch:          linux/amd64
  Experimental:     false
PS C:\Users\12623> dockerip
-----------------------------------------------------------------
修改docker远程服务器地址
-----------------------------------------------------------------
0 localhost
1 192.168.0.1:2375
2 192.168.0.2:2375
请输入待设置ip编号,按回车键确认: 2
输入有效,即将替换远程docker地址为"192.168.0.2:2375"
-----------------------------------------------------------------
操作完成,请按任意键退出...
------------------------------------------------------------------
C:\Users\11111> docker version
Client:
 Version:           19.03.1
 API version:       1.40
 Go version:        go1.12.7
 Git commit:        74b1e89e8a
 Built:             Wed Jul 31 15:18:18 2019
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.4
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.10
  Git commit:       9013bf583a
  Built:            Fri Oct 18 15:50:54 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
PS C:\Users\12623> dockerip
-----------------------------------------------------------------
修改docker远程服务器地址
-----------------------------------------------------------------
0 localhost
1 192.168.0.1:2375
2 192.168.0.2:2375
请输入待设置ip编号,按回车键确认: 0
输入有效,即将替换远程docker地址为"localhost"
-----------------------------------------------------------------
操作完成,请按任意键退出...
------------------------------------------------------------------
C:\Users\11111> docker version
Client:
 Version:           19.03.1
 API version:       1.40
 Go version:        go1.12.7
 Git commit:        74b1e89e8a
 Built:             Wed Jul 31 15:18:18 2019
 OS/Arch:           windows/amd64
 Experimental:      false
error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.40/version: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This er
ror may also indicate that the docker daemon is not running.

版权声明:本文为tobeforever原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/tobeforever/p/11868117.html