鲁班学院java架构师成长路线

Docker镜像的两种方式

有时候从Docker镜像仓库中下载的镜像不能满足要求,我们可以

于一个基础镜像构建一个自己的镜像

两种方式:

l 更新镜像:使用docker commit命令

l 构建镜像:使用docker build命令,需要创建Dockerfile文件

更新镜像

先使用基础镜像创建一个容器,然后对容器内容进行更改,然后使用docker commit命令提交为一个新的镜像(以tomcat为例)。

1.根据基础镜像,创建容器

1docker run --name mytomcat -p 80:8080 -d tomcat

2.修改容器内容

1docker exec -it mytomcat /bin/bash
2cd webapps/ROOT
3rm -f index.jsp
4echo hello world > index.html
5exit

3.提交为新镜像

1docker commit -m="描述消息" -a="作者" 容器ID或容器名 镜像名:TAG
2# 例:
3# docker commit -m="修改了首页" -a="华安" mytomcat huaan/tomcat:v1.0

4.使用新镜像运行容器

1docker run --name tom -p 8080:8080 -d huaan/tomcat:v1.0

使用Dockerfile构建镜像

什么是Dockerfile?

Dockerfile is nothing but the source code for building Docker images

l Docker can build images automatically by reading the instructions from a Dockerfile

l A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image

l Using docker build users can create an automated build that executes several command-line instructions in succession

Dockerfile格式

。Format:

1.#Comment

2.INSTRUCTION arguments

。The instruction is not case-sensitive

l However,convention is for them to be UPPERCASEto distinguish them from arguments more easily

。Docker runs instructions in a Dockerfile in order

。The first instruction must be 'FROM' in order to specify the Base Image from which you are building

使用Dockerfile构建SpringBoot应用镜像

一、准备

1.把你的springboot项目打包成可执行jar包

2.把jar包上传到Linux服务器

二、构建

1.在jar包路径下创建Dockerfile文件vi Dockerfile

1# 指定基础镜像,本地没有会从dockerHub pull下来
2FROM java:8
3#作者
4MAINTAINER huaan
5# 把可执行jar包复制到基础镜像的根目录下
6ADD luban.jar /luban.jar
7# 镜像要暴露的端口,如要使用端口,在执行docker run命令时使用-p生效
8EXPOSE 80
9# 在镜像运行为容器后执行的命令
10ENTRYPOINT ["java","-jar","/luban.jar"]

2.使用docker build命令构建镜像,基本语法

1docker build -t huaan/mypro:v1 .
2# -f指定Dockerfile文件的路径
3# -t指定镜像名字和TAG
4# .指当前目录,这里实际上需要一个上下文路径

三、运行

运行自己的SpringBoot镜像

1docker run --name pro -p 80:80 -d 镜像名:TAG