dockerfile基本命令+镜像制作

hwwjian

关注

阅读 46

2023-12-10

image

DockerFile

03DockerFile

1.DockerFile的概念

用来构建docker​镜像的构建文件,由一系列参数和命令构成的脚本

大体总览:

image

1.构建过程

要遵循的规则:

image

2.执行流程

Docker​执行一个Dockerfile​脚本的流程大致如下

  1. Docker​从基础镜像运行一个容器
  2. 执行一条指令病对容器作出修改
  3. 执行类似docker commit​的操作提交一个新的镜像层
  4. docker​在基于刚提交的镜像运行一个新的容器
  5. 执行dockerfile​中的下一条指令直到所有执行完成

<span style="font-weight: bold;" data-type="strong">解释下:</span>

1.dockerfile_软件的原材料

2.docker镜像是软件的交付品

3.docker容器则可以认为是软件的运行态

image

‍3.常用的构建指令

<span style="font-weight: bold;" data-type="strong">指令</span> <span style="font-weight: bold;" data-type="strong">说明</span>
<span style="font-weight: bold;" data-type="strong">FROM</span> <span style="font-weight: bold;" data-type="strong">基础镜像,当前新镜像是基于哪个镜像的,有继承的意味</span>
<span style="font-weight: bold;" data-type="strong">MAINTAINER</span> <span style="font-weight: bold;" data-type="strong">镜像维护者的姓名和邮箱地址</span>
<span style="font-weight: bold;" data-type="strong">RUN</span> <span style="font-weight: bold;" data-type="strong">容器构建时需要运行的命令</span>
<span style="font-weight: bold;" data-type="strong">EXPOSE</span> <span style="font-weight: bold;" data-type="strong">当前容器对外暴露的端口</span>
<span style="font-weight: bold;" data-type="strong">WORKDIR</span> <span style="font-weight: bold;" data-type="strong">指定在创建容器后,终端默认登录的进来工作目录,一个落脚点</span>
<span style="font-weight: bold;" data-type="strong">ENV</span> <span style="font-weight: bold;" data-type="strong">用来在构建镜像过程中设置环境变量</span>
<span style="font-weight: bold;" data-type="strong">ADD</span> <span style="font-weight: bold;" data-type="strong">将宿主机目录下的文件拷贝进镜像且ADD命令会自动处理URL和解压tar压缩包</span>
<span style="font-weight: bold;" data-type="strong">COPY</span> <span style="font-weight: bold;" data-type="strong">类似ADD,拷贝文件和目录到镜像中。</span> <span style="font-weight: bold;" data-type="strong">将从构建上下文目录中<源路径>的文件/目录复制到新的一层的镜像内的<目标路径>位置</span> <span style="font-weight: bold;" data-type="strong">COPY src dest</span> <span style="font-weight: bold;" data-type="strong">COPY ["src","dest"]</span>
<span style="font-weight: bold;" data-type="strong">VOLUME</span> <span style="font-weight: bold;" data-type="strong">容器数据卷,用于数据保存和持久化工作</span>
<span style="font-weight: bold;" data-type="strong">CMD</span> <span style="font-weight: bold;" data-type="strong">指定一个容器启动时要运行的命令</span> <span style="font-weight: bold;" data-type="strong">Dockerfile中可以有多个CMD指令,但只有最后一个生效,CMD会被docker run之后的参数替换</span>
<span style="font-weight: bold;" data-type="strong">ENTRYPOINT</span> <span style="font-weight: bold;" data-type="strong">指定一个容器启动时要运行的命令</span> <span style="font-weight: bold;" data-type="strong">ENTRYPOINT的目的和CMD一样,都是在指定容器启动程序及参数</span>
<span style="font-weight: bold;" data-type="strong">ONBUILD</span> <span style="font-weight: bold;" data-type="strong">当构建一个被继承的Dockerfile时运行命令,父镜像在被子继承后父镜像的onbuild被触发</span>

<span style="font-weight: bold;" data-type="strong">DockerFile命令</span>

<span style="font-weight: bold;" data-type="strong">BUILD</span> <span style="font-weight: bold;" data-type="strong">BOTH</span> <span style="font-weight: bold;" data-type="strong">RUN</span>
<span style="font-weight: bold;" data-type="strong">FROM</span> <span style="font-weight: bold;" data-type="strong">WORKDIR</span> <span style="font-weight: bold;" data-type="strong">CMD</span>
<span style="font-weight: bold;" data-type="strong">MAINTAINER</span> <span style="font-weight: bold;" data-type="strong">USER</span> <span style="font-weight: bold;" data-type="strong">ENV</span>
<span style="font-weight: bold;" data-type="strong">COPY</span> <span style="font-weight: bold;" data-type="strong">EXPOSE</span>
<span style="font-weight: bold;" data-type="strong">ADD</span> <span style="font-weight: bold;" data-type="strong">VOLUME</span>
<span style="font-weight: bold;" data-type="strong">RUN</span> <span style="font-weight: bold;" data-type="strong">ENTRYPOINT</span>
<span style="font-weight: bold;" data-type="strong">ONBUILD</span>
<span style="font-weight: bold;" data-type="strong">.dockerignore</span> <br />

3.构建镜像

1.创建DockerFile​文件

vim  dockers

FROM centos:centos7

MAINTAINER aristo<boyunv@163.com>

ENV MYPATH /usr/local

WORKDIR  $MYPATH

RUN yum -y install vim

EXPOSE  80

CMD echo $MYPATH
CMD echo "success =------> boyunv!~"
CMD /bin/bash

2.构建镜像

 docker build -f dockers -t centos:1.0  .
 ##-f: 我们创建的dockerfile文件
 ##-t: 创建的文件名
 ## 切记后面的一个点不能忘记添加

<span style="font-weight: bold;" data-type="strong">构建的结果</span>

在这里插入图片描述

3.运行我们的创建的1.0​镜像

docker run -it centos:1.0

3.MYSQL的启动

docker run -p 12345:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v /root/mysql/data/:/var/lib/mysql/ -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
f349e5bcec0706a353880e5c5b17e73f2d3dda704e57425292f09e0c5397cfb8

精彩评论(0)

0 0 举报