FROM openjdk:8-jdk-alpine
RUN apk update && apk upgrade && apk add netcat-openbsd
这是为了构建出一个包含轻量级JDK的jar包运行环境。docker build -t openjdk:netcat .
docker images
docker run --rm openjdk:netcat nc -h
.
是不能省略的;如果第一句运行失败了,很有肯恩是因为镜像下载的地址配置的不是很好;可以使用这个下载地址,阿里云的,亲测好用:"https://6kx4zyno.mirror.aliyuncs.com"
REPOSITORY TAG IMAGE ID CREATED SIZE
openjdk netcat abb92c2d95e0 About an hour ago 127MB
OpenBSD netcat (Debian patchlevel 1.130)
usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-O length]
[-P proxy_username] [-p source_port] [-q seconds] [-s source]
[-T toskeyword] [-V rtable] [-w timeout] [-X proxy_protocol]
[-x proxy_address[:port]] [destination] [port]
Command Summary:
-4 Use IPv4
-6 Use IPv6
-b Allow broadcast
-C Send CRLF as line-ending
-D Enable the debug socket option
-d Detach from stdin
-F Pass socket fd
-h This help text
-I length TCP receive buffer length
-i secs Delay interval for lines sent, ports scanned
-k Keep inbound sockets open for multiple connects
-l Listen mode, for inbound connects
-N Shutdown the network socket after EOF on stdin
-n Suppress name/port resolutions
-O length TCP send buffer length
-P proxyuser Username for proxy authentication
-p port Specify local port for remote connects
-q secs quit after EOF on stdin and delay of secs
-r Randomize remote ports
-S Enable the TCP MD5 signature option
-s addr Local source address
-T toskeyword Set IP Type of Service
-t Answer TELNET negotiation
-U Use UNIX domain socket
-u UDP mode
-V rtable Specify alternate routing table
-v Verbose
-w secs Timeout for connects and final net reads
-X proto Proxy protocol: "4", "5" (SOCKS) or "connect"
-x addr[:port] Specify proxy address and port
-Z DCCP mode
-z Zero-I/O mode [used for scanning]
Port numbers can be individual or ranges: lo-hi [inclusive]
在进行这一步之前,需要做好如下准备:
<plugin>
<artifactId>maven-resources-pluginartifactId>
<executions>
<execution>
<id>copy-resourcesid>
<phase>validatephase>
<goals>
<goal>copy-resourcesgoal>
goals>
<configuration>
<outputDirectory>${basedir}/target/dockerfileoutputDirectory>
<resources>
<resource>
<directory>src/main/dockerdirectory>
<filtering>truefiltering>
resource>
resources>
configuration>
execution>
executions>
plugin>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>0.4.10version>
<configuration>
<imageName>${docker.image.name}:${docker.image.tag}imageName>
<dockerDirectory>${basedir}/target/dockerfiledockerDirectory>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
FROM openjdk:netcat # 指定源镜像
RUN mkdir -p /usr/local/cargoservice # 在将要构建的镜像的用户空间中创建/usr/local/cargoservice目录
ADD @[email protected] /usr/local/cargoservice/ # 将事先打成.jar包的项目拷贝到这个文件夹之下
ADD run.sh run.sh # 将我们写好的.sh脚本(用来运行jar包)转移到镜像的文件夹中
RUN chmod +x run.sh # 赋予我们的.sh脚本运行的权限
CMD ./run.sh # 在镜像中运行.sh脚本
.sh脚本的参考内容如下:
它的核心目的就是运行我们达成.jar包的Springboot项目,使之能够提供相应的功能。
#!/bin/sh
echo "********************************************************"
echo "Starting Cargo Server"
echo "********************************************************"
java -jar /usr/local/cargoservice/@[email protected]
其中/usr/local/cargoservice/@[email protected]
中,用了@project.build.finalName@来代替编译好之后.jar包的名字,避免了命名混乱的问题。这是之前引入的maven-resources-plugin
包所提供的功能。
接下来,回到Springboot的工作目录(根目录之下),运行:mvn clean package docker:build
,即可根据我们的/src/main/docker/Dockerfile构建出我们想要的镜像。
docker run --rm -p 8080:8080 example/cargo-service:section11