CentOS7 下 配置Docker远程访问 与 windows下使用maven构筑Spring Boot 的 Docker镜像到远程服务端

1、设置Docker服务端,以支持远程访问:

修改docker服务端配置文件,命令:

vim /usr/lib/systemd/system/docker.service 

修改后:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
#remote call define
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock [Install] WantedBy=multi-user.target

以上加粗的蓝色内容即是新增配置,注意我使用的Docker版本为

  Docker version 17.09.0-ce, build afdb6d4

不同的版本可能配置文件内容不一样。

刷新配置、重启docker,命令:

systemctl daemon-reload
systemctl restart docker

查看修改结果命令:

ps -ef|grep docker

可看到:
......
root     29124     1  0 03:53 ?        00:00:11 /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
......

2、普通Linux下的Docker客户端通过指定服务端IP和端口的方式,可以远程访问Docker服务端,命令格式如下:

docker -H tcp://xxx.xxx.xxx.xxx:2375 images

如此可看到服务端的镜像列表

3、在Windows 中 利用 Maven 为该Docker服务端构筑docker 镜像(该Windows中无须安装Docker):

pom.xml配置插件:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.13</version>
                <configuration>
                    <imageName>liuyx/test:0.1</imageName>
            <!-- 本以为在此设置 前文服务端的地址和端口 可以生效,但是始终会报一个tcp protocol is not supported,鄙人才疏学浅,终究不知为什么,我们还是在别的地方设置吧 -->
<!--<dockerHost>tcp://xxx.xxx.xxx.xxx:2375</dockerHost>--> <!-- 设置Dockerfile路径,设置了就会忽略baseImage和entryPoint等Dockerfile中应出现的东西,转而去执行Dockerfile--> <!--<dockerDirectory>src/main/docker</dockerDirectory>-->
<skipDockerBuild>false</skipDockerBuild> <baseImage>java8</baseImage> <entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>

构筑命令:

#在此,先设置当前的环境变量“DOCKER_HOST”为前文提到的docker服务端的ip和端口,当然也可以直接去系统环境变量设置
set DOCKER_HOST=tcp://xxx.xxx.xxx.xxx:2375
mvn clean package -X docker:build

构筑完毕后,可在服务端查看构筑结果。

(完毕)