美文网首页
docker 通过Dockerfile构建镜像

docker 通过Dockerfile构建镜像

作者: 爱吃豆包 | 来源:发表于2019-07-11 10:41 被阅读0次

比如我们构建一个Spring cloud 的 Eureka 服务

先看目录清单文件:eureka-sever-0.0.1.jar、DockerFile

构建清单

Dockerfile清单文件

# 基础镜像
FROM java
# 镜像构建作者
LABEL maintainer docker_user<907147608@qq.com>
# 设置环境变量
# ENV JAVA_HOME=/xxx/xxx/jdk8
# 复制文件到目录 <本地目录> <容器目录>
COPY eureka-sever-0.0.1.jar /yunpan/smart-community/
# RUN 运行命令, 这个命令一般用户镜像安装的初始化操作,就是你这个进行需要的依赖设置
# RUN wget=http://xxx/xxx/jdk8
# CMD 命令, 如果 docker run [image] 后面没有追加命令,则执行CMD命令
# CMD nohup java -jar eureka-sever-0.0.1.jar > eureka-sever.log &
# ENTRYPOINT 命令,此命令无论 docker run [image] 后面是否追加命令都执行,如果有追加的命令,则拼接在 ENTRYPOINT 命令的后面
ENTRYPOINT ["nohup","java", "-jar","/yunpan/smart-community/eureka-sever-0.0.1.jar",">","eureka-sever.log","&"]

在windows下构建镜像
docker build -t eureka_image:0.0.1 E:\docker_worker\eureka 解析:

-t: 指定构建镜像的名称和版本(tag)
E:\docker_worker\eureka: 表示当前的上下文路径,指定Dockerfile之类的资源在哪里
E:\docker_worker\eureka>docker build -t eureka_image:0.0.1 E:\docker_worker\eureka
Sending build context to Docker daemon  44.79MB
Step 1/4 : FROM java
latest: Pulling from library/java
5040bd298390: Downloading [===========>                                       ]  11.34MB/51.36MB
fce5728aad85: Download complete
76610ec20bf5: Retrying in 2 seconds
60170fec2151: Downloading
e98f73de8f0d: Waiting
11f7af24ed9c: Waiting
49e2d6393f32: Waiting
bb9cdec9c7f3: Waiting
latest: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:latest
 ---> d23bdf5b1b1b
Step 2/4 : LABEL maintainer docker_user<907147608@qq.com>
 ---> Running in d45248d9d832
Removing intermediate container d45248d9d832
 ---> c69d400947f0
Step 3/4 : COPY eureka-sever-0.0.1.jar /yunpan/smart-community/
 ---> 2ff78a9be822
Step 4/4 : ENTRYPOINT ["nohup","java", "-jar","/yunpan/smart-community/eureka-sever-0.0.1.jar",">","eureka-sever.log","&"]
 ---> Running in d98318370813
Removing intermediate container d98318370813
 ---> 556eb5ab8e33
Successfully built 556eb5ab8e33
Successfully tagged eureka_image:0.0.1
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.

E:\docker_worker\eureka>

查看构建好的镜像

E:\docker_worker\eureka>docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
eureka_image               0.0.1               f62c9585047a        7 minutes ago       688MB

启动构建好的镜像

docker run -d -p 9000:8080 eureka_image:0.0.1

注意:我eureka在容器中默认的是8080端口,所以我要把本地机器的9000端口映射到容器中的8080端口

查看刚刚构建成功正在运行的容器

E:\docker_worker\eureka>docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                    NAMES
a1a152d739e8        eureka_image:0.0.1   "nohup java -jar /yu…"   3 minutes ago       Up 3 minutes        0.0.0.0:9000->8080/tcp   thirsty_hertz

查看容器日志

E:\docker_worker\eureka>docker logs -f a1a152d739e8
2019-07-11 02:17:19.029  INFO 1 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@ea4a92b: startup date [Thu Jul 11 02:17:19 UTC 2019]; root of context hierarchy
2019-07-11 02:17:19.573  INFO 1 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-07-11 02:17:19.672  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$f0131f26] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

2019-07-11 02:17:20.042  INFO 1 --- [           main] c.c.huiju.EurekaServerApplication        : No active profile set, falling back to default profiles: default
2019-07-11 02:17:20.061  INFO 1 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5bcab519: startup date [Thu Jul 11 02:17:20 UTC 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@ea4a92b
2019-07-11 02:17:21.318  INFO 1 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=6a0b506d-3241-3f31-8d60-217aba9d8216
2019-07-11 02:17:21.340  INFO 1 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-07-11 02:17:21.479  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$f0131f26] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-07-11 02:17:22.126  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-07-11 02:17:22.297  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]

...........

然后用浏览器访问 http://localhost:9000/

docker中eureka服务

遇到的坑:

构建好之后的容器启动就自动退出问题!
在上面的Dockerfile文件中,指定了 eureka-sever-0.0.1.jar 在容器镜像中的路径 /yunpan/smart-community/ ,但是我启动的时候没有加上这个路径,所以导致启动容器后就就直接退出了!
在启动的时候需要指定具体的路径!

传送门

Dockerfile文件指令解析
docker 容器之间互相访问(互联)

相关文章

网友评论

      本文标题:docker 通过Dockerfile构建镜像

      本文链接:https://www.haomeiwen.com/subject/rjhkkctx.html