文章目录
Docker生成镜像的两种方式
有时候从Docker镜像仓库中下载的镜像不能满足要求,我们可以基于一个基础镜像构建一个自己的镜像
两种方式:
- 更新镜像:使用 docker commit 命令
- 构建镜像:使用 docker build 命令,需要创建Dockerfile文件
更新镜像
先使用基础镜像创建一个容器,然后对容器内容进行更改,然后使用 docker commit 命令提交为一个新的镜像(以 tomcat为例)。
1.根据基础镜像,创建容器
docker run --name mytomcat -p 80:8080 -d tomcat
2.修改容器内容
docker exec -it mytomcat /bin/bash
cd webapps/ROOT
rm -f index.jsp
echo hello world > index.html
exit
3.提交为新镜像
docker commit -m="描述消息" -a="作者" 容器ID或容器名 镜像名:TAG
# 例:
# docker commit -m="修改了首页" -a="华安" mytomcat huaan/tomcat:v1.0
4.使用新镜像运行容器
docker run --name tom -p 8080:8080 -d huaan/tomcat:v1.0
使用Dockerfile构建镜像
什么是Dockerfile?
Dockerfile is nothing but the source code for building Docker images
- Docker can build images automatically by reading the instructions from a Dockerfile
- A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image
- Using docker build users can create an automated build that executes several command-line instructions in succession
Dockerfile格式
- Format:
- #Comment
- INSTRUCTION arguments
- The instruction is not case-sensitive
- However,convention is for them to be UPPERCASE to 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
# 基于哪个镜像,指定基础镜像,本地没有会从dockerHub pull下来
FROM java:8
# 将本地文件夹挂载到当前容器
VOLUME /tmp
# 拷贝文件到容器,rouyi-eureka-ser