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

 

構築完畢后,可在服務端查看構築結果。

 

(完畢)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM