1. 开启2375端口,供外部访问docker
vim /usr/lib/systemd/system/docker.service 修改ExecStart为下面一行内容 #ExecStart=/usr/bin/dockerd -H unix:// ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
systemctl daemon-reload // 1,加载docker守护线程 systemctl restart docker // 2,重启docker
测试端口是否成功 curl 127.0.0.1:2375/info
在win10客户端添加环境变量
添加dos下mvn命令的执行
直接在Path属性后面添加一条当前Maven的文件地址加上\bin
2. 使用docker-maven-plugin插件上传镜像到docker服务器
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <imageName>${project.name}:${project.version}</imageName> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <dockerHost>http://192.168.1.10:2375</dockerHost> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>
3. 在src/main下面新建文件夹docker
添加文件Dockerfile
FROM daocloud.io/library/java:8u40-b22 #这里是巨坑,下载慢的话一定要关闭从新下载 VOLUME /tmp ADD gexin-docker-demo-1.0-SNAPSHOT.jar app.jar RUN sh -c 'touch /app.jar' ENV JAVA_OPTS="" ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
执行命令 mvn clean package docker:build
执行成功出现如下截图内容
报错如下:
1. No plugin found for prefix 'docker' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (F:\repo), nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public)] -> [Help 1]
解决办法:在maven的conf/setting.xml中要加入
<pluginGroups> <!-- pluginGroup | Specifies a further group identifier to use for plugin lookup. <pluginGroup>com.your.plugins</pluginGroup> --> <pluginGroup>com.spotify</pluginGroup> </pluginGroups>
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
再次执行mvn clean package docker:build
4. 登录虚拟机可以看到
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gexin-docker-demo 1.0-SNAPSHOT ac7ed71487ca 16 seconds ago 904MB
daocloud.io/library/java 8u40-b22 0a5e1e22983a 4 years ago 815MB
[root@localhost ~]# docker run -d -p 8091:8091 gexin-docker-demo:1.0-SNAPSHOT
bd87d64f71668c9e97eb99de6b8d50ae0a9ca2e80ad0da8081bc50cd000b345c
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd87d64f7166 gexin-docker-demo:1.0-SNAPSHOT "sh -c 'java $JAVA_O…" 7 seconds ago Up 5 seconds 0.0.0.0:8091->8091/tcp confident_curran
在浏览器查看结果 http://192.168.1.10:8091/
总结:
1. 选择使用docker和dockerfile执行很重要
2. FROM 选择jdk很重要,可能造成下载过慢或者找不到包