Dockerfile镜像的制作


如果学习Docker,那么制作镜像这一步肯定不能少的,别人给你的是环境,而你自己做的才是你最终需要的东西,接下来就记录一下如何制作一个满足自己的镜像,我们使用docker一般就是部署我们的应用,那么我就制作一个镜像来发布我们的应用,制作方式我们就选择Dockerfile的方式

Dockerfile:

docker其实就像是一个脚本文件,或者你可以直接把他看成一个脚本或者就是看成一条条命令的集合,在Dockerfile文件中我们一般分为四个部分:基础镜像、创建者信息(可省略)、制作镜像过程(操作镜像指令)、启动容器的命令

接下来我们先了解一下这四部分分别是什么?

准备:获取基础镜像

我们可以从镜像仓库中获取适合我们的基础镜像使用docker search <镜像名>查询镜像,使用docker pull <镜像名>获取,如:我要获取一个jdk8的镜像那么我们

#查询镜像仓库中jdk8相关的镜像
docker search java8
#获取名字是corvis/java8的镜像
docker pull corvis/java8

如下图:这个镜像我已经下载过了所以显示的已经存在

全部下载完成之后我们可以查一下我们的本地会有一个名字是corvis/java8的镜像

#查询本地镜像
docker images

到这里我们的基础镜像已经获取到了,我们把创建的Dockerfile文件放到一个单独的目录下,并把我们的项目打好的包也放在该目录下,接下里我们开始写我们的Dockerfile文件

制作的步骤

  1. 指定基础镜像

    #指定基础镜像(我们所有的操作都是在这个镜像的基础上做的)
    FROM corvis/java8
  2. 写入创建者信息

    MAINTAINER SunArmy
  3. 把我们的项目copy到镜像中,并暴露端口,这里我直接用springBoot写了一个空的项目打包test.jar,端口开的80

    #从本地copy到镜像的/usr/local目录下,使用ADD和COPY都可以
    ADD test.jar /usr/local/
    #暴露项目端口到宿主机

EXPOSE 80


4. 启动容器我们需要执行的命令(启动项目的命令)

使用CMD和ENTRYPOINT都可以

CMD ["java","-jar","/usr/local/test.jar"]

![](https://img2018.cnblogs.com/blog/1470032/201906/1470032-20190620195709987-889940204.png)

~~~python
FROM corvis/java8
MAINTAINER SunArmy
COPY test.jar /usr/local/
EXPOSE 80
CMD ["java","-jar","/usr/local/test.jar"]
  1. 我们已经写好了Dockerfile文件那么我们直接在当前目录下执行命令来生成镜像

    #根据Dockerfile文件制作镜像
    docker build -t test:1.0 .
    #查看本地镜像
    docker images

至此,我们的镜像已经做好了

Dockerfile常用指令,语法

1、FROM <镜像名image>:<版本号tag>
一般是Dockerfile的第一行,指定基础镜像 2、MAINTAINET <创建者信息>
指明该镜像的创建者信息 3、RUN <命令>
容器中需要执行的命令,如:在容器创建之后需要在根目录创建一个logs目录
RUN mkdir /logs 4、COPY <本地文件> <容器路径>
复制本地文件到镜像中,本地路径是以Dockkerfile所在目录为根目录 如:把test.jar复制到/usr/local目录
COPY test.jar /usr/local 5、ADD <本地文件> <容器路径>
复制本地文件到镜像中,本地路径是以Dockkerfile所在目录为根目录,区别与COPY的可以从URL复制,可以直接解压.tar.gz并把解压之后的文件复制到镜像
如:把test.jar复制到/usr/local目录
ADD test.jar /usr/local
从URL复制到/usr/local
ADD URL /usr/local
解压test.tar.gz复制到镜像/usr/local
ADD test.tar.gz /usr/local 6、ENV <key> <value>
设置环境变量,可以直接用的环境变量有如下
$HOME 用户主目录
$HOMENAME 容器主机名
$PATH 容器环境变量(这个会经常用到)
如:我们要把/usr/bin添加到环境变量中
ENV PATH $PATH:/usr/bin 7、EXPOSE [<port>...]
Docker服务端暴露端口,如:我们镜像中有8080和8081两个端口需要暴露出去
EXPOSE 8080 8081 8、CMD ["",""]
括号中的是需要执行的命令
指定启动容器时执行的命令,每个Dockerfile只能有一条CMD指令,如果指定了多条指令,则最后一条执行(如果启动的时候指定了命令会被启动时指定的命令覆盖) 9、ENTRYPOINT ["",""]
和CMD指令一样,唯一不同的是即使启动的时候指定了命令,该命令也不会被覆盖 10、VOLUME ["/data"]
作用是创建在本地主机或其他容器可以挂载的数据卷,用来存放数据。 11、USER username
指定容器运行时的用户名或UID,后续的RUN也会使用指定的用户。要临时使用管理员权限可以使用sudo。在USER命令 之前可以使用RUN命令创建需要的用户。 12、WORKDIR /path
为后续的RUN CMD ENTRYPOINT指定配置工作目录,可以使用多个WORKDIR指令,若后续指令用得是相对路径,则会基 于之前的命令指定路径。 13.ONBUILD [INSTRUCTION]
该配置指定当所创建的镜像作为其他新建镜像的基础镜像时所执行的指令。

以上就是写Dockerfile文件所需要的基本指指令

写好之后我们切入Dockerfile目录下执行 docker build 即可制成我们自己的镜像,如:使用当前目录的Dockerfile文件创建镜像并设置标签,-t参数设置标签

docker build -t test:1.0 .

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
--build-arg list Set build-time variables
--cache-from strings Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair
Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair
Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is
'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--iidfile string Write the image ID to the file
--isolation string Container isolation technology
--label list Set metadata for an image
-m, --memory bytes Memory limit
--memory-swap bytes Swap limit equal to memory plus swap:
'-1' to enable unlimited swap
--network string Set the networking mode for the RUN
instructions during build (default "default")
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of
the image
-q, --quiet Suppress the build output and print image
ID on success
--rm Remove intermediate containers after a
successful build (default true)
--security-opt strings Security options
--shm-size bytes Size of /dev/shm
-t, --tag list Name and optionally a tag in the
'name:tag' format
--target string Set the target build stage to build.
--ulimit ulimit Ulimit options (default [])

把镜像保存到本地

docker save <保存到本地的文件> <镜像>

如:把test:1.0这个镜像保存到本地

docker save -o test_1.0.tar test:1.0

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
--build-arg list Set build-time variables
--cache-from strings Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair
Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair
Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is
'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--iidfile string Write the image ID to the file
--isolation string Container isolation technology
--label list Set metadata for an image
-m, --memory bytes Memory limit
--memory-swap bytes Swap limit equal to memory plus swap:
'-1' to enable unlimited swap
--network string Set the networking mode for the RUN
instructions during build (default "default")
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of
the image
-q, --quiet Suppress the build output and print image
ID on success
--rm Remove intermediate containers after a
successful build (default true)
--security-opt strings Security options
--shm-size bytes Size of /dev/shm
-t, --tag list Name and optionally a tag in the
'name:tag' format
--target string Set the target build stage to build.
--ulimit ulimit Ulimit options (default []) C:\Users\SunArmy\Desktop\demo>docker save --help Usage: docker save [OPTIONS] IMAGE [IMAGE...] Save one or more images to a tar archive (streamed to STDOUT by default) Options:
-o, --output string Write to a file, instead of STDOUT

Dockerfile镜像的制作的更多相关文章

  1. Docker 镜像的制作和使用

    镜像 Layer(层) 镜像里的内容是按「层」来组织的,「层」可以复用,一个完整的镜像也可以看做是一个「层」.多个「层」叠加在一起就形成了一个新的镜像,这个镜像也可以作为别的镜像的基础「层」进行更加复 ...

  2. NGINX镜像的制作

    NGINX镜像的制作 # mkdir -pv /opt/nginx # cd /opt/nginx/ # cat index.html www.dexter.com   编写Dockerfile # ...

  3. 基于url-to-pdf-api构建docker镜像,制作一个网页另存服务

    基于url-to-pdf-api构建docker镜像,制作一个网页另存服务 业务背景: 需要根据一个url路径打印这个网页的内容 解决方案: 1.使用wkhtml2pdf 2.使用puppeteer ...

  4. 封装win7系统、制作win7GHO镜像、制作一个自定义的镜像文件具体步骤、制作Win10镜像gho

    作者:导演你让灰太狼吃只羊 来源:CSDN 原文:https://blog.csdn.net/qq_35057426/article/details/83015516 版权声明:本文为博主原创文章,转 ...

  5. dockerfile镜像设置中文

    一.dockerfile镜像设置中文 centos镜像默认不支持中文,把下面的内容加到dockerfile即可 # 修改时区 RUN rm -rf /etc/localtime && ...

  6. Dockerfile镜像实例

    Dockerfile镜像实例 目录 Dockerfile镜像实例 一.构建SSH镜像 1. 建立工作目录 2. 生成镜像 3. 启动容器并修改root密码 二.systemctl镜像 1. 建立工作目 ...

  7. 7.云原生之Docker容器Dockerfile镜像构建浅析与实践

    转载自:https://www.bilibili.com/read/cv15220707/?from=readlist Dockerfile 镜像构建浅析与实践 描述:Dockerfile是一个文本格 ...

  8. 基于Dockerfile镜像制作的基本操作

    一.使用Dockerfile制作镜像 前面的博客中已经介绍了如何基于容器制作镜像,此方法的原理是使用一个正在运行的容器,根据生产所需进行配置更改等操作后,使其满足生产环境,再将这个容器打包制作为镜像, ...

  9. Dockerfile镜像制作时间同步

    1.问题描述 宿主机与容器时间相差8小时 2.原因 宿主机采用了CST时区,CST应该是指(China Shanghai Time,东八区时间)容器采用了UTC时区,UTC应该是指(Coordinat ...

随机推荐

  1. SVN A C D M G U R I 的含义

    A:add,新增 C:conflict,冲突 D:delete,删除 M:modify,本地已经修改 G:modify and merGed,本地文件修改并且和服务器的进行合并 U:update,从服 ...

  2. python3.x Day2 购物车程序练习

    购物车程序: 1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4. ...

  3. HTML-js 压缩上传的图片方法(默认上传的是file文件)

    //压缩图片方法 function compressImg(file,callback){ var src; var fileSize = parseFloat(parseInt(file['size ...

  4. HYSBZ - 1050(旅行comf 并查集Java实现)

    HYSBZ - 1050(旅行comf Java实现) 原题地址 解法:枚举每一条边,对于这条边,我们需要找到集合中和其值相差最小的最大边,这个集合是指与包括i边在内的ST联通集.对于这一要求,我们只 ...

  5. Placing Lampposts

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=91212#problem/E #include <iostream> #inc ...

  6. redis—持久化

    Redis持久化 概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis重启后,所有存储在内存中的数据将会丢失,在很多情况下是无法容忍这样的事情的.所以,我们需要将内 ...

  7. Vue2.0如何自定义时间过滤器

    我们知道Vue2.0开始不再支持自带的过滤器,需要我们自己去自定义过滤器,方法如下:           我们可以自己定义一个时间过滤器,在此引用了一个日期处理类库(Moment.js)可以很快的实现 ...

  8. vue组件知识总结

    vue组件化开发 将一个页面分割成若干个组件,一个页面js+css+html,将自己类容分割出来,方便开发,更好了维护我们的代码,每个组件封装自己的js+html+css,样式命名冲突 组件分类 页面 ...

  9. 使用ajax,后台传回的数据处理

    使用ajax,后台传到前台的是json数据,但这只是一个字符串的形式,需要进行转换为对象 转换后再使用$.each()方法进行遍历 使用alert(typeof  msg) ;  可以查看msg的类型 ...

  10. IBOutlet loadView UIButton的subview数量 UIWebView

    IBOutlet声明的插座变量和属性一起使用的时候,在.m文件调用的是属性. 在loadView方法中获取view属性会产生循环引用问题并导致内存溢出. Control+E到行尾,Control+A到 ...