Docker 学习:生成docker镜像

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值