我的本地开发环境是win10。本篇分享制作镜像,下一篇分享一下将镜像push到阿里云镜像仓库的操作。

网上有很多方法是在pom文件中添加docker插件,让springboot项目在打包之后,自动构建Docker镜像。

我按照网上的方法都没有成功,报错:

Failed to execute goal com.spotify:docker-maven-plugin:1.0.0:build (build-image) on project web_hello_world: Exception caught

不清楚问题出在哪里。

所以放弃了用idea构建,而是直接用Docker客户端,执行docker build 命令来构建Springboot项目的镜像。

首先先用idea将springboot项目正常打包成可执行jar包,找到jar包目录,创建文件DockerFile,在文件中写入镜像命令:

# From java image, version : 8
FROM openjdk:8-jdk-alpine
# 挂载test-docker目录
VOLUME /tmp
# web_hello_world-0.0.1.jar 是我们用idea的maven打包出来的jar包
ADD web_hello_world-0.0.1.jar app.jar
#这里网上很多写的是bash,在我本地使用bash会报错,改为sh则正常
RUN sh -c "touch /app.jar"
#默认8080端口
#指定项目暴露的端口号,与项目配置一样,即容器配置的暴露端口
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

然后打开windows的客户端:Docker Quickstart Terminal,定位到DockerFile文件目录,执行:

docker build -t="web_hello_world" .

-t命令是给镜像明明,名字后面加一个空格和一个点,屏幕输出:

$ docker build -t="web_hello_woeld" .
Sending build context to Docker daemon 16.83MB
Step 1/6 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/6 : VOLUME /tmp
---> Using cache
---> 61789a717640
Step 3/6 : ADD web_hello_world-0.0.1.jar app.jar
---> 772a4c77cd6f
Step 4/6 : RUN sh -c "touch /app.jar"
---> Running in b03a440344a4
Removing intermediate container b03a440344a4
---> 0107d1555b45
Step 5/6 : EXPOSE 80
---> Running in e64bb79d3942
Removing intermediate container e64bb79d3942
---> eb3928ed1c89
Step 6/6 : ENTRYPOINT ["java", "-jar", "app.jar"]
---> Running in 723e28e3af12
Removing intermediate container 723e28e3af12
---> 5f7396b0ee3a
Successfully built 5f7396b0ee3a
Successfully tagged web_hello_woeld:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

 

这样镜像就制作完成了。

可以使用docker images命令查看当前的镜像列表:

Admin@▒ߺ▒▒▒ MINGW64 /d/apache-maven-3.5.3/package/com/zhl/web_hello_world/0.0.1
$ docker images
REPOSITORY                                   TAG                 IMAGE ID            CREATED             SIZE
web_hello_woeld                              latest              5f7396b0ee3a        7 minutes ago       138MB
nginx                                        latest              5a3221f0137b        3 weeks ago         126MB
openjdk                                      8-jdk-alpine        a3562aa0b991        4 months ago        105MB
hello-world                                  latest              fce289e99eb9        8 months ago        1.84kB

 

运行镜像:

docker run -d --name test1  -p 8080:8080 web_hello_world

此时镜像完美运行,但是此时在浏览器访问:localhost:8080 是打不开我的测试页面的,因为ip不对,需要查看docker的ip:

Admin@▒ߺ▒▒▒ MINGW64 /d/apache-maven-3.5.3/package/com/zhl/web_hello_world/0.0.1
$ docker-machine ip
192.168.99.100

在浏览器输入:http://192.168.99.100:8080 就能打开测试springboot项目的测试页面

还有一个日志的问题。

运行镜像时,-v命令是将本地目录映射至容器目录,这样我们就能看到容器内运行时产生的日志,但是我在windows环境下没有映射成功,在centos下没有问题,暂时没找到答案。

下一篇分享一下将镜像push到阿里云镜像仓库的操作。

发表评论