docker(8):Dockerfile常用语法

一、实现功能

介绍Dockerfile常用额语法,从而可以自定义编写Dockerfile。

二、常用语法

1.FROM:选择基础image

FROM scratch #制作base image
FROM centos #使用现有base image
FROM ubuntu:14.04

ps:尽量使用官方image作为base image

2.LABEL:包含

LABEL maintainer="spark@126.com"
LABEL version="1.0"
LABEL description="This is a description"

ps:这些元数据必不可少

3.RUN:运行程序

RUN yum update && yum install -y vim \
    python-dev #反斜杠换行
RUN apt-get update && apt-get install -y perl \
    pwgen --no-install-recommends && rm -rf \
    /var/lib/apt/lists/* #注意清理缓存cache
    
ps:尽量使用"&&"和\把多条命令合并成一条

4.WORKDIR:设定工作目录

WORKDIR /test
WORKDIR demo
RUN pwd
结果:
/test/demo

ps:相当于mkdir + cd
使用WORKDIR,不要使用RUN cd
尽量使用绝对目录

5.ADD 和COPY

(1)ADD

把本地文件添加到当前image里面
ADD hello /

ADD可以添加并且解压缩
ADD test.tar.gz / #添加到根目录并解压

(2)COPY

COPY hello /

(3)两者区别

使用一般COPY优先,但是ADD可以解压功能
添加远程文件/目录,应当使用curl或者wget

5.ENV

ENV MYSQL_VERSION 5.7 #设置常量

RUN apt-get install -y mysql-server="${MYSQL_VERSION}" \
    && rm -rf /var/lib/apt/lists/* #引用常量
    
    
ps:尽量使用环境变量,利于维护

7.CMD 
(1)特点

-》容器启动使默认执行的命令

-》如果docker run指定了其他命令,CMD会被忽略
docker run -it [image] /bin/bash
则CMD会被忽略

-》如果定义多个cmd,则只有最后一个会被执行

(2)例子

Dockerfile内容
FROM centos
ENV name Docker
CMD echo "hello $name"

然后
[vagrant@localhost docker-entrypoint-shell]$ docker build -t spark/centos-cmd-shell .
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM centos
 ---> 9f38484d220f
Step 2/3 : ENV name Docker
 ---> Using cache
 ---> b2c5c3efb231
Step 3/3 : CMD echo "hello $name"
 ---> Running in 13d960536fd8
Removing intermediate container 13d960536fd8
 ---> 051e163b0309
Successfully built 051e163b0309
Successfully tagged spark/centos-cmd-shell:latest
[vagrant@localhost docker-entrypoint-shell]$ docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
spark/centos-cmd-shell   latest              051e163b0309        8 seconds ago       202MB
spark/centos-exec        latest              148b0c0780b0        2 hours ago         202MB
spark/centos-shell       latest              863515d94aba        2 hours ago         202MB
spark/centos-vim-new     latest              e3e86df752b6        21 hours ago        357MB
spark/hello-world        latest              d801c21d3e64        40 hours ago        857kB
centos                   latest              9f38484d220f        3 months ago        202MB
hello-world              latest              fce289e99eb9        5 months ago        1.84kB
[vagrant@localhost docker-entrypoint-shell]$ docker run spark/centos-cmd-shell
hello Docker
添加命令
[vagrant@localhost docker-entrypoint-shell]$ docker run spark/centos-cmd-shell /bin/bash
则不运行echo

8.ENTRYPOINT
(1)特点

-》让容器一应用程序或者服务的形式运行
-》不会被忽略,一定会执行

(2)实例

Dockerfile内容
FROM centos
ENV name Docker
ENTRYPOINT echo "hello $name"

[vagrant@localhost docker-entrypoint-shell]$ docker run spark/centos-shell
hello Docker
[vagrant@localhost docker-entrypoint-shell]$ docker run spark/centos-shell /bin/bash
hello Docker

三、demo

1.gitlib
https://github.com/docker-library

2.官方语法
https://docs.docker.com/v17.09/engine/reference/builder/
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值