含源码 【保姆级教程】Docker基础操作篇-Dokerfile( 三 )


文章插图
那么小名来解释一下这个文件吧
#项目需要java8的镜像支持,待会部署到Docker上时,Docker如果没有找到jdk8的镜像话,会自动帮你下载FROM java:8#把本地的文件拷贝到容器镜像中,名字为eamonCOPY *.jar /eamon.jar#在容器运行的时候要运行的命令CMD ["--server.port=8080"]#暴露容器端口EXPOSE 8080#追加命令到 --server.port=8080 之后追加 java -jar命令ENTRYPOINT ["java","-jar","/eamon.jar"]
2.修改好url后,把小名的测试程序打成jar包,如下图:
3.上传jar包和到服务器
这里小名使用的,直接将本地文件拖拽到服务器上的Demo目录里即可;用的同学请自行处理,此处不做演示:
#进入 /home 目录[root@eamon /]# cd /home#创建 Demo 文件夹[root@eamon home]# mkdir Demo#进入 Demo 文件夹[root@eamon home]# cd Demo#查看上传好的两个文件[root@eamon Demo]# lsDockerfilemysqltest-0.0.1-SNAPSHOT.jar
4.把刚刚上传的文件,构建成镜像
命令:
docker build -t eamonmysql .
#进入Demo目录[root@eamon /]# cd /home/Demo/#查看文件[root@eamon Demo]# ll总用量 33508-rw-r--r--. 1 root root124 2月7 16:24 Dockerfile-rw-r--r--. 1 root root 34305144 2月7 16:24 mysqltest-0.0.1-SNAPSHOT.jar#构建名为eamonmysql的镜像【错误】[root@eamon Demo]# docker build -t eamonmysql"docker build" requires exactly 1 argument.See 'docker build --help'.Usage:docker build [OPTIONS] PATH | URL | -Build an image from a Dockerfile#构建名为eamonmysql的镜像【正确】[root@eamon Demo]# docker build -t eamonmysql .Sending build context to Docker daemon34.31MBStep 1/5 : FROM java:88: Pulling from library/java5040bd298390: 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:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9dStatus: Downloaded newer image for java:8---> d23bdf5b1b1bStep 2/5 : COPY *.jar /eamon.jar---> 4659e16236b2Step 3/5 : CMD ["--server.port=8080"]---> Running in e9c72dda067fRemoving intermediate container e9c72dda067f---> 94808e1967a8Step 4/5 : EXPOSE 8080---> Running in d18fb18812a1Removing intermediate container d18fb18812a1---> 82aa873f6a9dStep 5/5 : ENTRYPOINT ["java","-jar","/eamon.jar"]---> Running in 92355f08991eRemoving intermediate container 92355f08991e---> 829877ea522fSuccessfully built 829877ea522fSuccessfully tagged eamonmysql:latest
看出 build -t 和 build -t.的区别了吗?
对了!就是结尾的.!千万不要像小名这样粗心呦~
官方文档地址:
Thisthat the PATH is ., and so all the files in the localget tard and sent to the. The PATHwhere to find the files for the “” of the build on the.that thecould beon aand that noof theat theside (where you’rebuild). That means that all the files at PATH get sent, not just the onesto ADD in the .
简单来说:Path设置成.的意思是:在当前目录下的多有文件都被发送进了的守护进程
5.运行刚才的镜像
[root@eamon Demo]# docker run -d -P --name eamon-mysql-test eamonmysql2fa54dcc77b8b9984400ee14cfd6690b77ab653c59241fcf3130c738328b184c[root@eamon Demo]# docker psCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES2fa54dcc77b8eamonmysql "java -jar /eamon.ja…"10 s8 s0.0.0.0:49153->8080/tcpeamon-mysql-test1a2058874e0bmysql:5.7"docker-entrypoint.s…"2 d28 m0.0.0.0:3306->3306/tcp, 33060/tcpmysql01
至此我们的镜像运行成功
第五步:我们通过物理机的浏览器访问一下容器里面的项目吧~